Service.php 4.01 KB
<?php

namespace WebPlugin\Pay\Unionpayweb;

use WebPlugin\Pay\PayAbstract;
use WebPlugin\Pay\Reqparams;
use WebPlugin\Pay\Rspparams;
use WebPlugin\PhpLog;

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)
    {
        $verify = false;
        if (isset($package ['signature']) && verify($package)) {
            $verify = true;
            $this->log->LogInfo('验签成功');
        } else {
            return false;
        }

        if ($package['respCode'] !== '00') {
            $this->log->LogInfo('支付返回码有误');
            return false;
        }

        $responseData = new Rspparams();
        if ($verify) {
            //更改订单状态
            $responseData->orderCode = $package['orderId'];
            $responseData->payResult = 0;
            $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;
        }

        return $responseData;
    }

    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' => $front_notify_url,        //前台通知地址
            'backUrl' => 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支付暂时不起作用
            '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;
    }

}