WeChat Pay Secure Integration
Source: Parent-Child Growth Diary Kit
AI How-To.md §3.3 WeChat Pay, decoupled from project name to become a universal integration specification.
What It Is
WeChat Pay integration involves fixed interfaces and a set of security ironclads (callback signature verification, idempotency, key management). This skill provides both a "must-implement" checklist and generates a FastAPI payment module skeleton (routes + WeChat Pay client + callback verification + idempotency + JSAPI payment parameters + .env template), saving you from starting from scratch and encountering pitfalls.
The script includes --self-test: it uses temporary RSA keys + random APIv3 key to verify signature/signing verification and callback message AES encryption/decryption round-trip correctness, proving the generated cryptographic logic works — you don't have to trust it blindly.
When to Use
- User says: "Integrate WeChat Pay" / "Mini Program payment" / "How to write WeChat Pay callback" / "WeChat refund" / "How to calculate paySign"
- Any WeChat Mini Program / Official Account project that needs to accept payments
Pre-requisite Credentials Before Development (P0, otherwise code won't run)
- WeChat Merchant ID MCH_ID (obtain from pay.weixin.qq.com)
- API v3 Key (Merchant Platform → Account Center → API Security → Set APIv3 Key, 32 characters)
- Merchant Certificate (apiclient_cert.pem + apiclient_key.pem, download from API Security page)
- Mini Program / Official Account AppID (bound to the merchant ID)
- Payment callback domain (configure whitelist on Merchant Platform)
WeChat Pay Interface Specification (Pre-built)
| Interface | Description | Key Notes |
|---|---|---|
JSAPI Order POST /v3/pay/transactions/jsapi | Mini Program / Official Account payment | Need to pass openid |
Native Order POST /v3/pay/transactions/native | QR code payment | Returns QR code URL |
H5 Order POST /v3/pay/transactions/h5 | Mobile browser payment | Need to configure H5 payment domain |
Query Order GET /v3/pay/transactions/out-trade-no/{out_trade_no} | Active query | Compensation for callback failure |
Close Order POST /v3/pay/transactions/out-trade-no/{out_trade_no}/close | Close order | Close on timeout without payment |
Payment Callback POST /your-callback-path | Async notification | Must verify signature, this is the basis for payment success |
Apply Refund POST /v3/refund/domestic/refunds | Refund | Need merchant certificate |
Mandatory Security Constraints (P0, none can be omitted)
- Payment result relies on async callback as the sole authority; do not trust front-end callbacks alone to determine payment success
- Callback interface must verify WeChat signature (
Wechatpay-Signatureheader + platform certificate) - Callback interface must implement idempotency (process same order only once even with multiple callbacks, using order number unique constraint / processed flag)
- Payment keys (apiv3_key, private key files) only stored on server locally /
.env, not committed to Git, not written to logs - Payment amount computed server-side, not trusting amount parameter from front-end
Script Usage (Generate FastAPI Payment Module Skeleton)
# 0) First verify the generated cryptographic logic works (recommended before first use)
python gen_pay.py --self-test
# 1) Generate module
python gen_pay.py --out ./pay_module
Generates:
pay_module/wechat_pay_client.py— Unified order (JSAPI/Native/H5), query, close, refund; signature and certificate loading;build_jsapi_payment()directly calculates thepaySignrequired by frontendwx.requestPayment; callback message AES-256-GCM decryption; auto-fetch and verification of platform certificatepay_module/router.py—/api/pay/create,/api/pay/notify(verify signature + idempotent),/api/pay/refundpay_module/.env.example— Key fields to fill in (never commit real values)pay_module/README.md— Integration steps
The generated code is a skeleton; you need to fill in real AppID / Merchant ID, and implement order table and "processed flag" logic (idempotency grounding).
Security Requirements (Re-emphasized)
- apiv3_key and private key file paths only stored in
.env, never appear in code or Git - If callback signature verification fails, return failure and do not update order status
- WeChat Pay certificates are rotated periodically; platform certificate is auto-fetched and cached on the first callback
Notes
- WeChat Pay v3 uses AES-256-GCM to decrypt callback messages and RSA for signature verification, requires
cryptographylibrary; the script will note dependencies - This skill only produces server-side integration skeleton; on the mini-program side, after receiving the
paymentobject from/api/pay/create, directly callwx.requestPayment(payment)(already includes paySign) - Use with
wechat-miniprogram-devrules(security boundaries) andai-work-acceptance(acceptance) for best results