Notice.php 10.2 KB
<?php

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

/**
 * 支付Notice
 */
class NoticeController extends WebAction
{
    // 存储的UID键名
    const SESSION_UID_KEY = 'payUserid';    
    
    public function init() {
        parent::init();
    }
    
    /**
     * 支付宝后台服务器返回,主要是用于补单
     */
    public function alipaynoticeAction()
    {
        $res = $this->getParseResponse($_POST, 2);
        if ($res->payResult != -1) {
            $this->payResultProc($res, 2);
            echo "success";
        }
        exit();
    }

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

    /**
     * 支付宝二维码支付
     */
    public function alibarcodenoticeAction()
    {
        $res = $this->getParseResponse($_POST, 17);
        if ($res->payResult != -1) {
            $this->payResultProc($res, 17);
            echo "success";
        }
        exit();
    }

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

    /**
     * 通联支付
     */
    public function allinpaynoticeAction()
    {
        $res = $this->getParseResponse($_POST, 16);
        if ($res->payResult != -1) {
            $this->payResultProc($res, 16);
            echo "success";
        }
        exit();
    }

    /**
     * 通联支付返回
     */
    public function allinpayreturnAction()
    {
        $res = $this->getParseResponse($_POST, 16);
        $dealResult = $this->payResultProc($res, 16);
        $dealResult['payData'] = array('payWay' => '通联支付');
        $this->commonShowResult($dealResult);
    }

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

    /**
     * 支付宝快捷网关自动对账回复
     */
    public function aliexpressgatewaynoticeAction()
    {
        $res = $this->getParseResponse($_POST, 13);
        if ($res->payResult != -1) {
            $this->payResultProc($res, 13);
            echo "success";
        }
        exit();
    }

    /*
     * 支付宝银行支付回调(用于后台同步)
     */
    public function alibanknoticeAction()
    {
        $res = $this->getParseResponse($_POST, 12);
        if ($res->payResult != -1) {
            $this->payResultProc($res, 12);
            echo "success";
        }

        exit();
    }

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

    /**
     * 网银信用卡
     */
    public function chinabankAction()
    {
        $res = $this->getParseResponse($_POST, 4);
        $dealResult = $this->payResultProc($res, 4);
        $dealResult['payData'] = array('payWay' => '网银在线');
        $this->commonShowResult($dealResult);
    }

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

    /**
     * 腾讯财付通的回应
     */
    public function tenpayAction()
    {
        $res = $this->getParseResponse($_GET, 1);
        $dealResult = $this->payResultProc($res, 1);
        $dealResult['payData'] = array('payWay' => '财付通');
        $this->commonShowResult($dealResult);
    }

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

    /**
     * 盛付通服务器端返回
     */
    public function shengpaynoticeAction()
    {
        $res = $this->getParseResponse($_POST, 11);
        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()
    {
        $uid = $this->getUid();
        if (empty($uid)) { // cookie中获取为空时再从之前保存的session中获取
            $uid = $this->getSession(self::SESSION_UID_KEY);
            $this->setSession(self::SESSION_UID_KEY, null);
        }
        $notify = new PayNotifyCallBack($uid);
        $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()
    {
        $res = $this->getParseResponse($_REQUEST, 25);
        $dealResult = $this->payResultProc($res, 25);
        $dealResult['payData'] = array('payWay' => '银联在线支付');
        $this->commonShowResult($dealResult);
    }

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

        exit();
    }

    /**
     * 支付宝手机支付(极简)的返回
     */
    public function alimobilenoticeminiAction()
    {
        $res = $this->getParseResponse($_POST, 20);
        if ($res->payResult != -1) {
            $this->payResultProc($res, 20);
            echo "OK";
        } else {
            echo 'faile';
        }

        exit();
    }
    /**
     * 获取支付方式类对象
     * @param type array $Parameters 回调参数
     * @param type int $payment 支付方式
     * @return type object
     */
    private function getParseResponse($Parameters, $payment)
    {
        $payment = PayModel::getPaymentById($payment);
        $payService = PayFactory::factory($payment);
        $res = $payService->parseResponse($Parameters);

        if (empty($res->bankName)) {
            $res->bankName = empty($payment['payName']) ? '' : $payment['payName'];
        }

        if (empty($res->bankCode)) {
            $res->bankCode = empty($payment['pay_code']) ? '' : $payment['pay_code'];
        }

        return $res;
    }

    /**
     * 通用显示结果的方法
     * @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'],
                'criteo' => array(// 统计代码有关数据
                    'orderNum' => $dealResult['data']['orderNum'],
                    'items' => $dealResult['data']['items']
                )
            );
        }

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

    /**
     * 处理支付返回的数据
     *
     * @param Rspparams $payResult 支付返回的结果
     * @param $payment 支付方式id
     * @return array
     */
    private function payResultProc($payResult, $payment)
    {
        //判断是否登录
        $uid = $this->getUid();
        if (empty($uid)) { // cookie中获取为空时再从之前保存的session中获取
            $uid = $this->getSession(self::SESSION_UID_KEY);
            $this->setSession(self::SESSION_UID_KEY, null);
        }

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

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

        return $result;
    }
}