TP5 實現微信支付和支付寶支付 1、微信支付 2、支付寶支付

1、微信支付

1.1、安裝依賴包
  • 我這裏使用 EasyWeChat 實現微信支付,另外我附上之前我封裝的微信類。
  • 詳細操作裏面寫的比較清楚,請先看這個:https://www.jianshu.com/p/d376d921bf16
1.2、準備工作

(1)需要去微信開放平臺註冊賬號

  • appid(微信開放平臺上的應用id)
  • mch_id(微信申請成功之後郵件中的商戶id)
  • notify_url(支付成功後的回調地址)
  • api_key(在微信商戶平臺上自己設定的api密鑰 32位)
  • apiclient_cert.pem 和 apiclient_key.pem 證書

相關的申請流程可以參考網站: https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Pay/Vendor_Service_Center.html

(2)將對應的配置寫入配置文件中(配置文件可以參考我下面封裝的微信類)

(3)創建訂單表

  • 關於訂單表,這裏給你們參考下我的
1.3、調用微信支付
$app = (new WeChatService())->connect(2);
$result = $app->order->unify([
    'body' => $subject,
    'out_trade_no' => $orderNumber,
    'total_fee' => $amount * 100, // **單位:分**
    'spbill_create_ip' => '', // 可選,如不傳該參數,SDK 將會自動獲取相應 IP 地址
    'notify_url' => Env::get('web.host') . '/api/vip/wechatNotify', // 支付結果通知網址,如果不設置則會使用配置裏的默認地址
    'trade_type' => 'JSAPI', // 請對應換成你的支付方式對應的值類型
    'openid' => $user['openid'], //從用戶額外表中取到openId
]);
$res = json_decode($result, 1)['return_msg'];
if ($res == 'OK') $this->success(MSG_OK,$result); //支付成功
$this->error('支付失敗'); //支付失敗
  • 後端 只需要在調用微信支付地方調用這段方法即可。
  • H5 支付,公衆號支付,掃碼支付,支付中籤約,全部都是用這個接口下單。
  • 前端 微信支付參考文檔 https://pay.weixin.qq.com/wiki/doc/api/index.html
  • 我們要根據前端需要的參數進行返回。
1.4、微信支付異步操作(微信支付同步不需要我們做處理)
/**
 * @ApiTitle    (微信異步支付會員VIP)
 * @ApiRoute    (/api/Vip/wechatNotify)
 * @ApiInternal
 */
public function wechatNotify()
{
    $app = (new WeChatService())->connect(1);

    $response = $app->handlePaidNotify(function ($message, $fail) {

        $type = input('type') ?? 1; //類型:1、開通/續費VIP
        $outTradeNo = $message['out_trade_no']; //自定義訂單號
        //查詢是否存在訂單
        $res = (new UserAccountModel)->where('order_number', $outTradeNo)->find();

        // 如果訂單不存在 或者 訂單已經支付過了
        if (!$res || $res->pay_time) return true;

        if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信狀態,不代表支付狀態

            $tradeNo = $message['transaction_id']; //微信支付訂單號
            $totalFee = $message['total_fee']; //充值總金額
            $timeEnd = $message['time_end']; //支付完成時間

            //如果金額不匹配直接退出
            if (($res->money) != $totalFee / 100) return true;

            if ($message['result_code'] === 'SUCCESS') {

                //支付成功
                //更新資金錶狀態
                (new UserAccountModel)->where('order_number', $outTradeNo)->update([
                    'trade_no' => $tradeNo, //支付時間
                    'pay_time' => $timeEnd, //支付時間
                    'pay_status' => 1, //支付狀態:0=未到賬,1=已到賬
                ]);

                //開通/升級VIP
                if ($type == 1) $this->vipSuccess($res['from_id'], $outTradeNo);

            }
        } else {
            return $fail('通信失敗,請稍後再通知我');
        }
        return true;
    });

    $response->send(); // Laravel 裏請使用:return $response;
}
  • 根據訂單號,我們需要判斷訂單相關內容。
  • 返回碼也成功的狀態下,我們再去進行判斷,成功則進行相關的業務操作。
  • 其中 vipSuccess 是我封裝的支付成功的操作方法。
  • 另外根據 type 進行不同的操作,這個微信異步方法就可以通用了。
1.5、開放獲取支付狀態的接口
public function getPayStatus()
{
    $orderNumber = input('order_number'); //內部訂單流水號
    $order = (new UserAccountModel)->where('order_number', $orderNumber)->find();
    if (!$order) $this->error('訂單不存在!');
    $status = $order['pay_status']; //支付狀態:0=待支付,1=支付成功
    if (!$status) $this->error('訂單尚未支付成功!');
    $this->success('訂單支付成功!');
}
  • 該接口需要給前端用於判斷支付狀態。

2、支付寶支付

2.1、安裝依賴包
  • 我這裏使用的是 yansongda/pay

  • composer 安裝命令

composer require yansongda/pay
  • 支持
    method 描述
    web 電腦支付
    wap 手機網站支付
    app APP 支付
    pos 刷卡支付
    scan 掃碼支付
    transfer 帳戶轉賬
    mini 小程序支付
2.2、準備工作

(1)申請支付開放平臺並填寫資料 https://open.alipay.com/platform/home.htm
(2)填寫配置文件,如下

