Thinkphp and onethink implement the WeChat payment plug-in, thinkphponethink

Source: Internet
Author: User
Tags learn php programming openid

Thinkphp and onethink implement the payment plug-in, thinkphponethink

ThinkPHP and micro-payment implementation of the payment plug-in, call jssdk to implement payment, here are detailed code and tutorials, see here:

// Implemented Wxpay Hook method public function Wxpay ($ param) {require '. /Addons/Wxpay/WxPayPubHelper. php '; $ jsApi = new \ JsApi_pub (); // ========== Step 1: openid ============== if (! Isset ($ _ GET ['code']) {// return code $ url = $ jsApi-> createOauthUrlForCode (\ WxPayConf_pub: JS_API_CALL_URL. '/order_id /'. $ param ['order _ id']); Header ("Location: $ url");} else {$ order_id = $ param ['order _ id']; $ info = M ('order')-> where ('Id = '. $ order_id)-> find (); if (empty ($ info) | $ info ['is _ pay'] = 1) {dump ('this order does not exist or has been paid '. $ order_id); exit () ;}$ this-> assign ('info', $ info); $ a = $ info ['money']; $ B = 100; $ c = $ a * $ B; // GET the code to GET openid $ code = $ _ GET ['code']; $ jsApi-> setCode ($ code ); $ openid = $ jsApi-> getOpenId ();} // ========== Step 2: Use the unified payment interface, get prepay_id ================/// use the unified payment interface $ unifiedOrder = new \ UnifiedOrder_pub (); $ unifiedOrder-> setParameter ("openid ", "$ openid"); // product description $ unifiedOrder-> setParameter ("body", "Online Reservation"); // product description // custom order number, for example, $ timeStamp = time (); // $ out_trade_no = \ WxPayConf_pub: APPID. "$ timeStamp"; $ out_trade_no = $ info ['ordersn ']; $ unifiedOrder-> setParameter ("out_trade_no", "$ out_trade_no "); // Merchant Order No. $ unifiedOrder-> setParameter ("total_eter", "$ c"); // total amount $ unifiedOrder-> setParameter ("policy_url", \ WxPayConf_pub: policy_url. '/order_id /'. $ param ['order _ id']); // notification address $ unifiedOrder-> setParameter ("trade_type", "JSAPI "); // transaction type $ prepay_id = $ unifiedOrder-> getPrepayId (); // ========= Step 3: use jsapi to initiate a payment =============$ jsApi-> setPrepayId ($ prepay_id); $ jsApiParameters = $ jsApi-> getParameters (); $ this-> assign ('jsapiparameters ', $ jsApiParameters); $ this-> display ('pae ');}

The above is all the content of this article. I hope it will help you learn PHP programming.

Articles you may be interested in:
  • What is OneThink oneThink to add a plug-in the background?

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.