使用 yansongda/laravel-pay 实现支付宝在线支付接口

作者: JONE 分类: 编程 发布时间: 2021-01-28 22:53

项目地址:

Laravel版:https://github.com/yansongda/laravel-pay

通用版:https://github.com/yansongda/pay

文档地址:https://pay.yansongda.cn/

设置参数说明(建议先支付宝沙箱调试)

首先用支付宝密钥生成工具:https://opendocs.alipay.com/open/291/106097/,保存好应用私钥和应用公钥,应用公钥用于保存到支付宝开放平台设置中并生成支付宝公钥。
ali_public_key:支付宝公钥
private_key:应用私钥
‘mode’ => ‘dev’  为选填参数,可要可不要,设置此参数后则进入沙箱模式
level’ => ‘info’ 日志等级,建议生产环境等级调整为 info,开发环境为 debug

注意事项1,验证回调信息,此方法验证通过时返回接收的数据,验证失败则报错

$result = $alipay->verify();

// 支付回调操作
public function alipay_callback(Request $request)
{
    try {
        $res = Pay::verify();
        // dd($res->toArray());

        // 事务处理
        DB::transaction(function () use ($res,$order) {
   
        });

    } catch (\Exception $exception) {
        $all = $request->all();
        $all['app_err_msg'] = $exception->getMessage();
        $all['app_request_url'] = $request->fullUrl();
        info($all);
    }

     /*        [
      "charset" => "utf-8",
      "out_trade_no" => "bc1611824661174712",
      "method" => "alipay.trade.page.pay.return",
      "total_amount" => "100.00",
      "sign" => "19+VTTVZ0Ord2tZNl8X6sjh2WlcUw24YweKhkomMkk+G3ah/IB2Snau5WhID2CD/M5A3oNpEUDdn6lzn+E4wwC3JiTVtohfeIHDqqmAWWryxlRLNYI2N4uNNHT/PwMZzpSQ53hoXnjz14M3bEVjCbrsaiQmMFktwtbRkSoGH9oa4qDnHtOGLkoc6OIVfH1HVq8WCaXXy6uvqlf5xkxUCEirdvegnqGWj26FDI0h1YxLVpmICdK8Rzn5SsJ1DC0ldv2feW1yFyjEA4qw9YuodmgkSV17CsAbnb23Ldyoq8qoLqy9LXi+e1stqZBNdWLAyPZeNXtrUABO9exILh7TE8w== ◀",
      "trade_no" => "2021012822001462580501364866",
      "auth_app_id" => "2016081500253620",
      "version" => "1.0",
      "app_id" => "2016081500253620",
      "sign_type" => "RSA2",
      "seller_id" => "2088102171320393",
      "timestamp" => "2021-01-28 17:06:06",
    ];*/

}

注意事项2:支付宝异步回调为post请求,防止被laravel的csrf中间件拦截

支付宝官方异步回调文档,支付宝异步回调

\app\Http\Middleware\VerifyCsrfToken.php  设置忽略特定路由

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        '/alipay_notice',  // 对应 路由 Route::any('/alipay_notice', 'App\Http\Controllers\Home\OrderController@alipay_notice'); // 订单支付 异步通知
]; }

支付宝异步回调数据示例

[2021-01-29 10:10:57] local.INFO: array (
  'gmt_create' => '2021-01-29 10:10:56',
  'charset' => 'utf-8',
  'seller_email' => 'plkflw8428@sandbox.com',
  'notify_time' => '2021-01-29 10:10:56',
  'subject' => '新年牛牛',
  'sign' => 'q8RrWghMO/HvsS4qxVn0ZZWvTdDnRdHwgFZPtYEO2T9B04o1gybOdJw/eNxkGx/hWH1AQWijFEKzXDTDpJOBEePLJ2VT3iwJ1O+BsFXcEQJN2v33Xp8LXcrXkaV900YWgIk0+w2ZIp297hlHqsM4Ky9XKdzx3ULr2CCZsxZm6loyfdGCiaVNYZo2JKKT4mkc+EwT/FFbOHT0nfhos+H195qB+hCXL1diWYonIz8HlF/mZwfqxV1sq3UOlEPOXKZA7NxjTg5oRvSVTdW61Wh5n+0DJMfFRIZcSCI6cy1rim1pyMxI1WVpnaNCyFiM6bpsZt+YFzBIAQ7p6oPnMSTFdA==',
  'buyer_id' => '2088102174862582',
  'version' => '1.0',
  'notify_id' => '2021012900222101056062580512320890',
  'notify_type' => 'trade_status_sync',
  'out_trade_no' => 'bc1611886241841635',
  'total_amount' => '3.70',
  'trade_status' => 'WAIT_BUYER_PAY',
  'trade_no' => '2021012922001462580501364421',
  'auth_app_id' => '2016081500253620',
  'buyer_logon_id' => 'miu***@sandbox.com',
  'app_id' => '2016081500253620',
  'sign_type' => 'RSA2',
  'seller_id' => '2088102171320393',
) 
[2021-01-29 10:10:58] local.INFO: array (
  'gmt_create' => '2021-01-29 10:10:56',
  'charset' => 'utf-8',
  'seller_email' => 'plkflw8428@sandbox.com',
  'subject' => '新年牛牛',
  'sign' => 'iH+dd0Ks2h9qF2rOtv7mbeN6IzKh6TurxdGxSx9qSFXNp1WwCV8B4BwN4DxGIzvR9K39WxrMmSGGBedKsMv/syLyNsMd6z8+l7aBWeuoeo3K/XjdaOIsodJQfiaqNXhGX7IaRhEjcaGr+e3zn+VFeRzCAe1e9BJ+tnQ6SjZ8keIr47/rDx5epZO4s2MiHKUzC1lriwdpN5dwCa092SxALJ49iDeR7pHcSuj/NUVpDCWjXjBCIiAbY7FZbF2OrqM2pu2y40q7b+hNO4iDI60EBmOikQqdNFPARZGqydrjvKSk4gkFgXVqyE1WsQPFaWBE6KaxqXUMUdl9t7bZKcTiag==',
  'buyer_id' => '2088102174862582',
  'invoice_amount' => '3.70',
  'notify_id' => '2021012900222101057062580512316700',
  'fund_bill_list' => '[{"amount":"3.70","fundChannel":"ALIPAYACCOUNT"}]',
  'notify_type' => 'trade_status_sync',
  'trade_status' => 'TRADE_SUCCESS',
  'receipt_amount' => '3.70',
  'buyer_pay_amount' => '3.70',
  'app_id' => '2016081500253620',
  'sign_type' => 'RSA2',
  'seller_id' => '2088102171320393',
  'gmt_payment' => '2021-01-29 10:10:56',
  'notify_time' => '2021-01-29 10:10:57',
  'version' => '1.0',
  'out_trade_no' => 'bc1611886241841635',
  'total_amount' => '3.70',
  'trade_no' => '2021012922001462580501364421',
  'auth_app_id' => '2016081500253620',
  'buyer_logon_id' => 'miu***@sandbox.com',
  'point_amount' => '0.00',
) 

注意事项3、账号设置

1 在商家中心 https://b.alipay.com 申请签约相关产品

2 在开放平通应用中心创建相应应用

https://open.alipay.com

3 把应用与商户号签约的产品进行绑定

4 在开放平台相关应用中为该应用“添加能力”,并签约