WeChat QR scan mode 2: Payment and callback

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to learn about the QR code payment mode and callback implementation.
Controller:
Namespace Home \ Controller;
Use Think \ Controller;
/********* QR code scanning mode 2 payment *********/
Class WxpayController extends Controller {
// Introduce the relevant class library in the class initialization method
Public function _ initialize () {// This is the official demo file. I have introduced other files to the api file. You can introduce them one by one as you like.
Vendor ('wxpay. lib. Wxpay # api ');
}
// Pay
Public function index (){
R ('Public/header ');
$ Host = 'HTTP: // '. $ _ SERVER ['HTTP _ host']; // obtain the current domain name
$ Notify = new \ NativePay (); // instantiate the class
$ WxPayConfig = new \ WxPayConfig (); // instantiate the class
$ Input = new \ WxPayUnifiedOrder (); // instantiate the class
$ Input-> SetBody ($ _ POST ['body']); // order description
$ Input-> SetOut_trade_no ($ _ POST ['out _ trade_no ']); // Bill No.
$ Input-> SetTotal_fee ($ _ POST ['total _ 0000'] * 100); // The payment amount is in units. Therefore, the passed parameter is multiplied by 100.
$ Input-> setpolicy_url ($ host. "/Wxpay/policy.html"); // callback address
$ Input-> SetTrade_type ("NATIVE ");
$ Input-> SetProduct_id ($ _ POST ['product _ id']); // item id
$ Result = $ Y-> GetPayUrl ($ input); // scan mode 2
$ Codeurl = $ result ["code_url"];
$ This-> assign ('codeurl', $ codeurl );
// Here you can add your order receiving code
$ This-> display ();
}

// Callback after successful payment
Public function required y (){
$ Xml = $ GLOBALS ['HTTP _ RAW_POST_DATA ']; // The returned xml
File_put_contents (dirname (_ FILE _). '/xml.txt', $ xml); // record whether the xml.txt FILE contains content after the log payment is successful. If an xml FILE exists, the callback is successful.
// File_get_contents (dirname (_ FILE _). '/xml.txt ');
$ XmlObj = simplexml_load_string ($ xml, 'simplexmlelement', LIBXML_NOCDATA );
$ XmlArr = json_decode (json_encode ($ xmlObj), true );
$ Out_trade_no = $ xmlArr ['out _ trade_no ']; // Order Number
$ Total_amount = $ xmlArr ['total _ 0000']/100; // The amount in the xml file returned after callback is in units
$ Result_code = $ xmlArr ['result _ Code']; // status
If ($ result_code = 'success') {// database operation
// Process database operations, such as modifying the order status to recharge the account, etc.
Echo 'success '; // the SUCCESS of the return must be carried to the end. Otherwise, the callback will be performed eight times.
Exit;
} Else {// failed
Return;
Exit;
}
}

}

Template file:

Scan the following QR code to recharge your account.






1. png (23.19 KB download: 9 times)

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.