PHP WeChat payment development-scan payment (Mode 2) Post-callback method

Source: Internet
Author: User
In fact, I feel that I am outdated when writing this article, but I search for & quot; payment development-scan payment (Mode 2) on Baidu and how to call back & quot; when looking for answers, I found that many friends still did not solve this problem, so I shared my solutions with you. In fact, when I wrote this article, I felt that I was out of date. However, when I searched Baidu for "how to call back after payment development scan payment (Mode 2, I found that many of my friends have not solved this problem, so I will share my solutions with you.

One-click download and payment sdk(the pen uses PHP as an example, and the sdkpackage is wxpayapi_php_v3.zip)

Download the SDK package and decompress it. in the decompressed Directory, we will see the following Directory:

2. check the payment developer documentation and find that the demo for scanning the code is the native. php file under the example directory.

For convenience, we need to put the unzipped file into the wxpay (which can be named as needed) folder under the root directory of the local environment.

3. for example, enter http: // localhost/wxpay/example/native. php in the browser

After opening the URL above, we found that there are two QR codes, such as the question. we are studying the second scan mode today (officially also recommended the second scan mode for payment)

4. we use a mobile phone to log in, scan the QR code of Mode 2 on the page above, and pay

Here we find an interesting problem. after you make the payment successfully, the PC page does not change, so the main problem we should consider is how to make a callback after the payment.

I will not talk much nonsense here. I have referred to many methods on the Internet and summarized them as follows:

1. delete some html in scan mode 1 in the native. php file, and only some html code in scan Mode 2 is left.

2. because the official documents also show that the payment result of Scan Mode 2 is asynchronous and does not return the payment result, we use javascript to listen to the payment result from time to time, then, based on the request results, callback is performed on the next page. The final code of the author is as follows. if you are interested, refer:

Native. php file

 GetPrePayUrl ("123456789"); // mode 2/*** process: * 1. call unified order, get code_url, generate QR code * 2. the user scans the QR code, 3. after the payment is complete, the server will notify you of the successful payment * 4. in the notification of successful payment, you need to check the order to confirm whether the payment is successful (see "notify. php) */$ input = new WxPayUnifiedOrder (); $ input-> SetBody ("1 cent for purchasing He Ning"); $ input-> SetAttach ("1 cent for purchasing He Ning "); $ num = WxPayConfig: MCHID. date ("YmdHis"); $ input-> SetOut_trade_no ($ num); $ input-> SetTotal_fee ("1 "); $ input-> SetTime_start (date ("YmdHis"); $ input-> SetTime_expire (date ( "YmdHis", time () + 600); $ input-> SetGoods_tag ("test"); $ input-> SetNotify_url ("http://paysdk.weixin.qq.com/example/notify.php "); $ input-> SetTrade_type ("NATIVE"); $ input-> SetProduct_id ("123456789"); $ result = $ payy-> GetPayUrl ($ input ); $ url2 = $ result ["code_url"];?>    
     
      Payment example    

Scan payment mode 2


"Style =" width: 150px; height: 150px; "/>

0

Script // set to execute the load () method var myIntval = setInterval (function () {load ()}, 1000) every 1000 milliseconds; function load () {document. getElementById ("timer "). innerHTML = parseInt (document. getElementById ("timer "). innerHTML) + 1; var xmlhttp; if (window. XMLHttpRequest) {// code for IE7 +, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest ();} else {// code for IE6, IE5 xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");} x Mlhttp. onreadystatechange = function () {if (xmlhttp. readyState = 4 & xmlhttp. status = 200) {trade_state = xmlhttp. responseText; if (trade_state = 'success ') {document. getElementById ("myDiv "). innerHTML = 'payment successfully'; // alert (transaction_id); // The execution latency is 3000 milliseconds. execute the tz () method clearInterval (myIntval); setTimeout ("location. href = 'success. php '", 3000);} else if (trade_state = 'refund') {document. getElementById ("myDiv "). innerHTML = 'transfer-in and return '; ClearInterval (myIntval);} else if (trade_state = 'notpa') {document. getElementById ("myDiv "). innerHTML = 'Please pay by QR code ';} else if (trade_state = 'closed') {document. getElementById ("myDiv "). innerHTML = 'closed '; clearInterval (myIntval);} else if (trade_state = 'refered') {document. getElementById ("myDiv "). innerHTML = 'unrecoverable '; clearInterval (myIntval);} else if (trade_state = 'userpaying') {document. getElementById ("myDiv "). InnerHTML = 'payment in users';} else if (trade_state = 'payerror') {document. getElementById ("myDiv "). innerHTML = 'payment failed'; clearInterval (myIntval) ;}}// orderquery. the php file returns the order status and determines the payment status through the order status xmlhttp. open ("POST", "orderquery. php ", false); // The following sentence must contain // add the tag/value pair to the header file to be sent. Xmlhttp. setRequestHeader ("Content-type", "application/x-www-form-urlencoded"); xmlhttp. send ("out_trade_no = ");} Script

The orderquery. php code has also been adjusted accordingly:

 $ Value) {echo "$ key: $ value
";}} If (isset ($ _ REQUEST [" transaction_id "]) & $ _ REQUEST [" transaction_id "]! = "") {$ Transaction_id = $ _ REQUEST ["transaction_id"]; $ input = new WxPayOrderQuery (); $ input-> SetTransaction_id ($ transaction_id ); // printf_info (WxPayApi: orderQuery ($ input); $ result = WxPayApi: orderQuery ($ input); echo $ result ['trade _ state']; exit ();} if (isset ($ _ REQUEST ["out_trade_no"]) & $ _ REQUEST ["out_trade_no"]! = "") {$ Out_trade_no = $ _ REQUEST ["out_trade_no"]; $ input = new WxPayOrderQuery (); $ input-> SetOut_trade_no ($ out_trade_no ); // printf_info (WxPayApi: orderQuery ($ input); $ result = WxPayApi: orderQuery ($ input); echo $ result ['trade _ state']; exit () ;}?>

Create a success. php file:

 Payment successful 






Payment successful

The above is the code summarized by the Author. After debugging, it is found that there is no problem. For more information, see.

The above is the details of the callback method after scanning and payment (Mode 2) for PHP payment development. For more information, see other related articles on php Chinese network!

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.