PayNotifyCallBack.php 3.75 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\PhpLog;

class PayNotifyCallBack extends WxPayNotify
{
    // 向ERP提交订单状态有关接口调用日志和更新订单状态有关接口调用日志
    const ORDER_STATUS_LOG = '/Data/logs/pc_pay/order_status';
    // 日志等级,2表示记录信息等级的日志
    const LOG_LEVEL = 2;
    // 获取微信订单中的订单号需要截取的开始位置
    const ORDER_CODE_START = 8;

    private $log = null;
    private $_uid;

    /**
     * PayNotifyCallBack constructor.
     * @param int $uid
     */
    public function __construct($uid)
    {
        // 初始化日志
        $this->log = new PhpLog(self::ORDER_STATUS_LOG, 'PRC', self::LOG_LEVEL);
        $this->_uid = $uid;
    }


    //查询订单
    public function Queryorder($transaction_id)
    {
        $input = new WxPayOrderQuery();
        $input->SetTransaction_id($transaction_id);
        $this->log->LogInfo('微信扫码支付交易号:' . $transaction_id);
        $result = WxPayApi::orderQuery($input);

        $this->log->LogInfo('begin PayNotifyCallBack->Queryorder');
        $this->log->LogInfo('===开始查询微信扫码订单=====');
        $this->log->LogInfo(var_export($result, true));

        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'];

            $this->log->LogInfo('==[' . $orderCode . ']=开始调用获取订单详情方式接口,接口方法为app.SpaceOrders.detail===');
            $orderInfo = OrderData::getOrderDetail('', $orderCode);
            $this->log->LogInfo('==[' . $orderCode . ']=结束调用获取订单详情方式接口,返回结果为===');
            $this->log->LogInfo(var_export($orderInfo, true));

            if (!isset($orderInfo['data']) || empty($orderInfo['data'])) {
                $this->log->LogInfo('==[' . $orderCode . ']=微信支付状态同步中未获取到订单详情信息===');
                return false;
            }
            
            //更新订单状态
            $updateOrderStatus = PayData::pcpayNotify($orderCode, $payment, $amount, $bankName, $bankCode, $tradeNo, $bankBillNo);
            $this->log->LogInfo(var_export($updateOrderStatus, true));
            $this->log->LogInfo('==[' . $orderCode . ']=微信扫码订单支付成功===');

            return true;
        }

        return false;
    }

    //重写回调处理函数
    public function NotifyProcess($data, &$msg)
    {
        $this->log->LogInfo('begin notifyProcess');
        $this->log->LogInfo(var_export($data, true));

        if (!array_key_exists("transaction_id", $data)) {
            $msg = "输入参数不正确";
            $this->log->LogInfo('=====微信扫码支付通知结果为:' . $msg . '=====');
            return false;
        }
        //查询订单,判断订单真实性
        if (!$this->Queryorder($data["transaction_id"])) {
            $msg = "订单查询失败";
            $this->log->LogInfo('=====微信扫码支付通知结果为:' . $msg . '=====');
            return false;
        }

        $this->log->LogInfo('=====微信扫码支付通知结果为:' . $msg . '=====');

        return true;
    }

}