4

接上文: Laravel教程: 3分钟实现小程序微信支付接入(上)——唤起支付

编码

绑定回调路由 routes/api.php

Route::any('/gateway/wxpay/callback', 'PayController@callback')->name('pay.callback');

注意前一篇文章的添加 .env 与此处路由的 URL 要保持一致。:

WECHAT_PAYMENT_NOTIFY_URL=https://www.mysite.com/gateway/wxpay/callback

app/Http/Controllers/PayController.php 添加 callback 方法


    /**
     * 微信支付回调
     *
     * @return Response
     */
    public function callback(\App\Repositories\BuyRepository $buyRepository)
    {
        return $buyRepository->callback();
    }

app/Repositories/BuyRepository.php 添加 callback 逻辑


    public function callback()
    {
        $this->wxpay = app('easywechat.payment');
        return $this->wxpay->handlePaidNotify(
            function ($message, $fail) {
                if ($message['result_code'] === 'FAIL') {
                    logger()->warning('WXPAY_CALLBACK', ['FAIL', $message]);
                    return true;
                } else if ($message['return_code'] === 'SUCCESS') {
                    // TODO: 你的发货逻辑
                    return true;
                }
            }
        );
    }

ARGUS
1.6k 声望155 粉丝

便宜的SSL证书 www.ssl.com.ru