1. Preparations before WeChat payment access (these actually have nothing to do with our development, just ask the company to get them)
The merchant already has a WeChat mini-program, and when the user opens the mini-program in WeChat by sharing with friends or scanning the QR code, they can call WeChat payment to complete the process of placing an order.
Note: Mini Programs cannot make jsapi payment by pulling up the H5 page, and only Mini Programs can be used to pay within the Mini Program
A--Apply for APPID
Since the product system of WeChat Pay is all carried on the social system of WeChat, before direct-connected merchants or service providers access WeChat Pay, they need to have a WeChat social carrier, and the corresponding ID of the carrier is APPID.
If applying for a small program as a social carrier, please go to the small program platform to apply
If the merchant already has its own APP and wants to connect the APP to WeChat Pay, please go to the open platform to apply
B--Apply for mchid
The operation of applying for mchid and APPID does not affect each other and can be operated in parallel. The application address is as follows: merchant account application platform
After the application is successful, a notification email will be sent to the contact email address filled by the service provider. The content includes the successful application mchid and its login account password, please keep it properly.
Note: One mchid can only correspond to one settlement currency. If you need to use multiple currencies to receive payments, you need to apply for the corresponding number of mchids.
C--Configure API key
The API v3 key is mainly used for platform certificate decryption and callback information decryption. For specific usage methods, see the chapter on certificate and callback message decryption in the interface rules document .
Please follow the steps below to configure the API key:
1 Log in to the WeChat Merchant Platform , enter the [Account Center > API Security] directory, and set the APIV3 key.
2 Click "Communicate" in the pop-up window.
3. Enter the API key, the content is 32 characters, including numbers and uppercase and lowercase letters. Click to get SMS verification code
4. Enter the SMS verification code and click "Confirm" to complete the setup.
When you are roughly ready, you will get such folder data
The following are the preparations before accessing the official documents
https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_8_1.shtml
2. Order via JSAPI
The merchant system first calls this interface to generate a prepayment transaction order in the WeChat payment service background, returns the correct prepayment transaction session ID, and then generates transaction strings according to different scenarios such as Native, JSAPI, and APP to initiate payment.
First send the http request to the backend to get the prepay_id
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi");
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-type","application/json; charset=utf-8");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode rootNode = objectMapper.createObjectNode();
rootNode.put("mchid","1900009191")
.put("appid", "wxd678efh567hg6787")
.put("description", "Image形象店-深圳腾大-QQ公仔")
.put("notify_url", "https://www.weixin.qq.com/wxpay/pay.php")
.put("out_trade_no", "1217752501201407033233368018");
rootNode.putObject("amount")
.put("total", 1);
rootNode.putObject("payer")
.put("openid", "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o");
objectMapper.writeValue(bos, rootNode);
httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
CloseableHttpResponse response = httpClient.execute(httpPost);
String bodyAsString = EntityUtils.toString(response.getEntity());
System.out.println(bodyAsString);
3. The mini program invokes the payment
A- The prepay_id obtained from the backend is the package
B- You need to encapsulate the following data and return it to the front end
C- but here paysign needs to continue signing returns
This paysign is actually encrypting appid + timeStamp + nonceStr + package with your secret key
Note: Because it is the payment of the applet, the first three data are directly filled in, and the last one is prepay_id=data value
And your secret key is the following
open this file
-----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY----- is your private key
Note: Keep the private key by yourself, and it cannot be leaked
Here message splices the original data for you, encrypts it with sign, and gets the required paySign
String sign(byte【】 message) {
Signature sign = Signature.getInstance("SHA256withRSA");
sign.initSign(yourPrivateKey);
sign.update(message);
return Base64.getEncoder().encodeToString(sign.sign());
}
Note: Here you need to first convert the message into a getbyte array for encryption
Encapsulate these data and send them to the front end for invoking payment
request example
wx.requestPayment
(
{
"timeStamp": "1414561699",
"nonceStr": "5K8264ILTKCH16CQ2502SI8ZNMTM67VS",
"package": "prepay_id=wx201410272009395522657a690389285100",
"signType": "RSA",
"paySign": "oR9d8PuhnIc+YZ8cBHFCwfgpaK9gd7vaRvkYD7rthRAZ\/X+QBhcCYL21N7cHCTUxbQ+EAt6Uy+lwSN22f5YZvI45MLko8Pfso0jm46v5hqcVwrk6uddkGuT+Cdvu4WBqDzaDjnNa5UK3GfE1Wfl2gHxIIY5lLdUgWFts17D4WuolLLkiFZV+JSHMvH7eaLdT9N5GBovBwu5yYKUR7skR8Fu+LozcSqQixnlEZUfyE55feLOQTUYzLmR9pNtPbPsu6WVhbNHMS3Ss2+AehHvz+n64GDmXxbX++IOBvm2olHu3PsOUGRwhudhVf7UcGcunXt8cqNjKNqZLhLw4jq\/xDg==",
"success":function(res){},
"fail":function(res){},
"complete":function(res){}
}
)
Official Documents - Mini Program Call Payment
https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_4.shtml
4. Payment notification callback
WeChat payment notifies the merchant of the successful payment of the user through the payment notification interface
The link is set through the request parameter "notify_url" in the basic order interface, which must be an https address. Please ensure that the callback URL is externally accessible and cannot carry suffix parameters, otherwise the merchant may not be able to receive the callback notification information from WeChat. Example callback URL: "https://pay.weixin.qq.com/wxpay/pay.action"
A - data for callback
The callback data is given to you by WeChat, so we can use IO6 to get the data carried by the caller
BufferedReader reader = request.getReader();
//在准备一个空的字符串
String str =null;
//在准备一个builder
StringBuilder stringBuilder = new StringBuilder();
//这里就是进行判断,如果获取到的东西有值,就把东西给放到这个BUILER里面去
while ((str = reader.readLine()) !=null ){
stringBuilder.append(str);
}
在进行转换在json对象
Here it is converted to a json object, which can be converted using FastJson. It is recommended that the callback data can be packaged into an object for reception in advance to the backend
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
Example of successful payment result notification
{
"id": "EV-2018022511223320873",
"create_time": "2015-05-20T13:29:35+08:00",
"resource_type": "encrypt-resource",
"event_type": "TRANSACTION.SUCCESS",
"summary": "支付成功",
"resource": {
"original_type": "transaction",
"algorithm": "AEAD_AES_256_GCM",
"ciphertext": "",
"associated_data": "",
"nonce": ""
}
}
Opinions are welcome, a siege lion who loves to learn and share