Service.php 4.66 KB
<?php

namespace WebPlugin\Pay\Tenpay;

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

class Service extends PayAbstract
{

    public $config;

    /**
     * Service constructor.
     * @param array $paymentParams
     */
    public function __construct(array $paymentParams)
    {
        $this->logProjectPrefix = 'tenpay';
        $this->config = new Config();
        $myConfig = json_decode($paymentParams["pay_params"]);
        $this->config->bargainor_id = $myConfig->merchant_id;
        $this->config->sp_key = $myConfig->merchant_key;
    }

    /**
     * @param Reqparams $params
     * @return array
     */
    public function getPayRequestPars(Reqparams $params)
    {
        UdpLog::info("【{$this->logProjectPrefix}-支付】,function:getPayRequestPars,参数", $params);
        parent::getPayRequestPars($params);
        $baseUrl = $this->getBaseNoticeUrl($params->isTest);

        $reqData = array();
        $sign_text = "cmdno=" . $this->config->cmdno;
        $sign_text .= "&date=" . date('Ymd');
        //	$sign_text .= "&bank_type=0";
        $goodName = urlencode(iconv("UTF-8", "GB2312", $params->goodsName));
        $sign_text .= "&bargainor_id=" . $this->config->bargainor_id;
        $sign_text .= "&transaction_id=" . $this->config->bargainor_id . date('Ymd') . date('mdHis');
        $sign_text .= "&sp_billno=" . $params->orderCode;  //内部订单号
        $sign_text .= "&total_fee=" . $params->totalFee;
        $sign_text .= "&fee_type=" . $this->config->fee_type;
        $sign_text .= "&return_url=" . $baseUrl . $this->config->return_url;
        $sign_text .= "&attach=" . $params->orderCode;
        if (!empty($params->spbill_create_ip)) {
            $sign_text .= "&spbill_create_ip=" . $params->spbill_create_ip;
        }

        $sign = strtoupper(md5($sign_text . "&key=" . $this->config->sp_key));

        $sign_text = $sign_text . "&sign=" . $sign . "&desc=" . $goodName . "&bank_type=0&cs=gb2312";
        $result = array(
            'pay_url' => $this->config->pay_url,
            'pars' => $sign_text,
            'reqType' => 'get'
        );
        UdpLog::info("【{$this->logProjectPrefix}-支付】,ordercode:{$params->orderCode},function:getPayRequestPars,处理结果", $result);
        return $result;
    }

    /**
     * 解析返回的参数
     * @param Array $arrResponse
     * @return void|Rspparams
     */
    public function parseResponse(array $arrResponse)
    {
        UdpLog::info("【{$this->logProjectPrefix}-支付】,function:parseResponse,参数", $arrResponse);
        $rsp = new Rspparams();
        if (!$this->checkResponse($arrResponse)) {
            //验证不成功
            $rsp->payResult = 400;
        } else {
            $rsp->bankName = "";
            $rsp->orderCode = $arrResponse["sp_billno"];
            $rsp->payResult = $this->convertResult($arrResponse["pay_result"]);
            $rsp->payTime = $arrResponse["pay_time"];
            $rsp->totalFee = round($arrResponse["total_fee"], 2) / 100;  //返回的是分,要转化成元
            $rsp->resultMsg = $arrResponse['pay_info'];
            //添加支付订单号和交易号
            $rsp->payOrderCode = $arrResponse["sp_billno"];
            $rsp->tradeNo = "";
            $rsp->bankBillNo = "";
        }
        UdpLog::info("【{$this->logProjectPrefix}-支付】,function:parseResponse,处理结果", $rsp);
        return $rsp;
    }

    /**
     * @param $resultCode
     * @return int|void
     */
    protected function convertResult($resultCode)
    {
        if ($resultCode == 0) {
            return 200;
        }
        return $resultCode;
    }

    /**
     * 验证回复的正确性
     * @param array $arrResponse
     * @return bool|void
     */
    protected function checkResponse(array $arrResponse)
    {
        $strResponseText = "cmdno=" . $arrResponse["cmdno"];
        $strResponseText .= "&pay_result=" . $arrResponse["pay_result"];
        $strResponseText .= "&date=" . $arrResponse["date"];
        $strResponseText .= "&transaction_id=" . $arrResponse["transaction_id"];
        $strResponseText .= "&sp_billno=" . $arrResponse["sp_billno"];
        $strResponseText .= "&total_fee=" . $arrResponse["total_fee"];
        $strResponseText .= "&fee_type=" . $arrResponse["fee_type"];
        $strResponseText .= "&attach=" . $arrResponse["attach"];
        $strResponseText .= "&key=" . $this->config->sp_key;

        if (strtoupper(md5($strResponseText)) == $arrResponse["sign"]) {
            return true;
        }
        return false;
    }

}