Service.php 4.68 KB
<?php

namespace WebPlugin\Pay\Tenpay;

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

class Service extends PayAbstract
{

    public $config;
    private $log;

    /**
     * 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;

        $this->log = new PhpLog($this->config->logDir, 'PRC', $this->config->logLevel);
    }

    /**
     * @param Reqparams $params
     * @return array
     */
    public function getPayRequestPars(Reqparams $params)
    {
        $this->log->LogInfo("===开始处理财付通的请求参数===");
        $this->log->logInfo("-----请求参数为---");
        $this->log->logInfo(var_export($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'
        );

        return $result;
    }

    /**
     * 解析返回的参数
     * @param Array $arrResponse
     * @return void|Rspparams
     */
    public function parseResponse(array $arrResponse)
    {
        $this->log->LogInfo("===开始处理财付通的回调参数===");
        $this->log->logInfo("-----回调参数为---");
        $this->log->logInfo(var_export($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 = "";
        }
        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;
    }

}