CommunityCoding & Developmentgithub.com

WeChat Pay Integration

A comprehensive integration specification for WeChat Pay (JSAPI/Native/Refund with security): mandatory callback signature verification, idempotency, and key isolation. Suitable for any WeChat application that needs to accept payments.

What is WeChat Pay Integration?

This skill provides a secure and complete integration specification for WeChat Pay, covering JSAPI (mini-program), Native (QR code), H5, and refund flows. It enforces critical security rules: mandatory callback signature verification using WeChat's platform certificate, idempotent order processing to prevent double charging, and strict key isolation (APIV3 key and merchant certificate never committed to Git or logged). The skill includes a Python script (`gen_pay.py`) that generates a FastAPI payment module skeleton with client, router, and environment template. It features a `--self-test` mode that uses temporary RSA keys and a random APIV3 key to verify signature/verification and callback AES encryption/decryption round-trip correctness, ensuring the generated cryptography logic works before production use. It is designed for developers using FastAPI (Python) and supports platforms such as WeChat Mini Programs, Official Accounts, and mobile browsers. The generated code includes auto-fetching and caching of WeChat platform certificates, AES-256-GCM callback decryption, and direct `paySign` computation for front-end `wx.requestPayment`.

Works with~Claude Code~Codex CLI~Cursor
npx skills add shuangying0001-beep/wechat-pay-integration

Installed? Explore more Coding & Development skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

Ask in your favorite AI

Open a new chat with this agent skill pre-loaded.

Documentation

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)

InterfaceDescriptionKey Notes
JSAPI Order POST /v3/pay/transactions/jsapiMini Program / Official Account paymentNeed to pass openid
Native Order POST /v3/pay/transactions/nativeQR code paymentReturns QR code URL
H5 Order POST /v3/pay/transactions/h5Mobile browser paymentNeed to configure H5 payment domain
Query Order GET /v3/pay/transactions/out-trade-no/{out_trade_no}Active queryCompensation for callback failure
Close Order POST /v3/pay/transactions/out-trade-no/{out_trade_no}/closeClose orderClose on timeout without payment
Payment Callback POST /your-callback-pathAsync notificationMust verify signature, this is the basis for payment success
Apply Refund POST /v3/refund/domestic/refundsRefundNeed merchant certificate

Mandatory Security Constraints (P0, none can be omitted)

  1. Payment result relies on async callback as the sole authority; do not trust front-end callbacks alone to determine payment success
  2. Callback interface must verify WeChat signature (Wechatpay-Signature header + platform certificate)
  3. Callback interface must implement idempotency (process same order only once even with multiple callbacks, using order number unique constraint / processed flag)
  4. Payment keys (apiv3_key, private key files) only stored on server locally / .env, not committed to Git, not written to logs
  5. 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 the paySign required by frontend wx.requestPayment; callback message AES-256-GCM decryption; auto-fetch and verification of platform certificate
  • pay_module/router.py — /api/pay/create, /api/pay/notify (verify signature + idempotent), /api/pay/refund
  • pay_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 cryptography library; the script will note dependencies
  • This skill only produces server-side integration skeleton; on the mini-program side, after receiving the payment object from /api/pay/create, directly call wx.requestPayment(payment) (already includes paySign)
  • Use with wechat-miniprogram-devrules (security boundaries) and ai-work-acceptance (acceptance) for best results

Related Skills