Let’s take PHP language as an example to explain the development process of WeChat payment.
1. Obtain order information
2. Generate signs based on order information and payment related accounts, and generate payment parameters
3. POST the payment parameter information to the WeChat server to obtain the return information
4. Generate the corresponding payment code (inside WeChat) or payment QR code (not in WeChat) based on the returned information to complete the payment.
Let’s talk about it in steps below:
1. There are three necessary order parameters related to WeChat payment, namely: body (product name or order description), out_trade_no (usually the order number) and total_fee (order amount, unit "score", pay attention to unit issues). In different applications, the first thing to do is to obtain relevant information in the order and prepare for the generation of payment parameters.
2. Other necessary payment parameters include appid (WeChat appid), mch_id (notify after the application is successful), device_info (the parameters are both unified on the web and WeChat, and are capitalized "WEB"), trade_type (the value is also different depending on the usage scenario. The external value is "NATIVE" on WeChat, and "JSAPI" inside WeChat), nonce_str (32-bit random string), spbill_create_ip (the terminal IP that initiates the payment, that is, the server IP), notify_url (payment callback address, WeChat server notifies the website whether the payment is completed or not, and the order status is modified), sign (signature), and there is another point that needs to be explained. If trade_type is JSAPI, openid is a required parameter.
The signature algorithm is a relatively easy-to-make mistake, because the signing steps are complicated. In fact, it is very important that sign does not participate in the signature.
A: Assign the parameters except sign mentioned in 1 and 2, put them into an array array, and sort them in dictionary order. In fact, the key values are sorted in order of A-Z.
B: Convert the array into a string string, format k1=v1&k2=v2&...kN=vN
C: Add the KEY value after this string (set by the user in the background of the WeChat payment merchant) Now string = k1=v1&k2=v2&...kN=vN&key=KEY.
D:string = md5(string)
E: sign = strtoupper(string)
At this point, the sign is generated.
Add sign to the array to generate a new array. Convert the array to XML. At this point, the parameters preparation work for WeChat payment has been completed.
3. Send the XML generated in 2 to WeChat using POST (/pay/unifiedorder), obtain the returned XML information and convert the information into an array format for easy operation. The returned XML information is as follows:
<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> <appid><![CDATA[wx2421b1c4370ec43b]]></appid> <mch_id><![CDATA[10000100]]></mch_id> <nonce_str><![CDATA[IITRi8Iabbblz1Jc]]></nonce_str> <sign><![CDATA[7921E432F65EB8ED0CE9755F0E86D72F]]></sign> <result_code><![CDATA[SUCCESS]]></result_code> <prepay_id><![CDATA[wx201411101639507cbf6ffd8b0779950874]]></prepay_id> <trade_type><![CDATA[JSAPI]]></trade_type> </xml>
If trade_type==native payment, there will be an additional parameter code_url, which is the address of the payment by scanning the WeChat code.
4. The following is the payment process.
If trade_type==native, then use some methods to convert code_url into QR code and scan the code with WeChat. If it is a WeChat internal click to pay, you need to call related things in WeChat js-sdk. The most important thing in this step is to generate a string in json format.
First, we need to generate an array array_jsapi that converts json strings.
A: The parameters of this array include: appId, timeStamp, nonceStr, package, signType (default is "MD5"). Note that the case is different from the above array.
B: Use this array to generate paySign parameters, and the signature method is the same as above.
C: Append the paySign parameter to the array_jsapi array.
D: Format the array as string js_string using json_encode.
After completing the above work, you can make payments within WeChat.
The following is the sample code for related payments:
<script type='text/javascript'> function jsApiCall() { ( 'getBrandWCPayRequest', $js_string, function(res){ (res.err_msg); if(res.err_msg=='get_brand_wcpay_request:ok') { alert('Payment Successfully'); } else { alert('Pay failed'); } } ); } function callpay() { if (typeof WeixinJSBridge == 'undefined'){ if( ){ ('WeixinJSBridgeReady', jsApiCall, false); }else if (){ ('WeixinJSBridgeReady', jsApiCall); ('onWeixinJSBridgeReady', jsApiCall); } }else{ jsApiCall(); } } </script>
In the code, js_string is the string we generated.
The callpay() function is called in the HTML code to initiate payment.
In this way, the payment work of WeChat payment will be completed.
The following is the callback work. This function ensures that after the order is paid successfully, the correct status will be displayed to the user.
After the payment is completed, WeChat uses a POST request to feed the payment result to the website server. The website server obtains POST information and determines whether to modify the order information based on whether the payment is successful or not.
A: Remove the sign in the POST parameter and record the value.
B: Sign the remaining parameters
C: Compare the signature result with the sign in POST. The same means that the signature is correct and modify the order status according to the payment result.
E: Return XML information to WeChat to make sure WeChat knows that the website has received the notification and avoid WeChat pushing POST again. The example is as follows:
<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> </xml>
If it fails, return
<xml> <return_code><![CDATA[FAIL]]></return_code> <return_msg><![CDATA[Reason for failure]]></return_msg> </xml>
At this point, the entire development introduction of WeChat Pay has been completed.