//支付寶支付設置
'alipay' => [
    'app_id' => '20160926005xxxxxx',
    'notify_url' => 'http://yansongda.cn/notify.php',
    'return_url' => 'http://yansongda.cn/return.php',
    'ali_public_key' => '',
    'private_key' => '',
    // 使用公鑰證書模式,請配置下面兩個參數,同時修改ali_public_key爲以.crt結尾的支付寶公鑰證書路徑,如(./cert/alipayCertPublicKey_RSA2.crt)
    // 'app_cert_public_key' => './cert/appCertPublicKey.crt', //應用公鑰證書路徑
    // 'alipay_root_cert' => './cert/alipayRootCert.crt', //支付寶根證書路徑
    'log' => [ // optional
        'file' => './logs/alipay.log',
        'level' => 'info', // 建議生產環境等級調整爲 info,開發環境爲 debug
        'type' => 'single', // optional, 可選 daily.
        'max_file' => 30, // optional, 當 type 爲 daily 時有效,默認 30 天
    ],
    'http' => [ // optional
        'timeout' => 5.0,
        'connect_timeout' => 5.0,
        // 更多配置項請參考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
    ],
    'mode' => 'dev', // optional,設置此參數,將進入沙箱模式
],
2.3、調用支付寶支付
//訂單內容
$order = [
    'out_trade_no' => $orderNumber,
    'total_amount' => $amount,
    'subject' => $subject,
];
$alipay = Pay::alipay(config('alipay'))->web($order); //網頁支付
//$alipay = Pay::alipay(config('alipay'))->app($order); //app支付

return $alipay->send();// laravel 框架中請直接 `return $alipay`
  • 其中封裝了方法,我們只要根據不同端調用不同的方法即可。
2.4、支付寶同步
/**
 * @ApiTitle    (支付寶同步接口)
 * @ApiRoute    (/api/Payment/alipayReturn)
 * @ApiInternal
 */
public function alipayReturn()
{
    $data = Pay::alipay(config('alipay'))->verify(); // 是的,驗籤就這麼簡單!

    // 訂單號:$data->out_trade_no
    // 支付寶交易號:$data->trade_no
    // 訂單總金額:$data->total_amount
    // 訂單號:$data->out_trade_no
    // 支付寶交易號:$data->trade_no
    // 訂單總金額:$data->total_amount

    Db::startTrans();
    try {

        //驗證數據
        $outTradeNo = $data->out_trade_no;
        $totalAmount = $data->total_amount;
        $tradeNo = $data->trade_no;
        $appId = $data->app_id;

        $res = (new UserAccountModel)->where('order_number', $outTradeNo)->find();
        if (!$res) $this->error('未找到該充值訂單!');
        if ($res['money'] != $totalAmount) $this->error('充值訂單金額異常!');
        if ($appId != config('alipay.app_id')) $this->error('充值支付平臺異常!');

        Db::commit();

    } catch (\Exception $e) {
        Db::rollback();
        $this->error($e->getMessage());
    }

    $data = [
        'order_number' => $outTradeNo, //訂單流水號
        'trade_no' => $tradeNo, //支付寶/微信訂單號
        'total_amount' => $totalAmount, //資金金額
    ];

    $this->success('操作成功,等待充值結果!', $data);
}
  • 同步接口是統一的,通用的。
2.5、支付寶異步操作
/**
 * @ApiTitle    (支付寶異步接口)
 * @ApiRoute    (/api/Payment/vipAliNotify)
 * @return \Symfony\Component\HttpFoundation\Response
 * @ApiAuthor  (黃育華 2020/3/9)
 * @ApiInternal
 */
public function alipayNotify()
{
    $alipay = Pay::alipay(config('alipay'));
    $type = input('type') ?? 1; //類型:1、開通/續費VIP

    Db::startTrans();
    try {
        $data = $alipay->verify(); // 是的,驗籤就這麼簡單!

        $state = $data->trade_status; //訂單狀態
        $outTradeNo = $data->out_trade_no; //自定義訂單號
        $tradeNo = $data->trade_no; //支付寶訂單號
        $totalAmount = $data->total_amount; //充值總金額
        $appId = $data->app_id; //收款方的APPID

        //獲取對應訂單的資金流水信息
        $res = (new UserAccountModel)->where('order_number', $outTradeNo)->find();

        // 請自行對 trade_status 進行判斷及其它邏輯進行判斷,在支付寶的業務通知中,只有交易通知狀態爲 TRADE_SUCCESS 或 TRADE_FINISHED 時,支付寶纔會認定爲買家付款成功。
        // 1、商戶需要驗證該通知數據中的out_trade_no是否爲商戶系統中創建的訂單號;
        // 2、判斷total_amount是否確實爲該訂單的實際金額(即商戶訂單創建時的金額);
        // 3、校驗通知中的seller_id(或者seller_email) 是否爲out_trade_no這筆單據的對應的操作方(有的時候,一個商戶可能有多個seller_id/seller_email);
        // 4、驗證app_id是否爲該商戶本身。
        // 5、其它業務邏輯情況。
        if (!in_array($state, ['TRADE_SUCCESS', 'TRADE_FINISHED'])) return $alipay->success()->send();
        if (!$res) return $alipay->success()->send();
        if ($res['money'] != $totalAmount) return $alipay->success()->send();
        if ($appId != config('alipay.app_id')) return $alipay->success()->send();

        //以下執行操作......
        //更新資金錶狀態
        (new UserAccountModel)->where('order_number', $outTradeNo)->update([
            'trade_no' => $tradeNo, //支付時間
            'pay_time' => time(), //支付時間
            'pay_status' => 1, //支付狀態:0=未到賬,1=已到賬
        ]);

        //開通/升級VIP
        if ($type == 1) $this->vipSuccess($res['from_id'], $outTradeNo);

        Log::debug('Alipay notify', $data->all());
        Db::commit();

    } catch (\Exception $e) {
        Db::rollback();
    }

    return $alipay->success()->send();// laravel 框架中請直接 `return $alipay->success()`
}

歡迎來指導和學習,如果有什麼問題可以在留言區留言並一起探討。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章