First, open wechat Pay
1.1 Associated merchant Number
Wechat public account platform, functions, wechat Payment, merchant account management, associated merchant number
1.2 Adding a Merchant Number
Cloud development platform Settings Other Settings wechat Payment configuration Add merchant number
1.3 Administrator Authorization
1.2 On that picture, click Authorize. The administrator’s wechat will pop up with a prompt and click “confirm authorization”.
Sub_mch_id does not match sub_appID.
Second, cloud function development
2.1 Creating a Cloud Function
2.2 Cloud function code
\pay\index.js
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" : event.body,
"outTradeNo" : event.outTradeNo,
"spbillCreateIp" : "127.0.0.1"."subMchId" : "0000000000".// The key is the sub-merchant ID, but it is the normal merchant ID
"totalFee" : parseInt(event.totalFee), UnifiedOrder :fail WX API error: -202
"envId": "ooo-xxxxxxxxxxxxxxxx".// Here is the id of the cloud environment to which the callback function belongs
"functionName": "payCallBack".// This is the callback function name
"nonceStr":event.nonceStr, // There is no nonceStr and tradeType in the relevant cloud function code in the official documentation. When testing, nonceStr does not exist, the error will be reported
"tradeType":"JSAPI"
})
return res
}
Copy the code
2.3 Uploading and Deploying cloud Functions
Three, small program call
3.1 Unified Order
- The data obtained after the wechat order, it is best to save to the database, keep the bottom
- To adjust the wechat payment interface
// Call the cloud function, wechat unified order
cloudPay(){
var _this = this
this.setData({
body: "Bill Payment - XXXXX".outTradeNo: this.data.id+"-"+util.uuid(16),
totalFee: this.data.totalCost*100 // Unit of payment: cent
})
app.showLoading(true)
wx.cloud.callFunction({
name: "pay".data: {
body: _this.data.body,
outTradeNo: _this.data.outTradeNo,
totalFee: _this.data.totalFee,
nonceStr:util.uuid(32)// Call your own uuid function
},
success(res) {
// errCode: 0
// errMsg: "cloudPay.unifiedOrder:ok"
// returnCode: "FAIL"
// returnMsg: "total_fee is empty. "
console.log("Submission successful", res.result)
if(res.result.returnCode! ="SUCCESS"){
app.showToast(res.result.returnMsg)
return
}
_this.unifiedOrder(res.result)
// _this.requestPayment(res.result)
},
fail(res) {
console.log("Submission failed", res)
}
})
},
Copy the code
3.2 Setting up the wechat payment interface
// Official standard payment method, set up the payment interface
requestPayment(payData) {
var _this = this;
const payment = payData.payment// The result of the previous function directly combines the parameters to be used in this functionwx.requestPayment({ ... payment,/ /... Is the syntax for expanding variables
success(res) {
console.log("Payment successful", res)
_this.paySuccess()
},
fail(res) {
console.log("Payment failure", res)
}
})
},
Copy the code
3.2 Payment callback successfully
- It is not recommended to go directly to
requestPayment
thesuccess
In the callback function, perform the successful payment operation - It is best to build another cloud function
payCallBack
- The cloud function
payCallBack
Call the interface of our home-built server to execute the successful payment operation - This is how to call the HTTP interface in the cloud function, but I’m not sure. Why not
If you feel good, just click three times. (Like + Favorites + follow)