Skip to main content
wxpay_sdk

文档地址:

http://blog.08050142.com/2016/07/07/wxpay-sdk/

微信支付(暂时实现了扫码支付、app支付、h5支付、回调辅助函数)

由于工作中暂时只用到了这些,按照微信支付官方SDK的PHP版本,实现了python版本,后面如果有时间会继续实现其他类型支付,
有用到微信支付的童鞋,可以方便的使用之!!!


安装:
pip install wxpay_sdk

使用:
####################################
# 1.扫码支付 模式二 #
####################################

params = {

'body': u'Ipad mini 16G 白色', # 商品或支付单简要描述,例如:Ipad mini 16G 白色

'out_trade_no': '9001231230956', # 商户系统内部的订单号,32个字符内、可包含字母

'total_fee': 2, # 订单总金额,单位为分

'product_id': '1116', # 商品ID

'notify_url': 'http://145657w88r.iok.la/weixin/pay_callback/',

'trade_type':'NATIVE',

}


wechatpay_qrcode_config = {

'wechatpay_appid': 'xxxxxxxxxxx', # 必填,微信分配的公众账号ID

'wechatpay_key': 'xxxxxxxxxxx', # 必填,appid 密钥

'wechatpay_mchid': 'xxxxxxxxxxx', # 必填,微信支付分配的商户号

'wechatpay_appsecret': 'xxxxxxxxxxx',

}

wxpay = WxPayBasic(conf=wechatpay_qrcode_config)

code_url = wxpay.unifiedorder2_get_code_url(**params)

后续处理把code_url做成二维码供用户扫码支付
.........



####################################
# 2.app支付 #
####################################

params = {

'body': u'Ipad mini 16G 白色', # 商品或支付单简要描述,例如:Ipad mini 16G 白色

'out_trade_no': '9401231230956', # 商户系统内部的订单号,32个字符内、可包含字母

'total_fee': 2, # 订单总金额,单位为分

'product_id': '2116', # 商品ID

'notify_url': 'http://145657w88r.iok.la/weixin/pay_callback/',

'trade_type':'APP',

}

wechatpay_qrcode_config = {

'wechatpay_appid': 'xxxxxxxxxxx', # 必填,微信分配的公众账号ID

'wechatpay_key': 'xxxxxxxxxxx', # 必填,appid 密钥

'wechatpay_mchid': 'xxxxxxxxxxx', # 必填,微信支付分配的商户号

'wechatpay_appsecret': 'xxxxxxxxxxx',

}

wxpay = WxPayBasic(conf=wechatpay_qrcode_config)

app_result = wxpay.unifiedorder_get_app_url(**params)

后续处理把app_result传递给app客户端,由客户端sdk使用此参数发起请求即可
..........



####################################
# 3.微信公众号h5支付 #
####################################

params = {

'openid':'',

'body': u'Ipad mini 16G 白色', # 商品或支付单简要描述,例如:Ipad mini 16G 白色

'out_trade_no': '940123123sdaf956', # 商户系统内部的订单号,32个字符内、可包含字母

'total_fee': 1, # 订单总金额,单位为分

'product_id': '2116', # 商品ID

'notify_url': 'http://145657w88r.iok.la/weixin/pay_callback/',

'trade_type':'JSAPI',

}

wechatpay_qrcode_config = {

'wechatpay_appid': 'xxxxxxxxxxx', # 必填,微信分配的公众账号ID

'wechatpay_key': 'xxxxxxxxxxx',

'wechatpay_mchid': 'xxxxxxxxxxx', # 必填,微信支付分配的商户号

'wechatpay_appsecret': 'xxxxxxxxxxx', # 必填,appid 密钥

}

wxpay = WxPayBasic(conf=wechatpay_qrcode_config)

app_result = wxpay.get_js_api_parameters(**params)

后续处理把app_result传递给微信js客户端,由客户端sdk使用此参数发起请求即可
..........



####################################
# 4支付回调定义 (注意:扫码支付&&app支付,使用的是不同config)#
# (以django的views为例)
####################################

@csrf_exempt

def wechat_pay_callback(request, *args, **kwargs):

req_xml_str = request.body

# 回调处理:签名验证,订单查询验证
# 返回验证结果(可作为直接返回给微信的xml)
wechatpay_qrcode_config = {
'wechatpay_appid': 'xxxxxxxxxxx', # 必填,微信分配的公众账号ID
'wechatpay_key': 'xxxxxxxxxxx', # 必填,appid 密钥
'wechatpay_mchid': 'xxxxxxxxxxx', # 必填,微信支付分配的商户号
'wechatpay_appsecret': 'xxxxxxxxxxx',
}
# wechatpay_qrcode_config = {
# 'wechatpay_appid': 'xxxxxxxxxxx', # 必填,微信分配的公众账号ID
# 'wechatpay_key': 'xxxxxxxxxxx', # 必填,appid 密钥
# 'wechatpay_mchid': 'xxxxxxxxxxx', # 必填,微信支付分配的商户号
# 'wechatpay_appsecret': 'xxxxxxxxxxx',
# }
wxpay = WxPayBasic(conf=wechatpay_qrcode_config)
res_xml_str = wxpay.wxpay_callback(req_xml_str)

res_xml_dict = xmltodict.parse(res_xml_str)
if res_xml_dict['xml']['return_code'] == 'SUCCESS':
# 处理商户订单逻辑
req_xml_dict = xmltodict.parse(req_xml_str)
total_fee = req_xml_dict['xml']['total_fee']
out_trade_no = req_xml_dict['xml']['out_trade_no']
............
else:
print 'wxpay callback error'

return HttpResponse(res_xml_str, content_type='text/xml')

Release files for wxpay_sdk 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wxpay_sdk 0.0.4
File Size Uploaded
wxpay_sdk-0.0.4.tar.gz 14.1 kB Details

Release files / wxpay_sdk-0.0.4.tar.gz

Download URL wxpay_sdk-0.0.4.tar.gz
Size 14.1 kB
Tags Source
SHA-256 checksum
How to use checksums
4cbccbd23c88e8f832a1848594b3fddc02a97c225944d09fbdb682a1d841c26e
BLAKE2b-256 checksum
How to use checksums
7b56f099733c1e98c85a2c2013fa478f2ca51aabad5f037179cc275084ec270a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.4 This release

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page