Airwallex logo
Airwallex logo

WeChat Mini Program - WeChat Pay

To enable WeChat Mini Program payment, additional offline review process is required, please contact your account manager for detailed information.

Accept WeChat Pay from your WeChat mini program. To complete the integration, you will need to interact with WeChat Mini Program API in your mini program.

Wechat - mini program

Step 1. Initialize a Payment Intent

Create a Payment Intent with the Create a Payment Intent API API.

POST /api/v1/pa/payment_intents/create

JSON
1{
2 "request_id": "ed11e38a-7234-11ea-aa94-7fd44ffd1b89",
3 "amount": 100,
4 "currency": "CNY",
5 "merchant_order_id": "85d7b0e0-7235-11ea-862e-9f6aa1adfca6"
6}
7

Step 2. Redirect to WeChat Pay to complete payment

2.1. Obtain the data to be used for calling the WeChat Pay mini program API

You need to get the open_id of the shopper before proceeding with the mini_program flow. open_id refers to the unique identifier of a WeChat user in a specific mini program. It is required when confirming with mini_program flow. To get the open_id, follow the instructions below:

  1. Invoke wx.login , then get a code from the response.

  2. Invoke jscode2session from server-side with the code obtained from the last step to get the open ID of the shopper.

Then, call the Confirm a Payment Intent API API with the open_id to get the data required when calling the WeChat Pay mini program API.

POST /api/v1/pa/payment_intents/{id}/confirm

JSON
1{
2 "request_id": "ed11e38a-7234-11ea-aa94-7fd44ffd1b89",
3 "payment_method": {
4 "type": "wechatpay",
5 "wechatpay": {
6 "flow": "mini_program",
7 "open_id": "oqZa55Qo-u0cVViWMAgv_YbyXb4U"
8 }
9 }
10 }

You will get a response similar to the following.

JSON
1{
2 // other fields omitted ...
3 "next_action": {
4 "type": "call_sdk",
5 "data": {
6 "appId": "wxce926ea78004260e",
7 "timeStamp": "1585294167",
8 "nonceStr": "Ut3ZbuIY90JFUYtYtaoA6Ic6to1TS9oI",
9 "package": "prepay_id=wx07175429162447bc24fd2df6d4303c0000",
10 "signType": "HMACSHA256",
11 "paySign": "D48E9B7B2CF14C1478E598872BCE52D509EE95B63D0933EAA419595560485589"
12 }
13 }
14}

2.2. Pass the data to WeChat Mini Program Payment API

Pass data in next_action you get in the previous step to WeChat Mini Program Payment API wx.requestPayment . The shopper will be redirected to the payment page to complete the payment.

Step 3. Obtain the payment result status

Once the order is successfully paid or failed, WeChat Pay will invoke the callback functions success and fail provided when calling wx.requestPayment respectively. We suggest you poll the status of the Payment Intent via the Retrieve a Payment Intent API API upon the invocation of the success callback function to get a reliable final status of the order.

GET /api/v1/pa/payment_intents/{id}

In addition, Airwallex will notify you of the payment result asynchronously via the webhooks. Please refer to the webhook documentation to set up your webhook accordingly. Although subscribing to webhook events is optional, it is recommended to subscribe to the payment_intent.succeeded webhook which indicates that the shopper has paid the order.

Was this page helpful?