Skip to main content

Python SDK for WechatPay V3

Project description

pywechatpay

PyPI version

pywechatpay 是微信支付V3版接口的python SDK.

功能介绍

  1. 接口 SDK. 请看 services 里面的 README.md 文档.
  2. HTTP 客户端 core.client, 支持请求签名和应答验签. 如果 SDK 未支持你需要的接口, 请用此客户端发起请求.
  3. 回调通知处理 core.notify, 支持微信支付回调通知的验证和解密.
  4. 证书下载等辅助能力

使用教程

安装

从 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)

接口

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")
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)

参考链接

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pywechatpay-0.1.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

pywechatpay-0.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file pywechatpay-0.1.0.tar.gz.

File metadata

  • Download URL: pywechatpay-0.1.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for pywechatpay-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9c95a5c4c40e178578637f1eabae94cae85889e8d2abab2de44b659850c69a5c
MD5 e9146d37749c596b775105c3f2e4f835
BLAKE2b-256 aecc46f450ea9f4f04d2018eb54ff73cca648731dbc28fcd59794b5e533f9678

See more details on using hashes here.

File details

Details for the file pywechatpay-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pywechatpay-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for pywechatpay-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 24275a04d1a4eda830c6dada2d3ff857ae4e0817530f3fa43bef2be59c35c2d6
MD5 afc52815d78b6abc3edf9634d24afc09
BLAKE2b-256 78a862d903d52885e8aa36ea0ed438b1560a18e9419753b227bca6d94833b04a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page