pywechatpay
pywechatpay 是微信支付V3版接口的python SDK.
功能介绍
- 接口 SDK. 请看
services里面的README.md文档. - HTTP 客户端
core.client, 支持请求签名和应答验签. 如果 SDK 未支持你需要的接口, 请用此客户端发起请求. - 回调通知处理
core.notify, 支持微信支付回调通知的验证和解密. - 证书下载等辅助能力
使用教程
安装
从 PyPi 安装:
$ pip install pywechatpay
准备
参考微信官方文档准备好密钥, 证书文件和配置( 证书/密钥/签名介绍)
初始化
from pywechatpay.core.client import with_wechat_pay_auto_auth_cipher
MCH_ID = "xxx"
MCH_SERIAL_NO = "xxx"
MCH_PRIVATE_KEY_STRING = "xxx"
APIv3_KEY = "xxx"
# 初始化 client, 并使它具有获取微信支付平台证书的能力
client = with_wechat_pay_auto_auth_cipher(MCH_ID, MCH_SERIAL_NO, MCH_PRIVATE_KEY_STRING, APIv3_KEY)
接口
- APP支付 pay/transactions/app
from pywechatpay.services.payments.app import AppApiService
svc = AppApiService(client)
# 方式一, 返回客户端调起微信支付的参数
result = svc.prepay_with_request_payment(appid="xxx", mchid="xxx", description="xxx", out_trade_no="xxx", total=1,
notify_url="xxx")
# 方式二, 返回原微信返回的响应
result = svc.pay_transactions_app(appid="xxx", mchid="xxx", description="xxx", out_trade_no="xxx", total=1,
notify_url="xxx")
# 查询订单
# 微信支付订单号查询
result = svc.pay_transactions_id(mchid="xxx", transaction_id="xxx")
# 商户订单号查询
result = svc.pay_transactions_out_trade_no(mchid="xxx", out_trade_no="xxx")
- H5支付 pay/transactions/h5
from pywechatpay.services.payments.h5 import H5ApiService
svc = H5ApiService(client)
result = svc.pay_transactions_h5(appid="xxx", mchid="xxx", description="xxx", out_trade_no="xxx", total=1,
payer_client_ip="x.x.x.x", notify_url="xxx")
发送 HTTP 请求
如果 SDK 还未支持你需要的接口, 使用 core.client.Client 的 GET,POST 等方法发送 HTTP 请求,而不用关注签名,验签等逻辑
# Native支付 https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_1.shtml
url = "https://api.mch.weixin.qq.com/v3/pay/transactions/native"
content = {
"appid": "xxx",
"mchid": "xxx",
"description": "xxx",
"out_trade_no": "xxx",
"notify_url": "xxx",
"amount": {"total": 1},
}
response = client.request("post", url, json=content)
print(response.json())
回调通知的验签和解密
from pywechatpay.core import downloader_mgr
from pywechatpay.core.notify import new_notify_handler
from pywechatpay.core.verifier import SHA256WithRSAVerifier
# 为回调请求的头部, 字典类型
headers = {
"xxx": "xxx",
}
# 为回调请求的内容, 字符串类型
body = "xxx"
cert_visitor = downloader_mgr.mgr_instance.get_certificate_visitor(mch_id="xxx")
handler = new_notify_handler(mch_api_v3_key="xxx", verifier=SHA256WithRSAVerifier(cert_visitor))
notify_req = handler.parse_notify_request(headers=headers, body=body)
参考链接
Metadata
Release files for pywechatpay 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pywechatpay-0.1.0.tar.gz | 3.5 kB | Without provenance | Details for pywechatpay-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pywechatpay-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pywechatpay-0.1.0-py3-none-any.whl |
Total release size: 10.6 kB
Release files / pywechatpay-0.1.0.tar.gz
| Download URL | pywechatpay-0.1.0.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9c95a5c4c40e178578637f1eabae94cae85889e8d2abab2de44b659850c69a5c
|
|
BLAKE2b-256 checksum How to use checksums |
aecc46f450ea9f4f04d2018eb54ff73cca648731dbc28fcd59794b5e533f9678
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.5.0.1 requests/2.22.0 requests-toolbelt/0.8.0 tqdm/4.43.0 CPython/3.7.6
|
Release files / pywechatpay-0.1.0-py3-none-any.whl
| Download URL | pywechatpay-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
24275a04d1a4eda830c6dada2d3ff857ae4e0817530f3fa43bef2be59c35c2d6
|
|
BLAKE2b-256 checksum How to use checksums |
78a862d903d52885e8aa36ea0ed438b1560a18e9419753b227bca6d94833b04a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.5.0.1 requests/2.22.0 requests-toolbelt/0.8.0 tqdm/4.43.0 CPython/3.7.6
|
Security for pywechatpay 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.