1. Open WeChat Pay1.1 Affiliate Merchant Number 1.2 Add merchant number 1.3 Administrator Authorization1.2 On that picture, click Authorize. A prompt will pop up on the administrator's WeChat, just click to confirm the authorization. Otherwise, when using it, an error message will appear indicating 2. Cloud Function Development2.1 Create a new cloud function2.2 Cloud Function Code 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", //Note here: although the key is the sub-merchant id, it is actually the normal merchant id "totalFee" : parseInt(event.totalFee), //Second pitfall: Note that it must be a number. If it is not a number, an error will be reported: unifiedOrder:fail wx api error: -202 "envId": "ooo-xxxxxxxxxxxxxxxx", //This is the cloud environment id to which the callback function belongs "functionName": "payCallBack", //This is the callback function name"nonceStr":event.nonceStr, //The third pitfall: The relevant cloud function code in the official document does not have nonceStr and tradeType. When testing, it will report an error that nonceStr does not exist. I looked through the document and found that this is a required item. After pasting it directly, you need to add these two parameters"tradeType":"JSAPI" }) return res } 2.3 Upload and deploy cloud functions3. Mini Program Call3.1 Unified ordering
//Call 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 //payment unit: cents}) 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) } }) }, 3.2 Open WeChat payment interface //Official standard payment method, call up the payment interface requestPayment(payData) { var _this = this; const payment = payData.payment //Note that the result of the previous function directly integrates the parameters to be used here. You can directly expand it and use wx.requestPayment({ ...payment, //...is the syntax for expanding variables success(res) { console.log("Payment successful", res) _this.paySuccess() }, fail(res) { console.log("Payment failed", res) } }) }, 3.2 Payment success callback
This concludes this article about how to use the WeChat payment function in a mini program through the mini program cloud. For more information about WeChat payment in a mini program, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to view and modify the time zone in MySQL
>>: Use the Linux seq command to generate a sequence of numbers (recommended)
<meta http-equiv="x-ua-compatible" co...
Introduction to border properties border property...
Table of contents 1. Initialization structure 2. ...
environment: MAC_OS 10.12 Python 3.6 mysql 5.7.25...
<frameset></frameset> is familiar to e...
First of all, we need to make it clear why we use...
Table of contents 1 What is array flattening? 2 A...
Related knowledge points Passing values from pa...
Table of contents Props comparison of class compo...
Overview This article is a script for automatical...
In practice, we often encounter a problem: how to...
Table of contents 1. Basic Concepts 2. Developmen...
The scroll bar position is retained when scrollin...
01. Command Overview md5sum - Calculate and verif...
Table of contents Preface The relationship betwee...