Skip to main content

Take payments with WeChat Pay

This guide explains how to let users pay for your Casdoor products with WeChat Pay. Casdoor supports Native payment, with a QR code, and JSAPI payment, inside the WeChat app.


Learning outcomes​

  • Get the API key, the certificate, the IDs, and the WeChat Pay public key from WeChat Pay.
  • Add WeChat Pay as a payment provider and add it to a product.
  • Turn on payment inside the WeChat app.

What you need​


Get the credentials​

  1. On the WeChat Pay merchant platform, go to Account Settings > API Security > Set APIv3 Secret and copy the API key v3. See APIv3 key settings.

    API key v3 setting

  2. Go to Account Settings > API Security > API Certificate and download the merchant API certificate. The download contains the certificate apiclient_cert.pem and its private key apiclient_key.pem.

    Merchant certificate download

  3. On the Certs page of Casdoor, add a cert. Paste the content of apiclient_cert.pem into Certificate and the content of apiclient_key.pem into Private key. Casdoor reads the serial number from the certificate. You can also put the serial number itself into Certificate.

    Merchant certificate in Casdoor

  4. Note your merchant ID and your App ID.

  5. Find out how WeChat Pay signs its responses for your merchant account. Go to Account Settings > API Security:

    • If the page has a WeChat Pay public key (微信支付公钥) section, your account uses the public key mode. New merchant accounts use this mode. Download the public key file pub_key.pem and copy the public key ID, which starts with PUB_KEY_ID_.
    • If the page has a Platform certificate (平台证书) section and no public key, your account uses the platform certificate mode. You don't need anything else: Casdoor downloads the platform certificates with your merchant certificate.

Add the provider in Casdoor​

  1. In the Casdoor admin console, go to Identity > Providers and add a provider.

  2. Set Category to Payment and Type to WeChat Pay.

  3. Fill in the fields:

    Casdoor fieldPublic key modePlatform certificate mode
    Client IDMerchant IDMerchant ID
    Client secretAPI Key v3API Key v3
    App IDApp IDApp ID
    Public key IDThe ID that starts with PUB_KEY_ID_Empty
    CertThe cert aboveThe cert above
    Public keyThe content of pub_key.pemEmpty

    When both Public key ID and Public key are set, Casdoor verifies the responses of WeChat Pay with this public key. Otherwise it downloads the platform certificates, which fails with RESOURCE_NOT_EXISTS for an account in the public key mode.

    WeChat Pay provider in Casdoor

  4. Save the provider.

Add the provider to a product​

Add the provider to the Payment providers of the product and save it.

WeChat Pay in the payment providers of a product

Pay inside the WeChat app​

For JSAPI payment, users sign in with WeChat in the built-in browser of the WeChat app and then pay there. Configure a WeChat OAuth provider for the same official account as the payment provider, and add both to the application.

Relation between the WeChat Pay provider and the WeChat OAuth provider

Troubleshooting​

ErrorCause and fix
RESOURCE_NOT_EXISTS when payingThe merchant account uses the public key mode and has no platform certificate. Fill in Public key ID and Public key.
invalid header field value for AuthorizationAn older Casdoor version reads Certificate as the serial number, so the PEM breaks the request header. Upgrade Casdoor, or put the serial number of the merchant certificate into Certificate.
SIGN_ERRORThe private key, the serial number, or the merchant ID don't belong together. Check that the cert holds the certificate and private key of this merchant.

See also​