Service.php 4.49 KB
<?php

namespace WebPlugin\Pay\Shengpay;

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

class Service extends PayAbstract
{
	
	var $config ;
	
	public function __construct(array $paymentParams)
	{
		$this->logProjectPrefix = 'shengpay';
		$this->config = new Config();
		$myConfig = json_decode($paymentParams["pay_params"]) ;
		$this->config->merchant_no = $myConfig->merchant_id;
		$this->config->merchant_key = $myConfig->merchant_key;
	}

    /**
     * @param Reqparams $params
     * @return array|void
     */
	public function getPayRequestPars(Reqparams $params)
	{
		$bankCode = $params->paymentParameter == 'platform' ? '' :  $params->paymentParameter;
		$baseUrl = $this->getBaseNoticeUrl($params->isTest);  
		$parameters = array(
			'Version' => $this->config->version,
			'Amount' => sprintf("%.2f", $params->totalFee * 0.01),
			'OrderNo' => $params->orderCode,
			'MerchantNo' => $this->config->merchant_no,
			'PayChannel' => $this->config->pay_channel,
			'PostBackUrl' => $baseUrl . $this->config->post_back_url,
			'NotifyUrl' => $baseUrl . $this->config->notify_url,
			'BackUrl' => $this->config->back_url,
			'OrderTime' => date('YmdHis', $params->orderTime),
			'CurrencyType' => $this->config->currency_type,
			'NotifyUrlType' => $this->config->notify_url_type,
			'SignType' => $this->config->sign_type,
			'BankCode' => $bankCode  //银行代码
		);
		$parameters["MAC"] = $this->getSign($parameters);
		
		$isDirect = empty($bankCode) ? false : TRUE;
		$result = array(
			'pay_url' => $this->getPayUrl($isDirect, $params->isTest),
			'pars' => $parameters,
			'reqType' => 'post'
		);

		return $result;
	}
	
	/**
	 * 获取支付地址
	 * Enter description here ...
	 * @param bool $isDirect 是否为直连
	 * @param bool $isTest  是否为测试环境
	 * @return string
	 */
	public function getPayUrl($isDirect, $isTest){
		if($isDirect){
			if($isTest){
				return $this->config->direct_pay_url_test;
			}else{
				return $this->config->direct_pay_url;
			}
		} else {
			if($isTest){
				return $this->config->pay_url_test;
			} else {
				return $this->config->pay_url;
			}
		}
	}

    /**
     * 获取签名
     * @param array $pars
     * @return string
     */
	public function getSign(array $pars)
	{
		$strPars = $pars['Version'] . $pars['Amount'] . $pars['OrderNo'] . $pars['MerchantNo'] . $pars['PayChannel']
			. $pars['PostBackUrl'] . $pars['NotifyUrl'] . $pars['BackUrl'] . $pars['OrderTime'] . $pars['CurrencyType']
			. $pars['NotifyUrlType'] . $pars['SignType'] . $pars['BankCode'];
		return md5($strPars . $this->config->merchant_key);
	}

    /**
     * 解析结果
     * @param array $arrResponse
     * @return void|Rspparams
     */
	function parseResponse(Array $arrResponse){
		
		$rsp = new Rspparams();
		if(!$this->checkResponse($arrResponse)){			
			//验证不成功
			$rsp->payResult = -1;
		}
		else{
			$rsp->bankName = "";
			$rsp->orderCode = $arrResponse["OrderNo"];
			$rsp->payResult = $this->convertResult($arrResponse["Status"]);
			$rsp->payTime = time();
			$rsp->totalFee = $arrResponse["Amount"];
			$rsp->resultMsg = $arrResponse["ExInfo"];
			//添加支付订单号和交易号
			$rsp->payOrderCode = $arrResponse["orderNo"];
			$rsp->tradeNo = "";
			$rsp->bankBillNo = "";
		}

		return $rsp;
	}

    /**
     * 验证回复的正确性
     * @param array $arrResponse
     * @return bool|void
     */
	protected function checkResponse(array $arrResponse)
	{
		$strPars = $arrResponse["Amount"] . '|' . $arrResponse['PayAmount'] . '|' . $arrResponse['OrderNo'] . '|' . $arrResponse['serialno'] . 
			'|' . $arrResponse['Status'] . '|' . $arrResponse['MerchantNo'] . '|' . $arrResponse['PayChannel'] . '|' . $arrResponse['Discount']
			. '|' . $arrResponse["SignType"] . '|' . $arrResponse['PayTime'] . '|' . $arrResponse['CurrencyType'] . '|' . $arrResponse['ProductNo']
			. '|' . $arrResponse['ProductDesc'] . '|' . $arrResponse['Remark1'] . '|' . $arrResponse['Remark2'] . '|' . $arrResponse['ExInfo'];
		$vaildSign = md5($strPars . '|' . $this->config->merchant_key);
		if(strtoupper($vaildSign) == $arrResponse["MAC"]){
			return true;
		}

		return false;			
	}

    /**
     * @param $resultCode
     * @return int|void
     */
	protected function convertResult($resultCode)
	{
		if($resultCode == "01") //20为支付成功,30为支付失败
		{
			return 200;
		}
		return $resultCode;
	}
	
	
}