Skip to main content

this is a package for weixin pay

Project description

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')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wxpay_sdk-0.0.4.tar.gz (14.1 kB view details)

Uploaded Source

File details

Details for the file wxpay_sdk-0.0.4.tar.gz.

File metadata

  • Download URL: wxpay_sdk-0.0.4.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for wxpay_sdk-0.0.4.tar.gz
Algorithm Hash digest
SHA256 4cbccbd23c88e8f832a1848594b3fddc02a97c225944d09fbdb682a1d841c26e
MD5 5b6f40b1cfbcd086cfb04d262a39e6f6
BLAKE2b-256 7b56f099733c1e98c85a2c2013fa478f2ca51aabad5f037179cc275084ec270a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page