Traditional development payment needs to do a lot of work at the front and back end, processing of various parameters, signature, secret key verification, etc., especially when it involves a variety of different platform payment, it is tedious; And before payment is dominated by the backend, if the backend is a chicken dish, then do the payment can be pretty tiring, so in order to avoid this kind of barrier, the front only need to pay, a person can get uniapp cloud development provides a way, a set of code compatible with different payment, small programs, App, H5 and other articles, the most main is free.

Here’s how simple this brainless approach to payment can be, but only if you use a framework that can create uniCloud cloud space, otherwise it might not work!

Step 1: The project creates the cloud space;

Select the project in Hbuilderx, right-click to create cloud development environment, currently only Tencent cloud and Ali Cloud, Ali cloud is free, if it is only to pay, Ali cloud is enough; If the project already has a cloud space, you do not need to create it. The cloundFunctions folder contains the common directory and the database directory. Common is the directory where the common modules are stored, and database is the database directory.

Step 2: Introduce the Uni-Pay plugin:

The uni-pay module will be introduced in the common directory. By default, there are index.js and package.json files. Then upload the common module to the service space;

Step 3: Create and write cloud functions:

To create a cloudfunction, right-click uniCloud/ CloudFunctions and create a cloudfunction. By default, the index.js file is generated, which is the entry file of the cloudfunction. Note: Cloud function created with the same name will override;

If the cloud function depends on the functions of a common module, install the dependencies of the functions of the common module. If uni-pay is introduced via uni_modules, right-click “Manage Public module dependencies” in the cloud function directory and select the required public module. After the dependency installation, introduce uni-pay in index.js; If uni-pay is installed via non-uni_modules, you need to generate a package.json file for the function via NPM init -y, and then run NPM install ‘cloud function module path to be imported’ in the terminal of the cloud function.

Write cloud function, take App wechat Pay as an example:

First introduce uni-pay; const unipay = require('unipay'); Const unipayIns = unipay.initweixin ({appId: 'your appId',// appId: 'your McHId ',// appId: 'your McHId ',// 'you parterner key',// pay secret key PFX: Fs. readFileSync('/path/to/your/pfxfile'), // p12 file path.Copy the code

Payment parameters, take app wechat pay as an example

Exports. main = async function (event,context) {let orderInfo = await unipayins. getOrderInfo({body: 'Item Description ', outTradeNo:' Merchant Order Number ', totalFee: 1, // Amount, unit: tradeType:'APP' notifyUrl: 'https://xxx.xx' // Address for payment result notification}) return {orderInfo}}Copy the code

Step 4: The client calls the cloud function

uniCloud.callFunction({ name: 'getOrderInfo', // this name is the name of the cloud function to be called data:{},// the parameters required for payment, Success (res) {uni. RequestPayment ({// #ifdef app-plus provider: 'wxpay carry this parameter required / / App, can through the uni. GetProvider get / / # endif / / # ifdef App - PLUS | | MP - ALIPAY orderInfo: res.result.orderInfo, // #endif ... Res.result.orderinfo Success (){return payment result}, fail(){}})}})Copy the code

At this point, the payment process is finished and the payment result is returned. If you need to query the order, you need to call the unipayins. orderQuery function to query.

Tip1:

If the project needs two payment methods of wechat and Alipay, or even the payment of small programs, different types of payment methods can be encapsulated into modules, and then initialized in each cloud function; The method is to create a public module under the common of the cloud function and name it config. In the index.js of the file, configure the parameters corresponding to different payment methods, install the dependency through the cloud function to be called, and introduce it in the same way as the introduction of uni-pay. It is important to note that when the client calls this function, the different payment methods need to be identified with the parameters, so that the different parameters can be processed in the called cloud function.

Tip2:

The unit of the amount passed in is cents. If the business is given yuan, then 100 yuan should be converted into cents. Pay attention to some special numbers, such as 1.1.

Tip3: 

Order number should pay attention to, because the order number can not be repeated, so in the actual payment may appear the user cancelled the payment, and pay again, so the more appropriate way is to give the order number by the business, the front end of the processing, such as adding a few random numbers, so as to ensure that it will not be repeated;

Tip4:

Cloud development of payment almost entirely lost the backend to participate in, but if your project is not finished all the cloud of development, the business itself also need backend to participate in, pay the complete results still need to pay the state of back-end modified, so the good will be agreed with the back-end, payment notice address receive the platform’s notice, will go to modify the payment status of the business, Avoid a situation where the payment has been completed but the status of the payment has not changed; If, your back end is a big chicken, you are afraid of his SAO operation may not receive the platform server payment results, so you can continue to call the query order cloud function, the payment results through the interface back to the back end, so he has a double guarantee to modify the state of the business!