Laravel a case of WeChat payment development

Source: Internet
Author: User
Tags button type

This article we mainly share with you laravel to pay development examples, payment in the now is very popular especially in the mobile phone we only need simple operation to complete the payment, I hope we can help everyone.

1. Install dependent packages

Run the following command in the project root directory to install dependencies:

Composer require Overtrue/laravel-wechat 2.1.*

After the installation is complete, register the service provider in config/app.php:

Overtrue\laravelwechat\serviceprovider::class
If you want to pass the WeChat façade in your code, you can add it to the config/app.php aliases array:

' Wechat ' = Overtrue\laravelwechat\facade::class

2. Configure public number settings
After doing this, run the following command to publish the configuration file to the Config directory:

PHP Artisan Vendor:publish

Edit the config/wechat.php file after running successfully:

<?php  return [      ' use_alias ' = env (' Wechat_use_alias ', false),      ' app_id ' = env (' Wechat_appid ', ' Your AppId '),//Required      ' secret ' = env (' Wechat_secret ', ' Your secret '),//Required      ' token ' = env (' Wechat_token ', ' Y Our Token '),//Required      ' encoding_key ' = env (' Wechat_encoding_key ', ' Your encoding AES key ')//only required for encryption mode)  ;

Fill in the relevant information of your public number to the corresponding configuration, note that if you want to use the payment function, a certified public number is required, and the current public platform test account does not support the payment interface.

Before using the payment, you need to configure the authorization directory, test account and other information on the public platform first. For payment development documentation, refer to: https://pay.weixin.qq.com/wiki/doc/api/index.html

3. Examples of Use

Note: Laravel 5 is enabled by default with the CSRF middleware, because the message is POST, so triggering the CSRF check causes the message to not respond correctly, so you can exclude the specified URL from CSRF validation. For specific reference: How to exclude the specified URL from csrf in Laravel.
Php

<?phpuse overtrue\wechat\payment;  Use Overtrue\wechat\payment\order;  Use overtrue\wechat\payment\business;      Use overtrue\wechat\payment\unifiedorder;/** * 1th step: Define Merchant */$business = new Business (app_id, App_key,  mch_id, Mch_key);/** * 2nd step: Define ORDER */$order = New Order ();  $order->body = ' test body ';  $order->out_trade_no = MD5 (Uniqid (). Microtime ()); $order->total_fee = ' 1 ';  Units are "points", string types $order->openid = open_id; $order->notify_url = ' http://xxx.com/wechat/payment/notify ';/** * 3rd Step: Uniform Order */$unifiedOrder = new Unifiedorder ($  Business, $order);/** * 4th step: Generate Payment Profile */$payment = new Payment ($unifiedOrder); Javascriptvar wxpayment = function () {if (typeof Weixinjsbridge = = = ' undefined ') {alert (' Please open the page!          ');      return false; } weixinjsbridge.invoke (' Getbrandwcpayrequest ', <?php echo $payment->getconfig ();?., Function (res             ) {switch (res.err_msg) {     Case ' Get_brand_wcpay_request:cancel ': Alert (' User canceled payment!                      ');                  Break Case ' Get_brand_wcpay_request:fail ': alert (' Payment failed!                      (' Res.err_desc ') ');                  Break Case ' Get_brand_wcpay_request:ok ': alert (' Pay success!                      ');                  Break                      Default:alert (Json.stringify (res));              Break  }          }      ); } Html<button type= "button" onclick= "Wxpayment ()" > Pay ¥<?php Echo ($order->total_fee/100);?> Yuan &l T;/button>

Payment Notice

<?phpuse overtrue\wechat\payment\notify; $notify = new Notify (      app_id,      app_key,      mch_id,      Mch_key  ); $transaction = $notify->verify (); if (! $transaction) {      $notify->reply (' FAIL ', ' Verify transaction Error ');  } Var_dump ($transaction); Echo $notify->reply ();

4. Use in lumen

In addition, Laravel-wechat also supports lumen. After installing laravel-wechat with composer, you need to add this line of code around 82 lines in bootstrap/app.php:

$app->register (Overtrue\laravelwechat\serviceprovider::class);

Also configure the following options in env:

Wechat_use_alias=false  wechat_appid=xxx  wechat_secret=xxx  wechat_token=xxx  wechat_encoding_ Key=xxx

If you are accustomed to using config/wechat.php to configure it, remember to add it after 19 lines in bootstrap/app.php:

$app->configure (' WeChat ');

Of course, in addition to this, Laravel-wechat also provides more SDK support.

Related Article

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.