Service.php 5.25 KB
<?php

namespace WebPlugin\Pay\Kuaiqian;

use WebPlugin\Pay\PayAbstract;
use WebPlugin\Pay\Reqparams;

class Service extends PayAbstract
{
	
	var $config ;
	
	function __construct(array $paymentParams)
	{
		$this->config = new Config();
		
		$myConfig = json_decode($paymentParams["pay_params"]) ;
		$this->config->merchantAcctId = $myConfig->merchant_id;
		$this->config->sp_key = $myConfig->merchant_key;		
	}
	
	public function getPayRequestPars(Reqparams $params)
    {
        $signText = '';

		//先添加不可为空的参数
		$signText .= "inputCharset=" . $this->config-> inputCharset;
		$signText .= "&bgUrl=" . $this->baseNoticeUrl . $this->config->bgUrl;
		$signText .=  "&version=" . $this->config->version;
		$signText .= "&language=" . $this->config->language;
		$signText .= "&signType=" . $this->config->signType;
		$signText .= "&merchantAcctId=" . $this->config-> merchantAcctId;
		$signText .= "&orderId=" . $params->orderCode;
		$signText .= "&orderAmount=" . $params->totalFee;
		$signText .= "&orderTime=" . date("YmdHis");
		$signText .= "&payType=" . $this->config->payType;
		$signMsg = strtoupper(md5($signText . "&key=" . $this->config->sp_key));
		
		return array(
			'pay_url' => $this->config->pay_url,
			'pars' => $signText . "&signMsg=" . $signMsg,
			'reqType' => 'get'
		); 		
	}

    /**
     * (non-PHPdoc)
     * @see QPay_Utils_Abstract::parseResponse()
     * @param array $arrResponse
     * @return void|QCPay_Utils_Rspparams
     */
	public function parseResponse(array $arrResponse)
	{		
		$rsp = new QCPay_Utils_Rspparams();
		if(!$this->checkResponse($arrResponse))
		{
			//验证不成功
			$rsp->payResult = -1; 
		}
		else
		{
			$rsp->bankName = $arrResponse["bankId"];
			$rsp->orderCode = $arrResponse["orderId"];
			$rsp->payResult = $this->convertResult($arrResponse["payResult"]);
			$rsp->payTime = $this->convertTime($arrResponse["orderTime"]);
			$rsp->totalFee = $arrResponse["orderAmount"];
			$rsp->resultMsg = "";
			//添加支付订单号和交易号
			$rsp->payOrderCode = $arrResponse["orderId"];
			$rsp->tradeNo = "";
			$rsp->bankBillNo = "";
		}
		return $rsp;		
	}

    /**
     * 转化时间
     * Enter description here ...
     * @param string $payTime
     * @return int
     */
	private function convertTime($payTime)
	{
		$strTime = substr($payTime, 0, 4) . '-' . substr($payTime, 4, 2) . '-' . substr($payTime, 6, 2) . ' ' . 
			substr($payTime, 8, 2) . ':' . substr($payTime, 10, 2) . ':' . substr($payTime, 12, 2);
		return strtotime($strTime);
	}

    /**
     * 转换结果
     * Enter description here ...
     * @param string $resultCode
     * @return int|void
     */
	protected function convertResult($resultCode)
	{
		if($resultCode == '10')
		{
			return 200;
		}
		return 400;
	}
	
	/**
	 * 验证回复的正确性
	 * @see QPay_Utils_Abstract::verifResponse()
	 */
	protected function checkResponse(array $arrResponse)
	{
        $merchantSignMsgVal = '';

		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"merchantAcctId",$arrResponse["merchantAcctId"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"version",$arrResponse["version"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"language",$arrResponse["language"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"signType",$arrResponse["signType"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"payType",$arrResponse["payType"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"bankId",$arrResponse["bankId"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"orderId",$arrResponse["orderId"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"orderTime",$arrResponse["orderTime"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"orderAmount",$arrResponse["orderAmount"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"dealId",$arrResponse["dealId"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"bankDealId",$arrResponse["bankDealId"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"dealTime",$arrResponse["dealTime"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"payAmount",$arrResponse["payAmount"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"fee",$arrResponse["fee"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"ext1",$arrResponse["ext1"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"ext2",$arrResponse["ext2"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"payResult",$arrResponse["payResult"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"errCode",$arrResponse["errCode"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"key",$this->config->sp_key);
		return strtoupper(md5($merchantSignMsgVal)) == $arrResponse["signMsg"] ? true :false;
	}
	
	private function appendParam($returnStr, $paramId, $paramValue) {
		
		if ($returnStr != "") {			
			if ($paramValue != "") {				
				$returnStr .= "&" . $paramId . "=" . $paramValue;
			}		
		} else {			
			If ($paramValue != "") {
				$returnStr = $paramId . "=" . $paramValue;
			}
		}		
		return $returnStr;
	}
	
	
}