Following the PHP language as an example, the development process of micro-credit payment is explained.
1. Get Order Information
2. Generate sign based on order information and payment account, and generate payment parameters
3. Post the payment parameter information to the micro-trust server to obtain the return information
4. Generate the corresponding payment code (within the micro-letter) or pay the two-dimensional code (non-micro-letter) According to the return information to complete the payment.
Here's a step-by-step Talk:
1. Micro-credit payment related to the necessary order parameters are three, respectively: body (commodity name or order description), Out_trade_no (generally for order number) and Total_fee (order Amount, Unit "", to pay attention to unit problems), in different applications, The first thing to do is to get the relevant information in order to prepare for the payment parameter generation.
2. Other necessary payment parameters are AppID (micro-letter AppID), MCH_ID (notified after successful application), Device_info (both Web-side and micro-end parameters are uniform, uppercase "Web"), Trade_type (this value is different depending on the use scenario , the micro-letter outside for "NATIVE", micro-letter internal "Jsapi"), Nonce_str (32-bit random string), SPBILL_CREATE_IP (initiating the payment of the terminal IP, that is, server IP), Notify_url (Payment callback address, Micro-trust server to notify the site to pay the completion or not, modify the order status, sign (signature), there is a need to explain where, if Trade_type for Jsapi, OpenID is required parameters.
Signature algorithm is more prone to error, the signature steps are cumbersome, in fact, it is very important that sign does not participate in the signature
A: The 1, 2 mentioned in addition to the sign parameter assignment, placed in an array of arrays, sorted according to the dictionary order, in fact, the key values in the order of a-Z to sort.
B: Converts an array to a string, formatted as K1=V1&K2=V2&...KN=VN
C: After this string plus the key value (in the micro-letter payment merchant backend user set himself) now string = K1=v1&k2=v2&...kn=vn&key=key.
d:string = MD5 (string)
E:sign = Strtoupper (String)
At this point, the sign generation is complete.
Add sign to array arrays to generate a new array. Converts the array to XML. At this point, the micro-credit payment parameters are ready for completion.
3. The XML generated in 2 is sent using post to the micro-letter (Https://api.mch.weixin.qq.com/pay/unifiedorder) to obtain the returned XML information, which converts the information into an array format for easy operation. The XML information returned 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 it is trade_type==native pay, there will be more than one parameter code_url, the URL for the micro-scan code to pay the address.
4. The following is the process of payment.
If trade_type==native, then use a number of ways to convert Code_url into two-dimensional code, using micro-scan code can be, if the micro-letter internal Click to pay, you need to invoke the micro-letter js-sdk in the relevant things, The key to this step is to generate a JSON-formatted string.
The first is to generate an array array_jsapi that converts a JSON string.
A: The parameters of the array include: Appid,timestamp,noncestr,package,signtype (Default is "MD5"), note that the case and the above array is not the same.
B: Use the array to generate Paysign parameters, signed in the same way.
C: Append the paysign parameter to the ARRAY_JSAPI array.
D: Use Json_encode to format the array as a string js_string.
By completing the above work, you can pay for it inside the micro-letter.
The following is a sample code for related payments:
<script type= ' text/javascript ' >
function jsapicall ()
{
Weixinjsbridge.invoke (
' Getbrandwcpayrequest ',
$js _string,
function (res) {
WeixinJSBridge.log (res.err_msg);
if (res.err_msg== ' Get_brand_wcpay_request:ok ')
{
alert (' pay success ');
}
else
{
alert (' Payment failed ');
}}} function Callpay ()
{
if (typeof Weixinjsbridge = = ' undefined ') {
if (document.addeventlistener) {
Document.addeventlistener (' Weixinjsbridgeready ', Jsapicall, false);
} else if (document.attachevent) {
document.attachevent (' Weixinjsbridgeready ', jsapicall);
Document.attachevent (' Onweixinjsbridgeready ', jsapicall);
}
else{
Jsapicall ();
}
</script>
Js_string in code is the string we generate.
The Callpay () function is invoked in the HTML code to initiate the payment.
So the payment of the micro-letter payment is completed.
Here is the callback work, which ensures that the correct status is displayed to the user after the order has been paid successfully.
After payment is completed, the micro-letter uses post requests to feed the payment results to the Web server, and the Web server obtains post information to determine whether or not to modify the order information, depending on the success of the payment.
A: Remove the sign from the post parameter and record the value.
B: Sign the remaining parameters
C: Match the signature result with the sign in the post, the same description of the signature, and modify the order status according to the payment result.
E: Return the XML message to the micro-letter to ensure that the micro-mail know that the site has received the notification, to avoid the micro-letter push post again, examples are as follows:
<xml>
<return_code><![ Cdata[success]]></return_code>
<return_msg><![ Cdata[ok]]></return_msg>
</xml>
If it fails, it returns
<xml>
<return_code><![ Cdata[fail]]></return_code>
<return_msg><![ cdata[failure reason]]></return_msg>
</xml>
At this point, the entire development of micro-credit payment is completed.