支付宝接口笔记


支付宝接口地址

https://docs.open.alipay.com

Laravel支付宝插件

https://github.com/ignited/laravel-omnipay https://github.com/lokielse/omnipay-alipay

相关文档

http://laravelacademy.org/post/1475.html

安装laravel-omnipay

# 修改 composer 的全局配置文件
composer config -g repo.packagist composer https://packagist.phpcomposer.com
# 安装laravel命令
composer require "lokielse/omnipay-alipay" 
composer require "ignited/laravel-omnipay" // 可能会报错,直接改composer.json
# 运行composer update安装这些依赖。

composer.json

"require": {
    "php": ">=5.6.4",
    "laravel/framework": "5.4.*",
    "laravel/tinker": "~1.0",
    "ignited/laravel-omnipay": "2.*",
    "lokielse/omnipay-alipay": "dev-master"
},

config/app.php添加服务提供者

'providers' => [
    Ignited\LaravelOmnipay\LaravelOmnipayServiceProvider::class
    //'Ignited\LaravelOmnipay\LaravelOmnipayServiceProvider',
]
'aliases' => [
    'Omnipay' => 'Ignited\LaravelOmnipay\Facades\OmnipayFacade',
]

发布配置

php artisan vendor:publish --provider="Ignited\LaravelOmnipay\LaravelOmnipayServiceProvider"

配置支付接口

config/laravel-omnipay.php

// 默认支付网关
'default' => 'alipay',

// 各个支付网关配置
'gateways' => [
    'paypal' => [
        'driver' => 'PayPal_Express',
        'options' => [
            'solutionType' => '',
            'landingPage' => '',
            'headerImageUrl' => ''
        ]
    ],

    'alipay' => [
        'driver' => 'Alipay_LegacyExpress',
        'options' => [
            'partner' => 'your pid here',
            'key' => 'your appid here',
            'sellerEmail' =>'your alipay account here',
            'returnUrl' => 'your returnUrl here',
            'notifyUrl' => 'your notifyUrl here'
        ]
    ]
]

路由设置

/routes/api.php

// 支付宝支付处理
Route::get('alipay/pay', 'AlipayController@pay');
// 支付后跳转页面
Route::get('alipay/notify', 'AlipayController@notify');

控制器设置

// 生成控制器
php artisan make:controller AlipayController

app/Http/Controllers/AlipayController.php

use Omnipay;
class AlipayController extends Controller
{
    public function pay()
    {
        $options = [
            'out_trade_no' => date('YmdHis') . mt_rand(1000, 9999),
            'subject'      => 'test',
            'total_fee'    => '0.01',
        ];
        $gateway = Omnipay::gateway('alipay');
        $response = $gateway->purchase($options)->send();
        $response->redirect();
    }
}

Legacy Express Gateway

Gateway (网关)

/**
 * @var LegacyExpressGateway $gateway
 */
$gateway = Omnipay::create('Alipay_LegacyExpress');
$gateway->setSellerEmail('the_seller_email');
$gateway->setPartner('the_partner_id');
$gateway->setKey('the_md5_sign_key'); //For MD5 sign type
//$gateway->setPrivateKey('the_rsa_sign_key'); //For RSA sign type
//$gateway->setAlipayPublicKey('the_alipay_public_key'); //For RSA sign type
$gateway->setReturnUrl('https://www.example.com/return');
$gateway->setNotifyUrl('https://www.example.com/notify');

Purchase (购买)

$request = $gateway->purchase([
  'out_trade_no' => date('YmdHis').mt_rand(1000,9999),
  'subject'      => 'test',
  'total_fee'    => '0.01',
]);

/**
 * @var LegacyExpressPurchaseResponse $response
 */
$response = $request->send();

$redirectUrl = $response->getRedirectUrl();
//or 
$response->redirect();

Return && Notify (同步、异步通知)

$request = $gateway->completePurchase();
$request->setParams($_REQUEST);

/**
 * @var AopTradeAppPayResponse $response
 */
$response = $request->send();

try {
    $response = $request->send();

    if($response->isPaid()){
        /**
         * Payment is successful
         */
        die('success'); //The notify response should be 'success' only
    }else{
        /**
         * Payment is not successful
         */
        die('fail'); //The notify response
    }
} catch (Exception $e) {
    /**
     * Payment is not successful
     */
    die('fail'); //The notify response
}

支付宝接口文档说明

Gateway Description 说明 Links
Alipay_AopPage Alipay Page Gateway 电脑网站支付 - new Usage Doc
Alipay_AopApp Alipay APP Gateway APP支付 - new Usage Doc
Alipay_AopF2F Alipay Face To Face Gateway 当面付 - new Usage Doc
Alipay_AopWap Alipay WAP Gateway 手机网站支付 - new Usage Doc
Alipay_LegacyApp Alipay Legacy APP Gateway APP支付 Usage Doc
Alipay_LegacyExpress Alipay Legacy Express Gateway 即时到账 Usage Doc
Alipay_LegacyWap Alipay Legacy WAP Gateway 手机网站支付 Usage Doc