Service.php 3.65 KB
<?php

namespace WebPlugin\Pay\Chinabank;

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

class Service extends PayAbstract
{
	
	var $config ;
	
	function __construct(array $paymentParams)
	{
		$this->logProjectPrefix = 'chinabank';
		$this->config = new Config();
		
		$myConfig = json_decode($paymentParams["pay_params"]) ;
		$this->config->merchantAcctId = $myConfig->merchant_id;
		$this->config->sp_key = $myConfig->merchant_key;
	//	$this->config->branchID = $myConfig->merchant_other_code;
				
	}
	
	public function getPayRequestPars(Reqparams $params) {
		
		parent::getPayRequestPars($params);
		$baseUrl = $this->getBaseNoticeUrl($params->isTest);  
		$reqData = array();
		$reqData['v_mid'] = $this->config->merchantAcctId;  //商户ID
		$reqData['v_oid'] = $params->orderCode;
		$reqData['v_amount'] = $params->totalFee * 0.01;
		$reqData['v_moneytype'] = $this->config->feeType;
		$reqData['v_url'] = $baseUrl . $this->config->merchantUrl;
		$reqData['v_md5info']= strtoupper(md5($reqData['v_amount'] . $reqData['v_moneytype'] . $reqData['v_oid']. $reqData['v_mid'] . $reqData['v_url']. $this->config->sp_key));
		$reqData['remark1'] = $params->orderCode;
		$reqData['remark2'] = "[url:=".$baseUrl . $this->config->autoReceiveUrl."]";
		
		$result = array(
			'pay_url' => $this->config->payUrl,
			'pars' => $reqData,
			'reqType' => 'post'
		);
		return $result;
	}

    /**
     * 解析结果
     * (non-PHPdoc)
     * @see QCPay_Utils_Abstract::parseResponse()
     * @param array $arrResponse
     * @return void|QCPay_Utils_Rspparams
     */
	function parseResponse(Array $arrResponse){
		// {"v_md5all":"8F7E33B3759DC55F0E776120B5C16A55","v_md5info":"e42e938417bd01d8eb69235c1ebe9be3","remark1":"110610002420","v_pmode":null,"remark2":"","v_idx":"5607406315","v_md5":"2E9135262B2729B23B049EBBE80E5183","v_pstatus":"20","v_pstring":null,"v_md5str":"2E9135262B2729B23B049EBBE80E5183","v_md5money":"9330e642e14622f4993ce5a6e9781bbd","v_moneytype":"CNY","v_oid":"110610002420","v_amount":"0.01"}
		//把中文进行转码
		$arrResponse["v_pmode"] = mb_convert_encoding($arrResponse["v_pmode"], "UTF-8", "GBK");
		$arrResponse["v_pstring"] = mb_convert_encoding($arrResponse["v_pstring"], "UTF-8", "GBK");
		//定义结果返回变量
		$rsp = new Rspparams();
		if(!$this->checkResponse($arrResponse)){			
			//验证不成功
			$rsp->payResult = 400;
		}
		else{
			$rsp->bankName = $arrResponse["v_pmode"];
			$rsp->orderCode = $arrResponse["v_oid"];
			$rsp->payResult = $this->convertResult($arrResponse["v_pstatus"]);
			$rsp->payTime = time();
			$rsp->totalFee = $arrResponse["v_amount"];
			$rsp->resultMsg = $arrResponse["v_pstring"];
			//添加支付订单号和交易号
			$rsp->payOrderCode = $arrResponse["v_oid"];
			$rsp->tradeNo = "";
			$rsp->bankBillNo = "";
		}
		return $rsp;		
	}

    /**
     * (non-PHPdoc)
     * @see QPay_Utils_Abstract::convertResult()
     * @param $resultCode
     * @return int|void
     */
	protected function convertResult($resultCode)
	{
		if($resultCode == 20) //20为支付成功,30为支付失败
		{
			return 200;
		}
		return $resultCode;
	}

    /**
     * 验证回复的正确性
     * @see QPay_Utils_Abstract::verifResponse()
     * @param array $arrResponse
     * @return bool|void
     */
	protected function checkResponse(array $arrResponse)
	{
		$signText = $arrResponse["v_oid"]. $arrResponse['v_pstatus'] . $arrResponse['v_amount']
			 . $arrResponse['v_moneytype'] . $this->config->sp_key;
		if(strtoupper(md5($signText)) == $arrResponse['v_md5str']){
			return true;
		}
		return false;		
	}
}