Service.php 3.81 KB
<?php

namespace WebPlugin\Pay\Allinpay;

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 = 'allinpay';
        $this->config = new Config();
        $myConfig = json_decode($paymentParams["pay_params"]);
        $this->config->merchantId = $myConfig->merchant_id;
        $this->config->merchantKey = $myConfig->merchant_key;
    }

    /**
     * @param Reqparams $params
     * @return array
     */
    public function getPayRequestPars(Reqparams $params)
    {
        $baseUrl = $this->getBaseNoticeUrl($params->isTest);
        $loseTime = intval(($params->orderTime + 7200 - time()) / 60);

        $reqData = array(
            'inputCharset' => $this->config->input_charset,
            'pickupUrl' => $baseUrl . $this->config->return_url,
            'receiveUrl' => $baseUrl . $this->config->notify_url,
            'version' => $this->config->version,
            'language' => 1,
            'signType' => $this->config->sign_type,
            'merchantId' => $this->config->merchantId,
            'orderNo' => $params->orderCode,
            'orderAmount' => $params->totalFee,
            'orderCurrency' => 0,  //币种,0为人民币
            'orderDatetime' => date('YmdHis', $params->orderTime),
            'orderExpireDatetime' => $loseTime,
            'payType' => $this->config->payType
        );

        $paramsArr = array();
        foreach ($reqData as $k => $v) {
            $paramsArr[] = $k . '=' . $v;
        }
        $reqData['signMsg'] = strtoupper(md5(implode('&', $paramsArr) . '&key=' . $this->config->merchantKey));
        return array(
            'pay_url' => $this->config->pay_url,
            'pars' => $reqData,
            'reqType' => 'post'
        );
    }

    /**
     * 回复解析(non-PHPdoc)
     * @param array $arrResponse
     * @return void|Rspparams
     */
    public 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["payResult"]);
            $rsp->payTime = $arrResponse["payDatetime"];
            $rsp->totalFee = $arrResponse["payAmount"] / 100;
            $rsp->resultMsg = $arrResponse["payResult"];
            //添加支付订单号和交易号
            $rsp->payOrderCode = $arrResponse["orderNo"];
            $rsp->tradeNo = "";
            $rsp->bankBillNo = "";
        }

        return $rsp;
    }

    protected function convertResult($resultCode)
    {
        if ($resultCode == 1) {
            return 200;
        }
        return 400;
    }


    /**
     * 验证回复的正确性
     * @see QPay_Utils_Abstract::verifResponse()
     * @param array $arrResponse
     * @return bool|void
     */
    protected function checkResponse(array $arrResponse)
    {
        $strParams = 'merchantId=' . $arrResponse['merchantId'];
        $paramKey = array('version', 'language', 'signType', 'payType', 'issuerId', 'paymentOrderId', 'orderNo', 'orderDatetime', 'orderAmount', 'payDatetime', 'payAmount', 'ext1', 'ext2', 'payResult', 'errorCode', 'returnDatetime');
        foreach ($paramKey as $k) {
            if (isset($arrResponse[$k]) && $arrResponse[$k] != "") {
                $strParams .= '&' . $k . '=' . $arrResponse[$k];
            }
        }
        $signMsg = strtoupper(md5($strParams . '&key=' . $this->config->merchantKey));
        return $signMsg == $arrResponse['signMsg'] ? true : false;
    }
}