問題背景
最近接入微信支付,微信官方並沒有提供Python版的服務端SDK,因而只能根據文檔手動實現一版,這里記錄一下微信支付的整體流程、踩坑過程與最終具體實現。
微信支付APP下單流程
根據微信官方文檔: https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_5_2.shtml
下單流程如下:
和支付寶不同,微信多了一個預付單的概念,這里把APP下單實際分為四大部分,其中包含請求微信后端需要的首次簽名和需要返回給APP的二次支付信息簽名--這里踩一個小坑,流程圖中並沒把第二次簽名支付信息需要返回給APP的步驟畫出來(即下面的步驟6.5),因而一開始誤以為只需要返回prepay_id給客戶端,導致校驗失敗。
一. 對應步驟1~4,APP 請求業務后端,業務后台進行V3簽名后,請求微信后端生成預付單prepay_id
二. 對應步驟5~6.5,業務后端收到微信后端返回prepay_id,將支付相關參數打包進行二次簽名后返回給APP,這里相比流程圖多了一個6.5--即業務后端返回簽名支付信息到APP
三. 對應步驟7~18,APP收到業務后端返回簽名支付信息后調起SDK發起支付請求,收到同步消息結果通知
四. 對應步驟19~22,APP查詢業務后端,業務后端通過回調通知或直接查詢微信后端返回最終支付結果
代碼實現
首次簽名邏輯
第一次請求生成預付單號的簽名文檔為:https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_0.shtml, 共5個部分參與簽名,其組成格式為:
HTTP請求方法\nURL\n請求時間戳\n請求隨機串\n請求報文主體\n
對應簽名代碼:
class WechatPayDALBase(object):
def __init__(self, mch_appid, mchid, v3key, serial_no, client_key):
self.mch_appid = mch_appid
self.mchid = mchid
self.v3key = v3key
# serial_no可通過openssl直接獲取, 例: openssl x509 -in 1900009191_20180326_cert.pem -noout -serial
self.serial_no = serial_no
with open(client_key, 'r') as ifile:
pkey = RSA.importKey(ifile.read())
self.signer = pkcs1_15.new(pkey)
def compute_sign_v3(self, method, url, body):
'''
V3簽名邏輯
'''
ts = int(time.time())
nonce = self.generate_nonce()
uparts= parse_url(url)
ustr = uparts.path + ('?{}'.format(uparts.query) if uparts.query else '')
content = '{}\n{}\n{}\n{}\n{}\n'.format(method, ustr, ts, nonce, body)
digest = SHA256.new(content.encode('utf-8'))
sign_v = base64.b64encode(self.signer.sign(digest)).decode('utf-8')
sign_str = 'serial_no="{}",mchid="{}",timestamp="{}",nonce_str="{}",signature="{}"'.format(
self.serial_no, self.mchid, ts, nonce, sign_v)
return sign_str
def make_headers_v3(self, url, headers=None, body='', method='GET'):
'''
微信支付V3版本簽名header生成函數
'''
if not headers:
headers = {}
headers['Accept'] = 'application/json'
sign = self.compute_sign_v3(method, url, body)
auth_info = 'WECHATPAY2-SHA256-RSA2048 {}'.format(sign)
headers['Authorization'] = auth_info
return headers
def generate_nonce(self):
rnd = int(time.time()) + random.randint(100000, 1000000)
nonce = hashlib.md5(str(rnd).encode()).hexdigest()[:16]
return nonce
二次簽名邏輯
由業務后端返回給APP的二次簽名信息文檔為:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_2_4.shtml
共4個部分參與簽名,其組成格式為:
應用id\n時間戳\n隨機字符串\n預支付交易會話ID\n
返回簽名支付信息的對應代碼:
def get_pay_sign_info(self, prepay_id):
ts = int(time.time())
nonce = self.generate_nonce()
content = '{}\n{}\n{}\n{}\n'.format(self.mch_appid, ts, nonce, prepay_id)
digest = SHA256.new(content.encode('utf-8'))
sign_v = base64.b64encode(self.signer.sign(digest)).decode('utf-8')
return {
'appid': self.mch_appid,
'partnerid': self.mchid,
'timestamp': str(ts),
'noncestr': nonce,
'prepay_id': prepay_id,
'package': 'Sign=WXPay',
'sign': sign_v,
}
業務后端查詢訂單詳情
文檔地址:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_2_2.shtml
代碼如下:
def query_order(self, out_trade_no):
'''
查詢指定訂單信息
'''
url = f'https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out_trade_no}?mchid={self.mchid}'
headers = self.make_headers_v3(url)
rsp = requests.get(url, headers=headers)
pay_logger.info('out_trade_no:{}, rsp:{}|{}'.format(out_trade_no, rsp.status_code, rsp.text))
rdct = rsp.json()
return rdct
業務后端調用APP下單API
文檔地址:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_2_1.shtml
代碼如下:
def create_order_info(self, data, callback_url):
'''
創建微信預支付訂單, 注意包含兩次簽名過程:
首次簽名用於請求微信后端獲取prepay_id
二次簽名信息返回客戶端用於調起SDK支付
'''
url = 'https://api.mch.weixin.qq.com/v3/pay/transactions/app'
ndt = datetime.now()
out_trade_no = self.generate_partner_trade_no(ndt)
data = {
'mchid': self.mchid,
'out_trade_no': out_trade_no,
'appid': self.mch_appid,
'description': data['subject'],
'notify_url': callback_url,
'amount': {
'currency': 'CNY',
'total': int(data['price']),
},
'time_expire': (ndt + timedelta(minutes=5)).strftime('%Y-%m-%dT%H:%M:%S+08:00')
}
jdata = json.dumps(data, separators=[',', ':'])
headers = {'Content-Type': 'application/json'}
# 第一次簽名, 直接請求微信后端
headers = self.make_headers_v3(url, headers=headers, body=jdata, method='POST')
rsp = requests.post(url, headers=headers, data=jdata)
pay_logger.info('rsp:{}|{}'.format(rsp.status_code, rsp.text))
rdct = rsp.json()
# 第二次簽名, 返回給客戶端調用
sign_info = self.get_pay_sign_info(rdct['prepay_id'])
return sign_info
源碼地址
試水代碼開源,把相關代碼分享在了github:https://github.com/liuzhi67/wechat-pay-python
轉載請注明出處,原文地址:https://www.cnblogs.com/AcAc-t/p/wechat_pay_by_python.html