Notify.php 2.2 KB
<?php

namespace app\index\controller;

use addons\wechat\library\Config as ConfigService;
use app\common\controller\Frontend;
use EasyWeChat\Foundation\Application as WXPAY_APP;
use think\Db;

/**
 * 支付回调
 * @internal
 */
class Notify extends Frontend
{

    protected $noNeedLogin = ['notifyHouseJoin'];
    protected $noNeedRight = ['*'];
    protected $layout = '';
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 社区活动报名支付回调
     */
    public function notifyHouseJoin(){
        $app = new WXPAY_APP(ConfigService::load());
        $response = $app->payment->handleNotify(function($notify, $successful){
            /*这里是支付回调逻辑处理,一下是DEMO*/
            // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
            $out_trade_no=$notify->out_trade_no;
            $info = Db::name('house_join')->where(['order_no'=>$out_trade_no])->find();
            if (!$info) { // 如果订单不存在
                return 'Order not exist.'; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
            }

            // 检查订单是否已经更新过支付状态
            if  ($info['transaction_id']) { // 假设订单字段“支付时间”不为空代表已经支付
                return true; // 已经支付成功了就不再更新了
            }

            // 用户是否支付成功
            if ($successful) {
                Db::startTrans();
                // 回填微信的订单号
                $update['transaction_id'] = $notify->transaction_id;
                $update['pay_status'] = '1'; //付款成功
                $update['pay_time'] = time();
                $update['join_status'] = '1'; //报名成功
                //更新状态: 已购买
                $res_order = Db::name('house_join')->where(['id' => $info['id']])->update($update);
                if(!$res_order) {
                    Db::rollback();
                    return false; // 返回处理完成
                }
                Db::commit();
            }

            return true; // 返回处理完成
        });

        $response->send();
    }

}