Laravel uses Alipay for payment development instances

Source: Internet
Author: User
Tags php file php and csrf attack

First, we will discuss how to use Alipay for payment in the Laravel application. There are many related packages on GitHub. Here we will introduce the two most popular packages: omnipay For Laravel 5 & Lumen and Laravel AliPay.

1. Use OmniPay For Laravel 5 & Lumen

Introduction & Introduction

Before getting started, let's take a look at the OmniPay GitHub project.

OmniPay is a framework-independent PHP library that supports various Gateway payment processes. Its website is elastic. Currently, the supported payment gateways include Alipay, UnionPay payment, Stripe, and PayPal. For more information about the payment gateways, see the GitHub project.

As the name suggests, OmniPay for Laravel 5 & Lumen is based on Laravel 5 and integrates the PHP OmniPay library with a service provider to easily support multiple payment gateways. Here we configure the payment method as Alipay.

Installation & Configuration

Add the following dependency to composer. json:

"Ignited/laravel-omnipay": "2 .*",
"Lokielse/omnipay-alipay": "dev-master"

Run composer update to install these dependencies.

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

Ignited \ LaravelOmnipay \ LaravelOmnipayServiceProvider: class

Add the OmniPay facade to the facade array at the same time:

'Omnipay' => Ignited \ LaravelOmnipay \ Facades \ OmnipayFacade: class

Finally, you can use php artisan vendor: publish to publish the configuration file.

In addition, OmniPay for Laravel 5 & Lumen supports Lumen. Register a service provider in bootstrap/app. php:

$ App-> register (Ignited \ LaravelOmnipay \ LumenOmnipayServiceProvider: class );

Copy the laravel-omnipay.php file to the config/laravel-omnipay.php and add the following code in bootstrap/app. php:

$ App-> configure ('laravel-omnipay ');

Create an Alipay application

The premise of using the Alipay SDK for payment is to first go to the Alipay open platform (open.alipay.com) to become a developer and create a website/mobile application, waiting for approval.

Then obtain the APPID and PID of the application in the Personal Center:

 

Then edit the config/laravel-omnipay.php in the project:

<? Php

Return [

// Default Payment Gateway
'Default' => 'alipay ',

// Configure various payment gateways
'Gateway' => [
'PayPal '=> [
'Driver '=> 'PayPal _ Express ',
'Options' => [
'Solutiontype' => '',
'Landingpage' => '',
'Headerimageurl' =>''
            ]
],

'Alipay' => [
'Driver '=> 'Alipay _ Express ',
'Options' => [
'Parts' => 'Your pid here ',
'Key' => 'Your appid here ',
'Selleremail '=> 'Your alipay account here ',
'Returnurl' => 'Your returnUrl here ',
'Your url' => 'Your own yurl here'
            ]
        ]
    ]

];

Basic usage

1) define related routes

// Alipay payment processing
Route: get ('Alipay/pae', 'alipaycontroller @ pae ');
// Go to the page after payment
Route: post ('Alipay/return ', 'alipaycontroller @ result ');
2) payment

The Alipay SDK supports the following interfaces:

Alipay_Express Alipay real-time receiving interface
Alipay_Secured Alipay secured transaction interface
Alipay_Dual Alipay dual-function transaction interface
Alipay_WapExpress Alipay WAP client interface
Alipay_MobileExpress Alipay wireless payment interface
Alipay_Bank Alipay online banking quick interface

Here, we use the Alipay real-time account arrival interface (currently, the real-time account arrival interface can be used only after the merchant signs the contract, and the individual account cannot be used ).

Create a new controller AlipayController and define its pay method as follows:

Public function pay (){

$ Gateway = Omnipay: gateway ();

$ Options = [
'Out _ trade_no '=> date ('ymdhis'). mt_rand ),
'Subobject' => 'Alipay test ',
'Total _ users' => '0. 01 ',
];

$ Response = $ gateway-> purchase ($ options)-> send ();
$ Response-> redirect ();
}

3) callback

Next we will define the result method corresponding to the jump page after payment:

Public function result (){

$ Gateway = Omnipay: gateway ();

$ Options = [
'Request _ params '= >$ _ request,
];

$ Response = $ gateway-> completePurchase ($ options)-> send ();

If ($ response-> isSuccessful () & $ response-> isTradeStatusOk ()){
// After successful payment
Exit ('payment successful ');
} Else {
// Notification of failed payment.
Exit ('payment failed ');
    }

}

In addition, because the callback request is from a third-party API and cannot pass CSRF verification, you need to exclude this URL in CSRF verification. Otherwise, a TokenMismatchException exception will be thrown, for specific troubleshooting methods, refer to the CSRF attack principle and its protection tutorial. The URL we want to exclude here is alipay/return.

2. Use Laravel AliPay

The preceding section describes how to use the OmniPay for Laravel 5 & Lumen package. Next, let's take a look at AliPay, a Laravel package that encapsulates the AliPay SDK. The GitHub project address is https://github.com/latrell/alipay. This project is a Chinese version. The instructions on GitHub are clear and I will not go into details here.

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.