Use EasyWechat to quickly develop WeChat public account payment and easywechat public account payment

Source: Internet
Author: User
Tags oauth openid

Use EasyWechat to quickly develop public account payment and easywechat public

Preparations:

After you apply for payment, you will receive two parameters: the merchant id and the Merchant key.
Note that these two parameters should not be confused with the parameters.
Parameter: appid, appkey, token
Payment parameters: merchant_id (merchant ID) and key (Payment key)
How can I get the payment key?
Go to https://pay.weixin.qq.com --> account center --> API security --> set API keys
Set a 32-bit key


Payment process:

1. Install the EasyWechat package in composer

Environment requirements:

  • PHP> = 5.5.9
  • PHP cURL Extension
  • PHP OpenSSL Extension

Installation:

Composer require overtrue/wechat :~ 3.1-vvv

 

2. Configure webpage authorization

 

 

3. initialize the SDK and createEasyWeChat\Foundation\ApplicationInstance

<? Phpuse EasyWeChat \ Foundation \ Application; protected $ app = null; public function construct () {$ options = [/*** Debug mode, bool value: true/false ** when the value is false, all logs do not record */'debug' => true,/*** basic account information, obtain */'app _ id' => 'your-app-id' from the public platform/open platform ', // AppID 'secret' => 'your-app-secret', // AppSecret 'Token' => 'your-token ', // Token 'aes _ key' => '', // EncodingAESKey. Be sure to enter it in safe mode !!! /*** Log configuration ** level: Log level. Optional values: * debug/info/notice/warning/error/critical/alert/emergency * permission: log file Permission (Optional). The default value is null (if it is null, monolog will take 0644) * file: Log file Location (absolute path !!!), Write permission required */'log' => ['level' => 'debug', 'permission' => 0777, 'file' => '/tmp/easywechat. log',],/*** OAuth configuration ** scopes: public platform (snsapi_userinfo/snsapi_base), Open Platform: snsapi_login * callback: the callback page address after OAuth authorization is completed */'oauth' => ['scopes '=> ['snsapi _ userinfo'], 'callback' => '/examples/oauth_callback.php',], /*** payment */'payment' => ['Merchant _ id' => 'your-mch-id ', 'key' => 'key-for-signature', 'cert _ p Ath '=> 'path/to/your/cert. pem', // XXX: absolute path !!!! 'Key _ path' => 'path/to/your/key', // XXX: absolute path !!!! 'Your Y _ url' => 'default order callback address ', // You can also set it to overwrite it when placing an order. // 'device _ info' => '000000', // 'sub _ app_id '=> '', // 'sub _ merchant_id '=> '', //...],]; $ this-> $ app = new Application ($ options );}

 

4. Get the payment object payment

$payment =$this->$app->payment;

 

5. input the order object order (order number, amount, openid) with Parameters

<? Phpuse EasyWeChat \ Foundation \ Application; use EasyWeChat \ Payment \ Order; $ attributes = ['trade _ type' => 'jsapi ', // JSAPI, NATIVE, APP... 'body' => 'ipad mini 16 Gbit/s white ', 'detail' => 'ipad mini 16 Gbit/s white', 'Out _ trade_no' => '123 ', // Order No. 'total _ url' => 5388, // unit: 'Your Y _ url' => 'HTTP: // xxx.com/order-policy', // url of the notification of the payment result, if this parameter is not set, the default address 'openid' => 'current user openid' in the configuration will be used. // trade_type = JSAPI, this parameter is required, the unique ID of the user under the merchant's appid, //...]; $ order = new Order ($ attributes );

 

6. preprocessing, get a preprocessing id, payment-> prepare (order );

$result = $payment->prepare($order);if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){    $prepayId = $result->prepay_id;}

 

7. Generate payment JS Configuration

$ Json = $ payment-> configForPayment ($ prepayId); // return a json string. to return an array, set the second parameter to false.

 

8. The Order Number and json will be written into the template for the user to confirm the payment, trigger js, and initiate the payment

return view('done',['order'=>$ordersn,'json'=>$json]);
<Script> $ ('form'). submit (function () {WeixinJSBridge. invoke ('getbrandwcpayrequest ',{!! $ Json !!}, Function (res) {if (res. err_msg = "get_brand_wcpay_request: OK") {// determine the front-end response using the preceding method. The team solemnly prompts: // res. err_msg will return // OK after the user successfully pays, but it is not absolutely reliable. }}); Return false ;}); </script>

 

9. Successful callback

After the user successfully pays, the server will initiate a POST request to the callback URL set in the order, the request content is an XML.

Configure the paid Method in the middleware VerifyCsrfToken without performing CSRF verification.

Public function paid () {$ response = $ this-> $ app-> payment-> handlenoul (function ($ policy, $ successful) {// use the "Payment order Number" or "Merchant order Number" in the notification to go to your database and find the order $ order = query order ($ every Y-> out_trade_no); if (! $ Order) {// return 'order not exist if the Order does not exist. '; // indicates that the order has been processed and cannot be found, don't inform me again} // if the order exists // check whether the order has been updated with the payment status if ($ order-> paid_at) {// if the order field "Payment time" is not blank, return true if the payment has been made; // if the payment is successful, no updates will be made.} // if ($ successful) {// if the payment is not made, change it to the payment status $ order-> paid_at = time (); // update the payment time to the current time $ order-> status = 'paid';} else {// payment failed $ order-> status = 'paid _ fail ';} $ order-> save (); // save order return true; // return processing completed}); return $ response ;}

Now the payment is complete. If you have other questions, please refer to the EasyWeChat document.



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.