国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home WeChat Applet WeChat Development Things to note when developing WeChat payment in Python

Things to note when developing WeChat payment in Python

Feb 25, 2017 am 10:43 AM

前言

微信支付是由微信及財付通聯(lián)合推出的移動支付創(chuàng)新產(chǎn)品。如今,隨著微信支付的全面開放,相關(guān)需求也越來越多,很多開發(fā)人員進行微信支付開發(fā)及商家申請微信支付時,面臨著諸多疑惑。

要想開發(fā)順利進行,首先要對業(yè)務(wù)流程有個清晰的認(rèn)識。這里以微信公眾號支付為例,因此也借用微信支付官方文檔中的業(yè)務(wù)流程圖:

Things to note when developing WeChat payment in Python

接下來來關(guān)注幾個開發(fā)過程中的關(guān)鍵點,包括:

????? 1、生成商戶訂單與調(diào)用統(tǒng)一下單 API

????? 2、微信服務(wù)器交互的數(shù)據(jù)格式

????? 3、公眾號支付下網(wǎng)頁內(nèi)通過 JS-API 調(diào)起支付

????? 4、異步通知商戶支付結(jié)果(回調(diào))?

一、生成商戶訂單與調(diào)用統(tǒng)一下單 API

這對應(yīng)業(yè)務(wù)流程中的第 4 和 第 5 步,商戶后臺首先為用戶生成訂單,然后調(diào)用微信的【統(tǒng)一下單】接口向微信支付系統(tǒng)提交訂單。這里有一個關(guān)鍵點就是簽名的生成。

簡單來講分為以下幾個步驟:

????? 1、將所有有效參數(shù)以“k=v”的形式進行拼接,有效參數(shù)是指非空參數(shù),也就是說如果參數(shù)為空,則不參與簽名;

????? 2、將所有的“k=v”對用“&”連接,得到“k1=v1&k2=v2&k3=v3”這樣的字符串;

????? 3、將微信支付 API 密鑰 拼接在最后,如“k1=v1&k2=v2&k3=v3&key=secret”;

????? 4、對整體進行 MD5 運算,即得到簽名。

這種簽名方法有一個高大上的名字叫做 HMAC(Hash-based Message Authentication Code,基于哈希的消息碼)。基于此思路,可以實現(xiàn)如下簽名方法:

def?gen_sign(params,?key):
??"""
??簽名生成函數(shù)
?
??:param?params:?參數(shù),dict?對象
??:param?key:?API?密鑰
??:return:?sign?string
??"""
?
??param_list?=?[]
??for?k?in?sorted(params.keys()):
????v?=?params.get(k)
????if?not?v:
??????#?參數(shù)的值為空不參與簽名
??????continue
????param_list.append('{0}={1}'.format(k,?v))
??#?在最后拼接?key
??param_list.append('key={}'.format(key))
??#?用?&?連接各?k-v?對,然后對字符串進行?MD5?運算
??return?md5('&'.join(param_list).encode('utf8')).hexdigest()

參與簽名的參數(shù)中有一個隨機字符串,在 Python 中有很多方法,當(dāng)然也可以利用 uuid 庫來生成:

def?gen_nonce_str():
??"""
??生成隨機字符串,有效字符a-zA-Z0-9
?
??:return:?隨機字符串
??"""
?
??return?''.join(str(uuid.uuid4()).split('-'))

?

二、微信服務(wù)器交互的數(shù)據(jù)格式

微信服務(wù)器與商戶服務(wù)器之間采用 XML 格式進行交互,這就涉及到與語言原生數(shù)據(jù)類型進行轉(zhuǎn)換以方便處理。交互的數(shù)據(jù)參數(shù)都是 key-value 的形式,因此在 Python 中使用字典會更加方便。而要解析 XML,也有一大把第三方庫供使用,比如 BeautifulSoup。

以下是具體實現(xiàn):

