This article mainly talks about the pitfalls encountered in the development process of connecting WeChat applet to WeChat payment. It is divided into two parts, the applet end and the backend interface encapsulation.
1. Backend interface encapsulation
This article introduces interface encapsulation based on ThinkPHP5. The specific steps are as follows:
1. The official WeChat payment document provides PHP Download the sample of script WeChat payment;
2. The sample has encapsulated each class, we only need to add the namespace, create a new directory wxpay under the extend directory of TP5, and put Copy the classes in the sample to this directory, and then add the namespace;
The most important class is WxPayApi.php, after adding the namespace The form is as follows, and the transformation of other classes is similar:
3. Introduce the namespace where it needs to be used, and a new object can be used. For example, the unified order interface:
First introduce the required classes:
Encapsulate the unified external ordering interface and directly return the parameters required by the mini program to call the payment interface:
The repayment interface is similar and can be encapsulated through a unified order class.
2. Mini program integration
The mini program is very simple. You only need to pass the data returned by the interface to the js interface of WeChat payment
The examples returned by the interface are as follows:
The examples of data returned by the integrated interface are as follows:
3. Summary
1. The WeChat payment API of the official account jssdk used by the mini program WeChat payment API has a wrong parameter in the official document, that is, the "I" in the passed appid must be Capitalize, otherwise it will always prompt parameter error.
2. After calling the unified ordering interface, the results returned by the unified ordering cannot be directly returned to the mini program, and the signature needs to be regenerated.
Recommendation: " Mini Program Development Tutorial"
The above is the detailed content of How to connect the mini program to WeChat payment? (Introduction to development tutorial). For more information, please follow other related articles on the PHP Chinese website!