python语言实现微信支付接口,扫一扫和H5

Python实现微信支付
一、准备环境
1、要有微信公众号,商户平台账号
https://pay.weixin.qq.com/wiki/doc/api/index.html

安装 requests库
pip install -r requirements.txt

安装第三方库
python setup.py install

示例
以统一下单为例:

#coding: utf-8
from pywxpay import WXPay
wxpay = WXPay(app_id=‘wx8888888998’,
mch_id=‘8888888’,
key=‘123434556677888999987766543543322’,
cert_pem_path=’/path/to/apiclient_cert.pem’,
key_pem_path=’/path/to/apiclient_key.pem’,
timeout=6.0)

wxpay_resp_dict = wxpay.unifiedorder(dict(device_info=‘WEB’,
body=‘测试商家-商品类目’,
detail=’’,
out_trade_no=‘2016090910595900000012’,
total_fee=1,
fee_type=‘CNY’,
notify_url=‘http://www.debug5.com’,
spbill_create_ip=‘123.12.12.123’,
trade_type=‘NATIVE’)
)

print( wxpay_resp_dict )

github地址:https://github.com/whisnos/wxpay.git

https://github.com/whisnos/wxpay

第一发表于:http://www.debug5.com

程序猿的道德修养:
本博客源码Github地址:
https://github.com/whisnos/myblog
(仅供学习使用)请随手给个star,谢谢

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信支付的 v3 版本相比较 v2 版本有了很多变化和升级,包括接口地址、签名方式、请求参数等等。在 Python 中对接微信支付 v3 接口,需要使用到官方提供的 SDK 和第三方库。 下面是一个简单的对接微信支付 v3 的示例代码: 1. 安装依赖库:需要安装 `wechatpay` 和 `requests` 两个库,可以通过 pip 命令进行安装: ```python pip install wechatpay requests ``` 2. 导入 SDK 和库: ```python import wechatpay import wechatpay.utils as utils import requests ``` 3. 配置商户证书和密钥: ```python merchant_id = '商户号' api_key = 'API密钥' cert_file = 'apiclient_cert.pem' key_file = 'apiclient_key.pem' ``` 4. 使用 SDK 创建支付订单: ```python url = 'https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi' nonce_str = utils.generate_random_string(32) timestamp = utils.get_current_timestamp() body = { "mchid": merchant_id, "appid": "应用ID", "description": "商品描述", "out_trade_no": "商户订单号", "amount": { "total": 1, "currency": "CNY" }, "payer": { "openid": "用户openid" } } headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'WECHATPAY2-SHA256-RSA2048 ' + wechatpay.get_authorization_header( method='POST', url=url, body=body, merchant_id=merchant_id, api_key=api_key, cert_file=cert_file, key_file=key_file, nonce_str=nonce_str, timestamp=timestamp ) } response = requests.post(url, headers=headers, json=body) ``` 5. 处理返回结果: ```python if response.status_code == 200: result = response.json() prepay_id = result.get('prepay_id') return_data = { 'appId': '应用ID', 'timeStamp': str(timestamp), 'nonceStr': nonce_str, 'package': 'prepay_id=' + prepay_id, 'signType': 'RSA', 'paySign': wechatpay.get_sha256withrsa_signature( timestamp + '\n' + nonce_str + '\n' + 'prepay_id=' + prepay_id + '\n', key_file=key_file ) } else: error_msg = response.json().get('message') return {'error': error_msg} ``` 以上是一个简单的微信支付 v3 对接示例,具体实现还需要根据自己的业务需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值