<?php
/**
 *
 * example目录下为简单的支付样例，仅能用于搭建快速体验微信支付使用
 * 样例的作用仅限于指导如何使用sdk，在安全上面仅做了简单处理， 复制使用样例代码时请慎重
 * 请勿直接直接使用样例对外提供服务
 *
 **/


require_once "AliPay.Config.php";
require_once "AopClient.php";

use think\facade\Log;
use think\facade\Db;

class PayNotifyCallBack
{
    public function Handle($objData)
    {
        $this->NotifyProcess($objData);
    }

    //重写回调处理函数
    public function NotifyProcess($objData)
    {
        Log::write('支付宝支付回调参数：' . json_encode($objData));
        if (!empty($objData)) {
            $body = json_decode($objData['body'], true);
            $mch_id = $body['mch_id'];
            $pay_type = $body['pay_type'];
            $platform = $body['platform'];
            $GLOBALS['mch_id'] = $mch_id;
            $GLOBALS['pay_type'] = $pay_type;
            $GLOBALS['platform'] = $platform;
            $config = new \AliPayConfig();
            $aopClient = new \AopClient();
            $aopClient->appId = $config->GetAppId();
            $aopClient->rsaPrivateKey = $config->GetPrivateKey();
            $aopClient->alipayrsaPublicKey = $config->GetPublicKey();
            if (!$aopClient->rsaCheckV1($objData, '', 'RSA2')) {
                Log::write("支付宝签名错误...");
                echo "fail";
                return;
            }
            $out_trade_no = $objData['out_trade_no'];
            $sys_pay_notify = Db::name('sys_pay_notify')->where('order_number', $out_trade_no)->find();
            if (empty($sys_pay_notify)) {
                $pay_notify['mch_id'] = $mch_id;
                $pay_notify['order_number'] = $out_trade_no;
                $pay_notify['notify_message'] = json_encode($objData);
                $pay_notify['create_time'] = time();
                Db::name('sys_pay_notify')->insert($pay_notify);
                //支付回调
                $param = ['out_trade_no' => $out_trade_no];
                \iyibai\services\HookService::call('SysOrderHook', 'payBusinessCallBack', $param);
            }
            echo "success";
            return;
        } else {
            echo "fail";
            return;
        }
    }
}

