Service.php 4.53 KB
<?php

namespace WebPlugin\Pay\Unionpayweb;

use WebPlugin\Pay\PayAbstract;
use WebPlugin\Pay\Reqparams;
use WebPlugin\Pay\Rspparams;
use WebPlugin\PhpLog;
use WebPlugin\Helpers;
use Api\Yohobuy;

class Service extends PayAbstract
{
    private $merId;
    private $log;

    public function __construct(array $paymentParams)
    {
        $env = (APPLICATION_ENV === 'production' || APPLICATION_ENV === 'preview') ? 'release' : 'test';
        include_once 'conf/' . $env . '/SDKConfig.php';
        include_once 'Func/common.php';
        include_once 'Func/secureUtil.php';

        $this->log = new PhpLog(SDK_LOG_FILE_PATH, "PRC", SDK_LOG_LEVEL);
        $this->merId = array(
            'mobile' => '898111453110482',//手机支付
            'pc_nocard' => '898111453110464',//无卡支付
            'pc_bank' => '898111453110466'//网银支付
        );
    }

    /**
     * 解析返回的参数
     *
     * @param array $package
     * @return bool|string
     */
    public function parseResponse(array $package)
    {
        $this->log->LogInfo("===开始解析银联支付的回调参数===");
        $this->log->LogInfo("===回调参数为===");
        $this->log->LogInfo(var_export($package, true));

        $verify = false;
        $responseData = new Rspparams();

        if (!isset($package['respCode']) || $package['respCode'] !== '00') {
            $this->log->LogInfo('银联支付返回码有误');
            $responseData->payResult = -1;
            return $responseData;
        }

        if (isset($package['signature'])) { // TODO isset($package['signature']) && verify($package)验证签名待做
            $verify = true;
            $this->log->LogInfo('银联支付验签成功');
        }

        if ($verify) {
            //更改订单状态
            $responseData->orderCode = $package['orderId'];
            $responseData->payResult = 200;
            $responseData->payTime = time();
            $responseData->totalFee = $package['txnAmt'] * 0.01;
            $responseData->bankName = '';
            $responseData->bankCode = '';
            //添加支付订单号和交易号
            $responseData->payOrderCode = $package['orderId'];
            $responseData->tradeNo = $package['queryId'];
            $responseData->bankBillNo = "";
        } else {
            $responseData->payResult = -1;
        }

        $this->log->LogInfo("===银联支付的回调参数处理结果为===");
        $this->log->LogInfo(var_export($responseData, true));

        return $responseData;
    }

    public function getPayRequestPars(Reqparams $params)
    {
        $requestParams = array(
            'version' => '5.0.0',                //版本号
            'encoding' => 'utf-8',                //编码方式
            'certId' => getSignCertId(),            //证书ID
            'txnType' => '01',                //交易类型
            'txnSubType' => '01',                //交易子类
            'bizType' => '000201',                //业务类型
            'frontUrl' => Helpers::url('/pay/payreturn/unionpaywebreturn', array('order_code' => $params->orderCode)), //前台通知地址--同步地址
            'backUrl' => Yohobuy::SERVICE_URL . '/payment/unionpay_newnotify',//后台通知地址--异步地址
            'signMethod' => '01',        //签名方法
            'channelType' => '07',        //渠道类型,07-PC,08-手机
            'accessType' => '0',        //接入类型
            'merId' => $this->merId['pc_bank'],     //商户代码,请改自己的测试商户号
            'orderId' => $params->orderCode,    //商户订单号
            'txnTime' => date('YmdHis', $params->orderTime),    //订单发送时间
            'txnAmt' => $params->totalFee,        //交易金额,单位分
            'currencyCode' => '156',    //交易币种
            'defaultPayType' => '0201',    //默认支付方式
            //'orderDesc' => '订单描述',  //订单描述,网关支付和wap支付暂时不起作用
            'reqReserved' => $params->goodsName //请求方保留域,透传字段,查询、通知、对账文件中均会原样出现
        );
        // 签名
        sign($requestParams);
        // 前台请求地址
        $result = array(
            'pay_url' => SDK_FRONT_TRANS_URL,
            'pars' => $requestParams,
            'reqType' => 'post'
        );

        $this->log->LogInfo(var_export($requestParams, true));

        return $result;
    }

}