Notice.php 11.4 KB
<?php

use Action\WebAction;
use Shopping\PayModel;
use WebPlugin\Pay\PayFactory;
use WebPlugin\Pay\Rspparams;
use WebPlugin\Pay\weixin\PayNotifyCallBack;
use WebPlugin\PhpLog;

/**
 * 支付Notice
 */
class NoticeController extends WebAction
{

    // 日志等级,2表示记录信息等级的日志
    const LOG_LEVEL = 2;
    // 向回调通知有关调用日志
    const ORDER_STATUS_LOG = '/Data/logs/pc_pay/order_status';

    /**
     * 支付宝后台服务器返回,主要是用于补单
     */
    public function alipaynoticeAction()
    {
        $payment = PayModel::getPaymentById(2);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_POST); //支付宝通知使用的
        if ($res->payResult != -1) {
            $this->payResultProc($res, 2);
            echo "success";
        }
        exit();
    }

    /**
     * 支付宝浏览器端返回
     */
    public function alipayreturnAction()
    {
        $payment = PayModel::getPaymentById(2);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_GET);
        $dealResult = $this->payResultProc($res, 2);
        $dealResult['payData'] = array('payWay' => '支付宝');
        $this->commonShowResult($dealResult);
    }

    /**
     * 支付宝二维码支付
     */
    public function alibarcodenoticeAction()
    {
        $payment = PayModel::getPaymentById(17);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_POST); //支付宝通知使用的
        if ($res->payResult != -1) {
            $this->payResultProc($res, 17);
            echo "success";
        }
        exit();
    }

    /**
     * 支付宝二维码支付浏览器端返回
     */
    public function alibarcodereturnAction()
    {
        $payment = PayModel::getPaymentById(17);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_GET);
        $dealResult = $this->payResultProc($res, 17);
        $dealResult['payData'] = array('payWay' => '支付宝');
        $this->commonShowResult($dealResult);
    }

    /**
     * 通联支付
     */
    public function allinpaynoticeAction()
    {
        $payment = PayModel::getPaymentById(16);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_POST);
        if ($res->payResult != -1) {
            $this->payResultProc($res, 16);
            echo "success";
        }
        exit();
    }

    /**
     * 通联支付返回
     */
    public function allinpayreturnAction()
    {
        $payment = PayModel::getPaymentById(16);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_POST);
        $dealResult = $this->payResultProc($res, 16);
        $dealResult['payData'] = array('payWay' => '通联支付');
        $this->commonShowResult($dealResult);
    }

    /**
     * 支付宝快捷网关浏览器回复
     */
    public function aliexpressgatewayreturnAction()
    {
        $payment = PayModel::getPaymentById(13);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_GET);
        $dealResult = $this->payResultProc($res, 13);
        $dealResult['payData'] = array('payWay' => '支付宝');
        $this->commonShowResult($dealResult);
    }

    /**
     * 支付宝快捷网关自动对账回复
     */
    public function aliexpressgatewaynoticeAction()
    {
        $payment = PayModel::getPaymentById(13);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_POST); //支付宝通知使用的
        if ($res->payResult != -1) {
            $this->payResultProc($res, 13);
            echo "success";
        }
        exit();
    }

    /*
     * 支付宝银行支付回调(用于后台同步)
     */
    public function alibanknoticeAction()
    {
        $payment = PayModel::getPaymentById(12);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_POST);

        if ($res->payResult != -1) {
            $this->payResultProc($res, 12);
            echo "success";
        }

        exit();
    }

    /*
     * 支付宝银行支付回调(用于前台同步)
     */
    public function alibankreturnAction()
    {
        $payment = PayModel::getPaymentById(12);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_GET);
        $dealResult = $this->payResultProc($res, 12);
        $dealResult['payData'] = array('payWay' => '银行卡');
        $this->commonShowResult($dealResult);
    }

    /**
     * 网银信用卡
     */
    public function chinabankAction()
    {
        $payment = PayModel::getPaymentById(4);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_POST);
        $dealResult = $this->payResultProc($res, 4);
        $dealResult['payData'] = array('payWay' => '网银在线');
        $this->commonShowResult($dealResult);
    }

    /**
     * 网银在线自动接收
     */
    public function chinabankautorevAction()
    {
        $payment = PayModel::getPaymentById(4);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_POST);
        if ($res->payResult != -1) {
            $this->payResultProc($res, 4);
            echo "ok";
        } else {
            echo 'error';
        }
        exit();
    }

    /**
     * 腾讯财付通的回应
     */
    public function tenpayAction()
    {
        $payment = PayModel::getPaymentById(1);
        $payService = PayFactory::factory($payment);
        $rspParams = $payService->parseResponse($_GET); //财付通是以get方式回复的。
        $this->payResultProc($rspParams, 1);
    }

    /**
     * 盛付通浏览器返回
     * Enter description here ...
     */
    public function shengpayreturnAction()
    {
        $payment = PayModel::getPaymentById(11);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_POST);
        $dealResult = $this->payResultProc($res, 11);
        $dealResult['payData'] = array('payWay' => '盛付通');
        $this->commonShowResult($dealResult);
    }

    /**
     * 盛付通服务器端返回
     */
    public function shengpaynoticeAction()
    {
        $payment = PayModel::getPaymentById(11);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_POST);
        if ($res->payResult != -1) {
            $this->payResultProc($res, 11);
            echo "OK";
        }
        exit();
    }

    /**
     * jsonPcallback响应订单支付状态
     */
    public function wechatcallbackAction()
    {
        $callback = $this->get('callback');
        $orderCode = $this->get('ordercode');

        $uid = $this->getUid(true);
        $response = PayModel::checkWechatPayStatus($uid, $orderCode);

        $this->helpJsonCallbackResult($callback, $response['code'], $response['message']);
    }

    /**
     * 微信扫码支付结果商户通知地址
     */
    public function wechatqrcodenotifyAction()
    {
        $notify = new PayNotifyCallBack();
        $notify->handle(false);
    }

    /**
     * 微信扫码支付返回
     */
    public function wechatqrcodereturnAction()
    {
        $dealResult = array(
            'code' => 500,
            'message' => '支付失败'
        );

        do {
            $orderCode = isset($_GET['order_code']) ? $_GET['order_code'] : 0;
            if (empty($orderCode)) {
                break;
            }

            $payment = PayModel::getPaymentById(21);
            $payService = PayFactory::factory($payment);
            $res = $payService->parseResponse(array('orderCode' => $orderCode));
            $dealResult = $this->payResultProc($res, 21);
            $dealResult['payData'] = array('payWay' => '微信扫码支付');
        } while (false);

        $this->commonShowResult($dealResult);
    }

    /**
     * 银联web支付返回
     */
    public function unionpaywebreturnAction()
    {
        $payment = PayModel::getPaymentById(25);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_REQUEST);
        $dealResult = $this->payResultProc($res, 25);
        $dealResult['payData'] = array('payWay' => '银联在线支付');
        $this->commonShowResult($dealResult);
    }

    /**
     * 银联支付成功后台回调响应
     */
    public function unionpaywebnoticeAction()
    {
        $payment = PayModel::getPaymentById(25);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_REQUEST);
        if ($res->payResult != -1) {
            $this->payResultProc($res, 25);
            echo "OK";
        }

        exit();
    }

    /**
     * 支付宝手机支付(极简)的返回
     */
    public function alimobilenoticeminiAction()
    {
        $payment = PayModel::getPaymentById(20);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($_POST); //支付宝通知使用的
        if ($res->payResult != -1) {
            $this->payResultProc($res, 20);
            echo "OK";
        } else {
            echo 'faile';
        }

        exit();
    }


    /**
     * 通用显示结果的方法
     * @param array $dealResult
     */
    private function commonShowResult($dealResult)
    {
        $code = $dealResult['code'];
        $view = 'wechatqrcodereturn';
        $data = array(
            'headerData' => true,
            'payData' => $dealResult['payData']
        );

        if ($code == 200) {
            $view = 'index';
            $data = array('payNotice' => $dealResult['data']);
        }

        //头部导航
        $this->setSimpleHeader();
        $this->_view->display($view, $data);
    }

    /**
     * 处理支付返回的数据
     *
     * @param Rspparams $payResult 支付返回的结果
     * @param $payment 支付方式id
     * @return array
     */
    private function payResultProc($payResult, $payment)
    {
        // 初始化日志
        $log = new PhpLog(self::ORDER_STATUS_LOG, 'PRC', self::LOG_LEVEL);
        $log->LogInfo('================开始验证登录状态============');
        $log->LogInfo('================支付结果数据为============');
        $log->LogInfo(var_export($payResult, true));

        //判断是否登录
        $uid = $this->getUid();
        if (empty($uid)) { // cookie中获取为空时再从之前保存的session中获取
            $uid = $this->getSession('payUserid');
        }

        $log->LogInfo('================已登录,获取到用户ID============');

        $result = array(
            'code' => 500,
            'message' => '支付失败'
        );

        if ($payResult->payResult == 200) {
            //支付成功
            $result = PayModel::procOrderData($uid, $payResult, $payment);
        }

        $log->LogInfo('================支付结果为============');
        $log->LogInfo(var_export($result, true));

        return $result;
    }
}