PayNotifyCallBack.php 3.26 KB
<?php

namespace WebPlugin\Pay\weixin;

use LibModels\Web\Home\OrderData;
use LibModels\Web\Product\PayData;
use WebPlugin\Pay\weixin\lib\WxPayApi;
use WebPlugin\Pay\weixin\lib\WxPayNotify;
use WebPlugin\Pay\weixin\lib\WxPayOrderQuery;
use WebPlugin\UdpLog;

class PayNotifyCallBack extends WxPayNotify
{
    // 获取微信订单中的orderCode需要截取的开始位置
    const ORDER_CODE_START = 8;

    private $_uid;

    /**
     * PayNotifyCallBack constructor.
     * @param int $uid
     */
    public function __construct($uid)
    {
        $this->_uid = $uid;
    }


    //查询订单
    public function Queryorder($transaction_id)
    {
        $input = new WxPayOrderQuery();
        $input->SetTransaction_id($transaction_id);
        UdpLog::info("【{$this->logProjectPrefix}-微信扫码支付】,function:Queryorder,微信扫码支付交易号", $transaction_id);
        $result = WxPayApi::orderQuery($input);
        UdpLog::info("【{$this->logProjectPrefix}-微信扫码支付】,function:Queryorder,扫码订单结果", $result);

        if (array_key_exists("return_code", $result) && array_key_exists("result_code", $result) && $result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS") {
            $orderCode = substr($result['out_trade_no'], self::ORDER_CODE_START);
            $payment = 21; // 微信扫码支付
            $bankName = 'WX';
            $bankCode = '';
            $amount = $result['total_fee'] / 100;
            $tradeNo = $result['out_trade_no'];
            $bankBillNo = $result['bank_type'];

            $orderInfo = OrderData::getOrderDetail('', $orderCode);
            UdpLog::info("【{$this->logProjectPrefix}-微信扫码支付】,orderCode:{$orderCode},function:Queryorder,订单详情", $orderInfo);

            if (!isset($orderInfo['data']) || empty($orderInfo['data'])) {
                UdpLog::error("【{$this->logProjectPrefix}-微信扫码支付】,orderCode:{$orderCode},function:Queryorder,微信支付状态同步中未获取到订单详情信息", $orderInfo);
                return false;
            }
            
            //更新订单状态
            $updateOrderStatus = PayData::pcpayNotify($orderCode, $payment, $amount, $bankName, $bankCode, $tradeNo, $bankBillNo);
            UdpLog::info("【{$this->logProjectPrefix}-微信扫码支付】,orderCode:{$orderCode},function:Queryorder,更新订单状态结果", $updateOrderStatus);
            return true;
        }

        return false;
    }

    //重写回调处理函数
    public function NotifyProcess($data, &$msg)
    {
        UdpLog::info("【{$this->logProjectPrefix}-微信扫码支付】,function:NotifyProcess,参数", $data);
        if (!array_key_exists("transaction_id", $data)) {
            $msg = "输入参数不正确";
            UdpLog::info("【{$this->logProjectPrefix}-微信扫码支付】,function:NotifyProcess,输入参数不正确", $data);
            return false;
        }
        //查询订单,判断订单真实性
        if (!$this->Queryorder($data["transaction_id"])) {
            $msg = "订单查询失败";
            UdpLog::info("【{$this->logProjectPrefix}-微信扫码支付】,function:NotifyProcess,订单查询失败", $data);
            return false;
        }

        return true;
    }

}