def?trans_xml_to_dict(xml):
??"""
??將微信支付交互返回的?XML?格式數(shù)據(jù)轉(zhuǎn)化為?Python?Dict?對象
?
??:param?xml:?原始?XML?格式數(shù)據(jù)
??:return:?dict?對象
??"""
?
??soup?=?BeautifulSoup(xml,?features='xml')
??xml?=?soup.find('xml')
??if?not?xml:
????return?{}
?
??#?將?XML?數(shù)據(jù)轉(zhuǎn)化為?Dict
??data?=?dict([(item.name,?item.text)?for?item?in?xml.find_all()])
??return?data
?
?
def?trans_dict_to_xml(data):
??"""
??將?dict?對象轉(zhuǎn)換成微信支付交互所需的?XML?格式數(shù)據(jù)
?
??:param?data:?dict?對象
??:return:?xml?格式數(shù)據(jù)
??"""
?
??xml?=?[]
??for?k?in?sorted(data.keys()):
????v?=?data.get(k)
????if?k?==?'detail'?and?not?v.startswith(''.format(v)
????xml.append('{value}{key}>'.format(key=k,?value=v))
??return?'<xml>{}</xml>'.format(''.join(xml))

注意 detail 參數(shù),即商品詳情,其值為 JSON 格式,在轉(zhuǎn)換為 XML 數(shù)據(jù)時應(yīng)前注意使用 CDATA 標(biāo)簽將其保護起來。

如:

<detail></detail>

三、公眾號支付下網(wǎng)頁內(nèi)通過 JS-API 調(diào)起支付

這一點對應(yīng)業(yè)務(wù)流程中的第 7 步。之所以提及它是因為微信官方文檔在此給開發(fā)者挖了一個坑(至少截至我在寫這篇文章時是的),就是在“網(wǎng)頁端調(diào)起支付API”中關(guān)于 JS 的示例代碼是采用的 WeixinJSBridge,這在很早以前就是 Deprecated 的“玩意兒”,如今更是已經(jīng)不可用了。正確的做法是使用 JS-SDK,可以參考微信公眾號的 wiki。

使用 JS-SDK 前需要先調(diào)用 config,這里也包含一個簽名,但注意這個簽名與之前微信支付的簽名并不相干。其首先需要用微信公眾號的 APPID 和 APPKEY 來換取 access_token,然后用該 access_token 調(diào)用 JS-SDK 換取 ticket 的接口得到 ticket,最后再使用該 ticket 和用戶當(dāng)前頁面的 URI 通過 sha1 運算生成簽名。

在此之后,即可調(diào)用 wx.chooseWXPay 來調(diào)起支付,這里也有一個坑:timestamp。wx.chooseWXPay 中的參數(shù)要求 timestamp 是全小寫。而微信支付中簽名時要求 timestamp 中的“s”是大寫。真的是要傻傻分不清了。?

四、異步通知商戶支付結(jié)果(回調(diào))

最后是關(guān)于異步回調(diào),對應(yīng)業(yè)務(wù)流程中的第 10 步。在用戶支付操作完成后,微信服務(wù)器會通過回調(diào)的形式告知商戶服務(wù)器支付結(jié)果。回調(diào)的地址與【統(tǒng)一下單】中定義的 notify_url 一致。當(dāng)接收到回調(diào)時,首先應(yīng)驗證簽名的有效性以保證“來源可靠”,然后可以通過回調(diào)中所帶的 openid、out_trade_no 等來定位唯一訂單。

總結(jié)

There are many forms of WeChat payment, and the business processes are also different. But as long as you can play with one of them, the others can basically be realized quickly. In addition, the implementation of the payment function involves security in the business process, so we must pay attention to clarify the business process and check all key nodes. The above is the entire content of this article. I hope it will be helpful to everyone in using Python to develop WeChat payment.

For more articles related to the precautions for developing WeChat payment in Python, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)