Native Wechat Payments.
Native here means that Wechat mobile app must be used in order to perform a payment.
pip install git+https://github.com/vericant/wechat_pay.git@0.5#egg=wechat_payFirst of all you need to initialize an object:
from wechat_pay import WechatPay
payment = WechatPay('<app id>', '<merchant id>', '<payments key>',
'<notify url>', '<server ip>')data = payment.retrieve_prepay_data('<product name>', '<transaction no>',
'<price>')It will return a dictionary with a bunch of stuff. The part that is particularly interesting here is code_url which looks something like this: weixin://wxpay/bizpayurl?pr=e42JWWu
With this code you could then generate a QR-code image:
qrencode -l H -t PNG -o qr.png 'weixin://wxpay/bizpayurl?pr=e42JWWu'…and show it to your user in order to let her pay.
Full explanation of how this works can be found in service documentation.
Right after user makes a payment, Wechat server hits you back to <notify url> you specified with POST request, body of which contains useful information. Let's say you got it in the xml variable:
if payment.validate_notification(xml):
# do something- Close the order. For following situations:
- The payment is failed, merchant need to generate new order so close the original order to avoid duplicated payments.
- Over the time, merchant need to close the order.
Note: The min gap time between request order and close the same order is 5 minutes.
data = payment.close_order('<transaction no>')It will return a dictionary with a bunch of stuff if closed successfully.
The 'return_code' in dictionary describe whether it's success/fail.
The 'err_code' in dictionary describe the error code if it failed.
Full explanation of how this works and error code details can be found in service documentation.
- Wechat payments (Chinese)
- JSAPI implementation by @richfisher which was used originally, and this code still inherits some approaches from.
Integrations were traditionally hard to test, but not anymore. Use localtunnel and be happy.