Service.php 4.61 KB
<?php

namespace WebPlugin\Pay\Unionpayweb;

use WebPlugin\Pay\PayAbstract;
use WebPlugin\Pay\Reqparams;
use WebPlugin\Pay\Rspparams;
use WebPlugin\UdpLog;
use WebPlugin\Helpers;

class Service extends PayAbstract
{
    private $merId;

    public function __construct(array $paymentParams)
    {
        include_once 'conf/release/SDKConfig.php';
        include_once 'Func/common.php';
        include_once 'Func/secureUtil.php';

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

    /**
     * 解析返回的参数
     *
     * @param array $package
     * @return bool|string
     */
    public function parseResponse(array $package)
    {
        UdpLog::info("【{$this->logProjectPrefix}-银联支付】,function:parseResponse,参数", $package);
        $verify = false;
        $responseData = new Rspparams();

        //https://open.unionpay.com/ajweb/help/faq/list?id=234&level=0&from=0&keyword=respCode 状态码
        if (empty($package['respCode']) || in_array($package['respCode'], array('00', 'A6')) === false) {
            $responseData->payResult = -1;
            return $responseData;
        }

        //去除为空的数组
        foreach ($package as $key => $value) {
            if ($value === '') {
                unset($package[$key]);
            }
        }

        if (verify($package)) {
            //验证签名合法性
            $verify = true;
        }

        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;
        }

        UdpLog::info("【{$this->logProjectPrefix}-银联支付】,function:parseResponse,处理结果", $responseData);

        return $responseData;
    }

    //https://open.unionpay.com/ajweb/help/faq/list?id=38&level=0&from=0
    public function getPayRequestPars(Reqparams $params)
    {
        // $front_notify_url = SDK_FRONT_NOTIFY_URL . '?order_code=' . $params->orderCode;
        $requestParams = array(
            'version' => '5.0.0',                //版本号
            'encoding' => 'utf-8',                //编码方式
            'certId' => getSignCertId(),            //证书ID
            'txnType' => '01',                //交易类型
            'txnSubType' => '01',                //交易子类
            'bizType' => '000201',                //业务类型
            'frontUrl' => Helpers::getHttpOrHttps() . Helpers::url(SDK_FRONT_NOTIFY_URL), //前台通知地址--同步地址
            'backUrl' => SERVICE_NOTIFY . SDK_BACK_NOTIFY_URL,//后台通知地址--异步地址
            '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支付暂时不起作用
            // 'payTimeout' => '',//超时时间
            'reqReserved' => $params->goodsName //请求方保留域,透传字段,查询、通知、对账文件中均会原样出现
        );
        // 签名
        sign($requestParams);
        // 前台请求地址
        $result = array(
            'pay_url' => SDK_FRONT_TRANS_URL,
            'pars' => $requestParams,
            'reqType' => 'post'
        );

        UdpLog::info("【{$this->logProjectPrefix}-银联支付】,orderCode:{$params->orderCode},function:getPayRequestPars,处理结果", $result);
        return $result;
    }

}