Using Cloud Functions + Cloud Call to Integrate WeChat Pay
Overview
The cloud call function of Mini Program Cloud Development natively integrates with the WeChat Pay interface. Developers only need to call the corresponding function to easily complete the entire payment process.
- No need to worry about certificates and signatures; the payment process is simplified.
- Based on WeChat's private protocol and private link, it is more secure and efficient;
- No operations required, high availability;
- On-demand scaling, elastic scaling, pay-as-you-go billing, cost reduction;
- Supports receiving payment callbacks via cloud functions without the need to self-host callback services.
Process Comparison: Traditional Process vs Cloud Development
Code Sample
Step 1: Invoke Cloud Function in Mini Program
After the C-end user initiates the payment process, the mini program side calls a cloud function (we assume the cloud function is named makeOrder
):
// Mini Program code
wx.cloud.callFunction({
name: "makeOrder",
data: {
/* Developer-defined parameters */
},
});
Step 2: The cloud function generates an order and returns the order information.
After receiving the call, the cloud function makeOrder
uses the API provided by the WeChat server-side SDK and without the need for certificates or signatures, can directly generate an order.
After generating the order, the order information is returned to the mini program.
cloudPay.unifiedOrder()
API documentation
// cloud function makeOrder
const cloud = require("wx-server-sdk");
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
});
exports.main = async (event, context) => {
const res = await cloud.cloudPay.unifiedOrder({
body: "Xiaoqiu TIT Store - Supermarket",
outTradeNo: "1217752501201407033233368018",
spbillCreateIp: "127.0.0.1",
subMchId: "1900009231",
totalFee: 1,
envId: "test-f0b102",
functionName: "payCallback", // Function name for payment callback
});
return res;
};
Step 3: The mini program side initiates payment
After the mini program side receives the order information returned by the cloud function, it initiates payment:
// Mini Program code
wx.cloud.callFunction({
name: "makeOrder",
data: {
/* Developer-defined parameters */
},
success: (res) => {
// Get the order information returned by the cloud function
const payment = res.result.payment;
// Invoke WeChat Pay in the client
wx.requestPayment({
...payment,
success(res) {
/* Success callback */
},
fail(res) {
/* Failure callback */
},
});
},
});
Step 4: Use cloud functions to receive payment callbacks and complete the payment process
After the user completes the payment, the WeChat backend will invoke the specified cloud function (assumed to be named payCallback
), passing order information in the parameters.
Developers can implement their own shipping and order fulfillment logic in this cloud function.
// Cloud function payCallback
exports.main = async (event, context) => {
const {
return_code, // Status code
appid, // Mini Program AppID
mch_id, // WeChat Pay merchant ID
device_info, // Device number assigned by WeChat Pay
openid, // Unique identifier of the user under the merchant appid
trade_type, // Transaction type: JSAPI, NATIVE, APP
bank_type, // Bank type
// ......
// For more parameters, refer to: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7&index=8
} = event;
/*
developers' own logic
*/
// Return success to the WeChat backend; otherwise, it will repeatedly call this function
return { errcode: 0 };
};