Skip to content

Commit 59f2662

Browse files
author
Jolly23
committed
new: dev: 加入微信刷卡支付功能,加入使用说明及example
*加入功能:微信刷卡支付,详见https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapt er=9_10&index=1 *example.py中补充刷卡支付功能的调用实例 *README.md中补充刷卡支付功能的介绍 *检查全部功能还可以正常使用,微信没有对其他接口进行更改。 ——2017/05/19
1 parent 3075a66 commit 59f2662

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-8
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,23 @@
114114
# 如果check_name为True,下面re_user_name必须传入
115115
# 如果check_name为False,请删除下一行参数re_user_name
116116
re_user_name=u'***客户的真实姓名***', # 校验不成功付款会是失败
117-
amount=100, # total_fee 单位是 分, 100 = 1元, 单用户 单笔上限/当日上限:2W/2W
117+
amount=100, # amount 单位是 分, 100 = 1元, 单用户 单笔上限/当日上限:2W/2W
118118
desc=u'充值失败退款', # 付款原因
119119
spbill_create_ip='222.222.222.222', # 调用微信企业付款接口服务器公网IP地址
120120
)
121121
```
122122

123+
提交刷卡支付请求(通过微信钱包付款码的方式付款)
124+
商户需可通过 设备扫码 等方式获取到客户付款码,向微信提交支付请求
125+
```python
126+
wx_pay.swiping_card_payment(
127+
body=u'***商品名称/付款显示名称***', # 例如:综合超市
128+
total_fee=100, # total_fee 单位是 分, 100 = 1元, 单用户 单笔上限/当日上限:2W/2W
129+
auth_code='131336161431593669', # 扫码支付授权码,设备读取用户微信中的条码或者二维码信息(注:用户刷卡条形码规则:18位纯数字,以10、11、12、13、14、15开头)
130+
spbill_create_ip='222.222.222.222', # 调用微信企业付款接口服务器公网IP地址
131+
)
132+
```
133+
123134
## 工具函数
124135

125136
签名

example.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
from flask import jsonify
5-
from time import time
64
from hashlib import sha1
5+
from time import time
6+
7+
from flask import jsonify
78

89
from wx_pay import WxPay, WxPayError
910

@@ -190,12 +191,30 @@ def enterprise_payment_to_wallet():
190191
# api_cert_path: 微信支付商户证书(apiclient_key.pem)的本地保存路径
191192
api_key_path='/home/xxx/SERVER/ext_file/apiclient_key.pem',
192193
openid=u'***to_user_openid***', # 要接收转账的用户openid
193-
check_name=True, # 是否强制校验收款用户姓名
194+
check_name=True, # 是否强制校验收款用户姓名
194195
# 如果check_name为True,下面re_user_name必须传入
195196
# 如果check_name为False,请删除下一行参数re_user_name
196197
re_user_name=u'***客户的真实姓名***', # 校验不成功付款会是失败
197198
amount=100, # total_fee 单位是 分, 100 = 1元, 单用户 单笔上限/当日上限:2W/2W
198-
desc=u'充值失败退款', # 付款原因
199+
desc=u'充值失败退款', # 付款原因
200+
spbill_create_ip='222.222.222.222', # 调用微信企业付款接口服务器公网IP地址
201+
)
202+
203+
204+
def swiping_card_example():
205+
"""
206+
刷卡支付 example
207+
"""
208+
wx_pay = WxPay(
209+
wx_app_id='WX_APP_ID',
210+
wx_mch_id='WX_MCH_ID',
211+
wx_mch_key='WX_MCH_KEY',
212+
wx_notify_url='http://www.example.com/pay/weixin/notify'
213+
)
214+
wx_pay.swiping_card_payment(
215+
body=u'***商品名称/付款显示名称***', # 例如:综合超市
216+
total_fee=100, # total_fee 消费金额 单位是 分
217+
auth_code='131336161431593669', # 获取到的客户微信付款码
199218
spbill_create_ip='222.222.222.222', # 调用微信企业付款接口服务器公网IP地址
200219
)
201220

wx_pay.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# -*- coding: utf-8 -*-
2-
import time
3-
import string
4-
import random
52
import hashlib
3+
import random
4+
import string
5+
import time
66
import urllib2
7+
78
import requests
89

910
try:
@@ -400,3 +401,45 @@ def enterprise_payment(self, api_cert_path, api_key_path, **data):
400401
if raw["return_code"] == "FAIL":
401402
raise WxPayError(raw["return_msg"])
402403
return raw
404+
405+
def swiping_card_payment(self, **data):
406+
"""
407+
提交刷卡支付
408+
详细规则参考 https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1
409+
410+
:param data: body, out_trade_no, total_fee, auth_code, (可选参数 device_info, detail, goods_tag, limit_pay)
411+
body: 商品描述
412+
*out_trade_no: 商户订单号
413+
total_fee: 标价金额, 整数, 单位 分
414+
auth_code: 微信支付二维码扫描结果
415+
*device_info: 终端设备号(商户自定义,如门店编号)
416+
user_ip 在flask框架下可以自动填写, 非flask框架需传入spbill_create_ip
417+
:return: 统一下单生成结果
418+
"""
419+
url = "https://api.mch.weixin.qq.com/pay/micropay"
420+
421+
# 必填参数
422+
if "body" not in data:
423+
raise WxPayError(u"缺少刷卡支付接口必填参数body")
424+
if "total_fee" not in data:
425+
raise WxPayError(u"缺少刷卡支付接口必填参数total_fee")
426+
if "out_trade_no" not in data:
427+
data.setdefault("out_trade_no", self.nonce_str())
428+
429+
user_ip = self.user_ip_address()
430+
if not user_ip and "spbill_create_ip" not in data:
431+
raise WxPayError(u"当前未使用flask框架,缺少刷卡支付接口必填参数spbill_create_ip")
432+
433+
data.setdefault("appid", self.WX_APP_ID)
434+
data.setdefault("mch_id", self.WX_MCH_ID)
435+
data.setdefault("nonce_str", self.nonce_str())
436+
data.setdefault("spbill_create_ip", user_ip)
437+
data.setdefault("sign", self.sign(data))
438+
439+
raw = self.fetch(url, data)
440+
if raw["return_code"] == "FAIL":
441+
raise WxPayError(raw["return_msg"])
442+
err_msg = raw.get("err_code_des")
443+
if err_msg:
444+
raise WxPayError(err_msg)
445+
return raw

0 commit comments

Comments
 (0)