With the development of WeChat applets, more and more mobile applications have chosen WeChat products as their medium. Whether it is official account development or small program development, WeChat payment is always an inevitable topic. Since WeChat Pay involves many scenarios, in this article we only learn how to access WeChat Pay in official accounts and mini programs.
1. Prerequisites for WeChat Pay
1.1 Official account
WeChat public accounts can be roughly divided into service . The specific differences between subscription accounts and service accounts are described in an article written in the previous article 16076b6213a6ed Spring Boot development WeChat public , so I won’t repeat them here. In other words, the service account provides more advanced functions.
Micro-channel pay access needs service number has been completed micro letter certified . If it is a small program, you also need to complete WeChat authentication .
The official account can be associated with 10 mini programs of the same subject, and 3 mini programs of different subjects. If it is a mini program of the same subject as the has been authenticated, it can be directly managed in the 16076b6213a755 mini program management in the background of the official account , Perform quick registration and authentication, so that you don’t need to pay the
300
RMB required for WeChat authentication again.
1.2 WeChat Merchant Platform
After the WeChat authentication is completed, the WeChat payment function WeChat payment in the backstage of the official account. After submitting the WeChat payment application, it will be reviewed within 3-5 working days. After the review is passed, an email containing the merchant account information will be sent to the mailbox you filled in, and at the same time, it will be sent to the public account you filled in. For the remittance of dime, you need to check the specific amount and verify it in the merchant platform.
Merchants are divided into ordinary merchants and service merchants. Don't make a mistake in applying.
Ordinary merchants can conduct transactions, but cannot expand merchants.
Service providers can expand merchants, but cannot trade.
The service provider is to provide a unified payment portal. It needs to be bound to a specific ordinary merchant. When WeChat pays, it will carry the ordinary merchant parameters in the payment interface. After the payment is successful, the amount will be directly transferred to the specific ordinary merchant account.
When applying, you can directly apply for ordinary merchant .
1.3 Bind Merchant
The initiation of WeChat Pay relies on the binding relationship between official accounts, mini programs and other applications and merchant accounts. Therefore, it is necessary to bind merchants with specific applications before proceeding with development.
If the merchant and the AppID to be bound are the same subject, only the following steps are required to complete the binding.
- Link AppID in merchant platform-product center-AppID account management, enter AppId to apply for binding
- Confirm in the WeChat Pay-Merchant Account Management in the official account or Mini Program background.
If the merchant and the AppID that needs to be bound are different subjects, the steps are the same as the above. In addition to entering the AppId, you also need to fill in the AppId authentication information.
2. WeChat payment related configuration
2.1 Types of payment products
1. Payment code payment
The user opens the WeChat wallet-payment code interface, and the merchant scans the code and submits the payment to complete.
2. JSAPI payment
The user enters the merchant H5 page by scanning the code through WeChat, following the official account, etc., and calls JSSDK in WeChat to complete the payment.
3. Native payment
The user opens WeChat, scans, scans the QR code of the merchant, and completes the payment.
4. APP payment
The WeChat SDK is integrated in the merchant APP, and the user clicks to jump to WeChat to complete the payment.
5. H5 payment
The scene where a user requests WeChat payment in a mobile browser other than WeChat evokes WeChat payment.
6. Mini Program Payment
A scenario where a user uses WeChat payment in a WeChat applet.
7. Face payment
No need to take out your mobile phone and swipe your face to complete the payment, suitable for various offline scenarios.
Apply for payment products in merchant platform-product center-my products.
2.2 Payment authorization directory configuration
Configure the payment authorization directory in the merchant platform-product center-development configuration (that is, the order interface address you developed). It should be noted that the authorization directory can be configured with a maximum of five . Please define the payment interface reasonably during the development process. .
2.3 Configure Merchant Key
Set the API key in the merchant platform-account center-API security.
When setting up for the first time, you need to install the operating certificate, fool-proof installation, just follow the prompts step by step.
API key is required when a 32 Wei random string, remember not arbitrarily change the API key .
In the WeChat API v3 version, in addition to configuring the API key, you also need to configure the APIv3 key and apply for the API certificate issued by the CA.
- The API v3 key is mainly used for platform certificate decryption and callback information decryption.
- API certificates are used to call higher-level api interfaces, including interfaces such as refunds and red envelopes.
If you use the open source WeChat development kit, please find out if the v3 version is supported.
2.4 Configure the server
Enable and fill in the server information in the official account background-development-basic configuration-server configuration.
2.5 Whitelist configuration
Configure the developer key in the official account background-development-basic configuration-official account development information, and fill in the IP whitelist at the same time.
2.6 JS interface secure domain name
Set the JS interface security domain name in the official account background-official account settings-function settings.
The above configuration is based on the official account payment configuration, the applet payment is not so troublesome, the applet payment does not need to configure the payment authorization directory and authorized domain name.
JSAPI | Applets | |
---|---|---|
Payment agreement | HTTP/HTTPS | HTTPS |
Payment directory | Have | no |
Authorized domain name | Have | no |
3. WeChat payment process
Since WeChat has upgraded the API interface, in the API v3 interface, the requested API certificate needs to be loaded. WeChat has encapsulated the relevant jar package and provided loading examples. For details, please refer to " https://pay.weixin.qq.com /wiki/doc/apiv3/open/pay/chapter2_3.shtml ", I won’t repeat it here. Let's take API v2 as an example to learn in detail the main process of WeChat access (because some API v3 interfaces are still being upgraded, and the v2 interface is relatively complete).
The picture above is from the WeChat development document. Let's analyze the payment process in detail.
3.1 WeChat order interface
The user initiates a payment through the WeChat client, generates an order in the merchant's background, and then calls the WeChat order interface to generate a pre-paid order and return the order number!
The main parameters involved in the order interface, only a few important parameters are listed:
Request parameter | Must pass | Types of | description |
---|---|---|---|
appid | Yes | String | Public account appid |
mch_id | Yes | String | business number |
nonce_str | Yes | String | Random string, within 32 bits |
sign | Yes | String | Signature, MD5 is used for encryption by default |
out_trade_no | Yes | String | System internal order number |
total_fee | Yes | Int | The total amount of the order, in cents |
notify_url | Yes | String | Payment result notification interface |
sign
is also more general, involving a guarantee that the signature is unpredictable nonce_str
- Use dictionary sorting to generate key-value pairs for all sent non-empty parameters (key1=value1&key2=value2);
- Splice the merchant platform key at the end of the above string ("String"+&key=key);
- Use MD5 encryption for the above string
3.2 Payment
Pull up WeChat payment, enter the password, and complete the payment. This step needs to execute JS transfer payment in the H5 webpage.
The following parameters are required. Therefore, after the prepaid order is returned, the following parameters need to be encapsulated and responded to the page, and the page will complete the payment.
parameter name | Must pass | Types of | description |
---|---|---|---|
appId | Yes | String | Official account id |
timeStamp | Yes | String | Current timestamp |
nonceStr | Yes | String | Random string |
package | Yes | String | Prepaid order, the format is prepay_id= * |
signType | Yes | String | Signature type, default MD5 |
paySign | Yes | String | signature |
The signature method is the same as that of the order interface.
The JS pseudo code is as follows:
function onBridgeReady(){
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
// 公众号ID,由商户传入
"appId":"wx2421b1c4370ec43b",
// 时间戳,自1970年以来的秒数
"timeStamp":"1395712654",
// 随机串
"nonceStr":"e61463f8efa94090b1f366cccfbbb444",
"package":"prepay_id=u802345jgfjsdfgsdg888",
// 微信签名方式
"signType":"MD5",
// 微信签名
"paySign":"70EA570631E4BB79628FBCA90534C63FF7FADD89"
},
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ){
// 使用以上方式判断前端返回,微信团队郑重提示:
// res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
}
});
}
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
}else{
onBridgeReady();
}
Note that the sentence // res.err_msg in the pseudo code will return ok after the user has successfully paid, but it does not guarantee that it is absolutely reliable. Why do you say that, I should understand it by giving an example. If you go to the supermarket to buy things, do you say that you can take the things with you after the payment is successful? Definitely not. It is when the merchant receives the money that your payment is considered successful and you can take the things away. In other words, the success indicated here does not necessarily mean that the payment is successful. Specifically, the WeChat platform will notify you in an asynchronous manner.
3.3 Asynchronous notification
Asynchronous notification is an important step, where you can process your business logic based on the notification result. However, it may not be notified due to network fluctuations or other reasons, or that the response received by WeChat does not meet the requirements of the API, WeChat will continue to initiate multiple notifications (please handle it in the callback notification interface properly, avoid duplication of notifications and business duplication processing ), until the success of the notification frequency 15s
/ 15s
/ 30s
/ 3m
/ 10m
/ 20m
/ 30m
/ 30m
/ 30m
/ 60m
/ 3h
/ 3h
/ 3h
/ 6h
/ 6h
- Total 24h4m
). However, WeChat does not guarantee that the notification will be successful in the end.
The asynchronous notification response parameter as follows:
parameter name | Must pass | Types of | description |
---|---|---|---|
return_code | Yes | String | Return status code, SUCCESS / FAIL |
return_msg | no | String | returned messages |
What if WeChat keeps notifying me? In the example just now, you obviously paid successfully, but the merchant kept saying that she didn't receive the money. What should you do at this time? It must be to see if her phone really didn't receive the money! Same here.
3.4 Payment status query
- When the merchant APP or front-end page receives the payment return, the merchant needs to call the merchant's order check interface to confirm the order status and display the query result to the user.
- Merchant backends need to accurately and efficiently process the asynchronous payment result notification sent by WeChat Pay, and return the processing result to WeChat Pay according to the interface specification.
- When the merchant's backend does not receive the notification of the asynchronous payment result, the merchant should actively call the
WeChat payment inquiry interface to synchronize the order status.
- The merchant obtains the transaction bill on the T day from the WeChat payment side on the T+1 day, and checks it with the order in the merchant system. If the order is successful on the WeChat payment side, but not on the merchant side, the merchant needs to make up the shipment or refund to the user.
Four, summary
This article mainly takes official account payment as an example to summarize the relevant configuration and payment process required to access WeChat payment. Other payments like APP payment are also common application scenarios in development. APP payment needs WeChat open platform to access WeChat payment. In addition, the WeChat payment API is smoothly upgraded to v3, and some interfaces have not been upgraded yet. Compared with v2, the upgraded interface has undergone some data format changes. If you use a third-party development kit for development, you need to pay attention to the version corresponding to the interface.
reference
WeChat Pay Development Document
Pay attention, don't get lost
If you think the article is good, welcome to pay attention, like, and collect. Your support is the driving force for my creation. Thank you all.
If there is a problem with the writing of the article, please don't hesitate to write, welcome to leave a message and point out, I will check and revise it in time.
If you want to see more things, you can search for "Java Journey" on WeChat to follow. Reply to the "Manual" to receive the Java Interview Manual!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。