laravel5 integrated alipay Alipay scan code payment process (Laravel payment solution)

Keywords: PHP Mobile Laravel github

First of all, we will discuss how to use Alipay to pay in Laravel applications. For this, there are many related packages on GitHub, among which the two most popular packages are Omnipay For Laravel 5 & Lumen and Laravel AliPay. Laravel Alipay is used to illustrate the case:

Preparation: Alipay account / ant gold service open platform account No. - > Alipay sign up (immediate arrival)
Execute command installation package to project root

composer require latrell/alipay dev-master

Execution update

composer update

After performing the update, check the vendor directory to see if there is a latrell directory. If yes, the installation is successful. Otherwise, it is not. Because the directory of latrell contains alipay related files

Find the config/app.php configuration file, where key is an array of providers, and add service providers to the array.

'providers' => [
         /*
          * Laravel Framework Service Providers...
          */
          
          'Latrell\Alipay\AlipayServiceProvider',
        ]

Execute the command and generate the configuration file to the config / directory

php artisan vendor:publish

Configuration description

The configuration file config/latrell-alipay.php is a public configuration information file

config/latrell-alipay-web.php is the Web version of Alipay SDK configuration.

config/latrell-alipay-mobile.php is Alipay SDK configuration for mobile phones.

Open config/latrell-alipay-web.php and set the security check code and notice page

<?php
return [

   // Security check code, a 32-bit character composed of numbers and letters.
   'key' => 'a6cq60*****************zl',

   //Signature method
   'sign_type' => 'MD5',

   // Server asynchronous notification page path. Make corresponding modifications according to your own project path
   'notify_url' => 'http://web.wan.com/notify',

   // Page Jump synchronization notification page path. Make corresponding modifications according to your own project path
   'return_url' => 'http://web.wan.com/return'
];

Open config/latrell-alipay.php, set up seller Alipay account and partner identity id

<?php
  return [
     //Cooperator id,A 16 bit pure number beginning with 2088.
     'partner_id' => '2088************',

     //Seller Alipay account.
     'seller_id' => '28*******4@qq.com'
  ];

Set payment request routing

//Alipay payment processing routing
Route::get('alipay','Home\alipayController@Alipay');  // Initiate payment request
Route::any('notify','Home\alipayController@AliPayNotify'); //Server asynchronous notification page path
Route::any('return','Home\alipayController@AliPayReturn');  //Page Jump synchronization notification page path

Alipay sweep code payment case code

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/8
 * Time: 20:19
 */

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class alipayController extends Controller{

// Initiate payment request
public function Alipay(){
    $alipay = app('alipay.web');
    $alipay->setOutTradeNo('E0002332039');
    $alipay->setTotalFee('0.01');
    $alipay->setSubject('Millet 5 s');
    $alipay->setBody('Commodity: Alipay payment test');

    $alipay->setQrPayMode('5'); //This setting is optional 1-5,Add this parameter setting to support QR code payment.

    // Jump to the payment page.
    return redirect()->to($alipay->getPayLink());
}

// Asynchronous notification of payment results
public function AliPayNotify(Request $request){
// Verify the request.
if (!app('alipay.web')->verify()) {
    Log::notice('Alipay notify post data verification fail.', [
        'data' => $request->instance()->getContent()
    ]);
    return 'fail';
}
// Determine the notification type.
switch ($request ->input('trade_status','')) {
    case 'TRADE_SUCCESS':
    case 'TRADE_FINISHED':
        // TODO: If the payment is successful, obtain the order number for other related operations.
        Log::debug('Alipay notify post data verification success.', [
            'out_trade_no' => $request -> input('out_trade_no',''),
            'trade_no' => $request -> input('trade_no','')
        ]);
        break;
}
return 'success';
}

// Sync notification payment results
public function AliPayReturn(Request $request){
// Verify the request.
if (!app('alipay.web')->verify()) {
    Log::notice('Alipay returned query data validation failed.', [
        'data' => $request->getQueryString()
    ]);
    return view('alipayfail');
}
// Determine the notification type.
switch ($request ->input('trade_status','')) {
    case 'TRADE_SUCCESS':
    case 'TRADE_FINISHED':
        // TODO: If the payment is successful, obtain the order number for other related operations.
        Log::debug('Alipay notification received data validation successfully.', [
            'out_trade_no' => $request ->input('out_trade_no',''),
            'trade_no' => $request -> input('trade_no','')
        ]);
        break;
}
return view('alipaysuccess');
}
}

 

Alipay payment mobile terminal:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/8
 * Time: 22:19
 */
namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class alipayController extends Controller{
// Initiate payment request
public function Alipay(){
    $alipay = app('alipay.mobile');
    $alipay->setOutTradeNo('E0002332039');
    $alipay->setTotalFee('0.01');
    $alipay->setSubject('Millet 5 s');
    $alipay->setBody('Commodity: Alipay payment test');
   
   // Returns the signed payment parameters to Alipay mobile terminal. SDK. 
    return $alipay->getPayPara();
}

// Alipay notifications payment results asynchronously
public function AliPayNotify(Request $request){
// Verify the request.
if (!app('alipay.mobile')->verify()) {
    Log::notice('Alipay notify post data verification fail.', [
        'data' => $request->instance()->getContent()
    ]);
    return 'fail';
}
// Determine the notification type.
switch ($request ->input('trade_status','')) {
    case 'TRADE_SUCCESS':
    case 'TRADE_FINISHED':
        // TODO: If the payment is successful, obtain the order number for other related operations.
        Log::debug('Alipay notify post data verification success.', [
            'out_trade_no' => $request -> input('out_trade_no',''),
            'trade_no' => $request -> input('trade_no','')
        ]);
        break;
}
return 'success';
}

AliPay. Its GitHub project address is: https://github.com/Latrell/Alipay . This project is in Chinese. The instructions on GitHub are very clear

Posted by Rollo Tamasi on Wed, 04 Dec 2019 05:01:39 -0800