Service.php 5.91 KB
<?php

namespace WebPlugin\Pay\Kqshenzhou;

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

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)
    {
        $signMsgVal = '';
		
		$signMsgVal=$this->appendParam($signMsgVal,"inputCharset",$this->config->inputCharset);
		$signMsgVal=$this->appendParam($signMsgVal,"bgUrl", $this->baseNoticeUrl . $this->config->bgUrl);
		$signMsgVal=$this->appendParam($signMsgVal,"pageUrl",$this->config->pageUrl);
		$signMsgVal=$this->appendParam($signMsgVal,"version",$this->config->version);
		$signMsgVal=$this->appendParam($signMsgVal,"language",$this->config->language);
		$signMsgVal=$this->appendParam($signMsgVal,"signType",$this->config->signType);
		
		$signMsgVal=$this->appendParam($signMsgVal,"merchantAcctId",$this->config->merchantAcctId);
		$signMsgVal=$this->appendParam($signMsgVal,"payerName","");
		$signMsgVal=$this->appendParam($signMsgVal,"payerContactType","");
		$signMsgVal=$this->appendParam($signMsgVal,"payerContact","");
		
		$signMsgVal=$this->appendParam($signMsgVal,"orderId",$params->orderCode);
		$signMsgVal=$this->appendParam($signMsgVal,"orderAmount",$params->totalFee);
		$signMsgVal=$this->appendParam($signMsgVal,"payType",$this->config->payType);
		$signMsgVal=$this->appendParam($signMsgVal,"fullAmountFlag",$this->config->fullAmountFlag);
		$signMsgVal=$this->appendParam($signMsgVal,"orderTime", date("YmdHis"));
		$signMsgVal=$this->appendParam($signMsgVal,"productName",urlencode($params->goodsName));
		$signMsgVal=$this->appendParam($signMsgVal,"productNum","");
		$signMsgVal=$this->appendParam($signMsgVal,"productId","");
		$signMsgVal=$this->appendParam($signMsgVal,"productDesc",urlencode(""));
		$signMsgVal=$this->appendParam($signMsgVal,"ext1","");
		$signMsgVal=$this->appendParam($signMsgVal,"ext2","");
		$signMsgVal=$this->appendParam($signMsgVal,"key",$this->config->sp_key);
		$signMsg= strtoupper(md5($signMsgVal));

        $result =  array(
            'pay_url' => $this->config->pay_url,
            'pars' => $signMsgVal . "&signMsg=" . $signMsg,
            'reqType' => 'get'
        );

        return $result;
	}
	
	public function parseResponse(array $arrResponse)
	{
		$rsp = new Rspparams();
		if(!$this->checkResponse($arrResponse))
		{
			//验证不成功
			$rsp->payResult = -1; 
		}
		else
		{
			$rsp->bankName = $this->getPaytypeName($arrResponse["payType"]) ;
			$rsp->bargainorId = $arrResponse["merchantAcctId"];
			$rsp->orderId = $arrResponse["orderId"];
			$rsp->payResult = $this->convertResult($arrResponse["payResult"]);
			$rsp->payTime = $arrResponse["billOrderTime"];
			$rsp->totalFee = $arrResponse["payAmount"];
		}
		return $rsp;
	}

    /**
     * 获取支付方式名称
     * @return string
     */
	private function getPaytypeName()
	{
		$res = "";
		switch($res)
		{
			case "00":
				return "神州行卡密支付和快钱账户支付";
			case "41":
				return "快钱账户支付";
			case "42":
				return "神州行卡密支付和快钱账户支付";
			case "52":
				return "神州行卡密支付";
		}
	}

    /**
     * @param $resultCode
     * @return int|void
     */
	protected function convertResult($resultCode)
	{
		return $resultCode == "10" ? 0 : 1;
	}
	
	/**
	 * 验证回复的正确性
	 * @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,"payType", $arrResponse["payType"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"cardNumber", $arrResponse["cardNumber"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"cardPwd", $arrResponse["cardPwd"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"orderId", $arrResponse["orderId"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"orderAmount", $arrResponse["orderAmount"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"dealId", $arrResponse["dealId"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"orderTime", $arrResponse["orderTime"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"ext1", $arrResponse["ext1"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"ext2", $arrResponse["ext2"]);	
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"payAmount", $arrResponse["payAmount"]);
	    $merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"billOrderTime", $arrResponse["billOrderTime"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"payResult", $arrResponse["payResult"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"signType", $arrResponse["signType"]);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"key", $this->config->sp_key);
	    if( strtoupper(md5($merchantSignMsgVal)) == $arrResponse["signMsg"] )
	    {
	    	return true;
	    }
	    return false;
	}
	
	private function appendParam($returnStr, $paramId, $paramValue) {
		
		if ($returnStr != "") {			
			if ($paramValue != "") {				
				$returnStr .= "&" . $paramId . "=" . $paramValue;
			}		
		} else {			
			If ($paramValue != "") {
				$returnStr = $paramId . "=" . $paramValue;
			}
		}		
		return $returnStr;
	}
}