Develop the APP-side WeChat payment function in PHP, and develop the app payment function in php.

Source: Internet
Author: User

Develop the APP payment function in PHP, and develop the app payment function in php.

My personal experience in developing APP payment with PHP

Recently, due to the needs of the company, I had to develop the payment on the APP. After reading the documentation, I felt pretty good. I didn't encounter a big pitfall. I should pay attention to not too many points.

Write a notebook as a memorandum.

APP payment process

From the above picture, we can see that the process should be noted that there are three parts in total;
Part 1: Call the ordering API, return the pre-payment order, and then return the information (4, 5, 6, 7) after the signature)
Part 2: asynchronous notification (15, 16)
Part 3: final judgment of the payment result
The first part is to call the order API, return the pre-payment order, and sign the order before returning information.

Detailed descriptions are provided in this document.

In the appendix, I wrote my code and reached out to the party. I can use the code with a slight change.

// Function weChatPay () {$ json = array (); // a required parameter for generating a pre-payment transaction ticket: $ newPara = array (); // Application ID $ newPara ["appid"] = "wx2421b1c4370ec43b"; // merchant ID $ newPara ["mch_id"] = "10000100 "; // device No. $ newPara ["device_info"] = "WEB"; // random string. function generation is recommended here $ newPara ["nonce_str"] = "1add1a30ac87aa2db72f57a2375d8fec "; // product description $ newPara ["body"] = "APP payment test"; // merchant's order number, here is the merchant's Internal Order Number $ newPara ["out_trade_no"] = "1415659990"; // total amount $ newPa Ra ["total_fee"] = 1; // Terminal IP $ newPara ["spbill_create_ip"] = $ _ SERVER ["REMOTE_ADDR"]; // notification address. Note, do not add the parameter $ newPara ["yy_url"] = "http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php" in the url here; // transaction type $ newPara ["trade_type"] = "APP "; // The first signature $ newPara ["sign"] = produceWeChatSign ($ newPara); // convert the array into xml format $ xmlData = getWeChatXML ($ newPara ); // use the php curl package to send data to the unified order interface and return the normal prepay_id $ get_data = sendPrePayCurl ($ xm LData); // returns the result for determination. If ($ get_data ['Return _ Code'] = "SUCCESS" & $ get_data ['result _ Code'] = "SUCCESS ") {// perform a secondary Signature Based on the result returned by the payment // the random string required for the secondary signature $ newPara ["nonce_str"] = "5k8rjiltkch16cq2502si8znmtm67vs "; // timeStamp required for the second signature $ newPara ['timestamp'] = time (). ""; // Add the remaining parameters of the secondary signature $ secondSignArray = array ("appid" => $ newPara ['appid '], "noncestr" => $ newPara ['nonce _ str'], "package" => "Sign = WXPay ", "prepayid" => $ get_data ['prepay _ id'], "partnerid" => $ newPara ['mch _ id'], "timestamp" => $ newPara ['timestamp'],); $ json ['datas'] = $ secondSignArray; $ json ['ordersn '] = $ newPara ["out_trade_no"]; $ json ['datas'] ['sign'] = weChatSecondSign ($ newPara, $ get_data ['prepay _ id']); $ json ['message'] = "pre-payment completed"; // pre-payment completed, perform the internal business logic at the bottom of the page/****************************/return json_encode ($ json );} else {$ json ['message'] = $ get_data ['Return _ msg '] ;}} return json_encode ($ json );} // The function produceWeChatSignfunction produceWeChatSign ($ newPara) {$ stringA = self: getSignContent ($ newPara); $ stringSignTemp = $ stringA. "& key = 192006250b4c09247ec02edce69f6a2d"; return strtoupper (MD5 ($ stringSignTemp);} // generate the xml format function public static function getWeChatXML ($ newPara) {$ xmlData = "<xml>"; foreach ($ newPara as $ key => $ value) {$ xmlData = $ xmlData. "<". $ key. "> ". $ value. "</". $ key. ">" ;}$ xmlData = $ xmlData. "</xml>"; return $ xmlData;} // function that sends data to an interface via curl sendPrePayCurl ($ xmlData) {$ url = "https://api.mch.weixin.qq.com/pay/unifiedorder "; $ header [] = "Content-type: text/xml"; $ curl = curl_init (); curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ header); curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ curl, CURLOPT_POST, 1); curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ xmlData ); $ data = curl_exec ($ curl); if (curl_errno ($ curl) {print curl_error ($ curl);} curl_close ($ curl); return self :: XMLDataParse ($ data);} // data parsing function in xml format: public static function XMLDataParse ($ data) {$ msg = array (); $ msg = (array) simplexml_load_string ($ data, 'simplexmlelement', LIBXML_NOCDATA); return $ msg;} // function weChatSecondSign of the second signature ($ newPara, $ prepay_id) {$ secondSignArray = array ("appid" => $ newPara ['appid '], "noncestr" => $ newPara ['nonce _ str'], "package" => "Sign = WXPay", "prepayid" => $ prepay_id, "partnerid" => $ newPara ['mch _ id'], "timestamp" => $ newPara ['timestamp'],); $ stringA = self: getSignContent ($ secondSignArray); $ stringSignTemp = $ stringA. "& key = 192006250b4c09247ec02edce69f6a2d"; return strtoupper (MD5 ($ stringSignTemp ));}

Two notes:

1. The secondary signature must be completed in the background. After the secondary signature is completed, all the information used for the secondary signature is sent to the front-end, so that the front-end can call up the payment. In this case, payment cannot be lifted.
2. Two signatures use different random strings.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.