跳至主要內容

支付宝接口笔记

Moments大约 3 分钟

支付宝接口笔记


支付宝接口地址

https://docs.open.alipay.comopen in new window

Laravel支付宝插件

https://github.com/ignited/laravel-omnipayopen in new windowhttps://github.com/lokielse/omnipay-alipayopen in new window

相关文档

http://laravelacademy.org/post/1475.htmlopen in new window

安装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
}

支付宝接口文档说明

GatewayDescription说明Links
Alipay_AopPageAlipay Page Gateway电脑网站支付 - newUsageopen in new window Docopen in new window
Alipay_AopAppAlipay APP GatewayAPP支付 - newUsageopen in new window Docopen in new window
Alipay_AopF2FAlipay Face To Face Gateway当面付 - newUsageopen in new window Docopen in new window
Alipay_AopWapAlipay WAP Gateway手机网站支付 - newUsageopen in new window Docopen in new window
Alipay_LegacyAppAlipay Legacy APP GatewayAPP支付Usageopen in new window Docopen in new window
Alipay_LegacyExpressAlipay Legacy Express Gateway即时到账Usageopen in new window Docopen in new window
Alipay_LegacyWapAlipay Legacy WAP Gateway手机网站支付Usageopen in new window Docopen in new window
上次编辑于:
贡献者: Moments