跳到主内容

使用微信支付收款

本指南介绍如何让用户通过微信支付为您的 Casdoor 产品付款。Casdoor 支持带二维码的 Native 支付 以及微信应用内的 JSAPI 支付。


学习目标​

  • 从微信支付获取 API 密钥、证书、ID 和微信支付公钥。
  • 添加微信支付作为支付提供商,并将其添加到产品中。
  • 启用微信应用内支付。

所需条件​


获取凭证​

  1. 在微信支付商户平台,前往 Account Settings > API Security > Set APIv3 Secret 并复制 APIv3 密钥。请参阅 APIv3 密钥设置。

    API key v3 setting

  2. 前往 Account Settings > API Security > API Certificate 并下载商户 API 证书。下载内容包含证书 apiclient_cert.pem 及其私钥 apiclient_key.pem。

    Merchant certificate download

  3. 在 Casdoor 的 Certs 页面添加一个证书。将 apiclient_cert.pem 的内容粘贴到 Certificate 中,将 apiclient_key.pem 的内容粘贴到 Private key 中。Casdoor 会从证书中读取序列号。你也可以直接将序列号放入 Certificate。

    Merchant certificate in Casdoor

  4. 记录你的商户 ID和 App ID。

  5. 了解微信支付如何为你的商户账户签署响应。前往 Account Settings > API Security:

    • 如果页面中有 WeChat Pay public key(微信支付公钥)部分,则你的账户使用公钥模式。新的商户账户使用此模式。下载公钥文件 pub_key.pem 并复制以 PUB_KEY_ID_ 开头的公钥 ID。
    • 如果页面中有 Platform certificate(平台证书)部分且没有公钥,则你的账户使用平台证书模式。你不需要其他任何东西:Casdoor 会使用你的商户证书下载平台证书。

在 Casdoor 中添加提供商​

  1. 在 Casdoor 管理控制台中,前往 Identity > Providers 并添加一个提供商。

  2. 将 Category 设置为 Payment,将 Type 设置为 WeChat Pay。

  3. 填写以下字段:

    字段Public key modePlatform certificate mode
    Client ID商户IDMerchant ID
    Client secretAPI密钥v3API Key v3
    App ID应用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

    当同时设置了 Public key ID 和 Public key 时,Casdoor 会使用此公钥验证微信支付的响应。否则它会下载平台证书,这对于处于公钥模式的账户会失败并报错 RESOURCE_NOT_EXISTS。

    WeChat Pay provider in Casdoor

  4. 保存提供商.

将提供商添加到产品​

将该提供商添加到产品的 Payment providers 中并保存。

WeChat Pay in the payment providers of a product

在微信应用内支付​

对于 JSAPI 支付,用户在微信应用的内置浏览器中使用微信登录,然后在那里支付。为与支付提供商相同的公众号配置一个 WeChat OAuth 提供商,并将两者都添加到应用中。

Relation between the WeChat Pay provider and the WeChat OAuth provider

故障排除​

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 Authorization较旧版本的 Casdoor 会将 Certificate 读取为序列号,导致 PEM 格式破坏请求头。升级 Casdoor,或将商户证书的序列号放入 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.

另请参阅​