AliwapService.php 4.37 KB
<?php

namespace Plugin\Pay\aliwap;

use Plugin\Pay\aliwap\AliwapConfig;
use Plugin\Pay\aliwap\AliwapReqparams;
use Plugin\Pay\aliwap\AliwapRspparams;
use Api\Yohobuy;
use Plugin\Helpers;
use Plugin\UdpLog;
/**
 * 支付宝手机网页支付
 */
class AliwapService
{

    var $config;
    var $alipay_config;

    public function __construct()
    {
        $this->logProjectPrefix = 'aliwap';
        $this->config = new AliwapConfig();
    }

    /**
     * 构造请求参数
     * @see QPay_Utils_Interface::getPayRequestPars()
     */
    public function getPayRequestPars(AliwapReqparams $params)
    {
        //构造要请求的参数数组,无需改动
        $parameter = array(
            'service' => $this->config->service,
            'partner' => $this->config->partner,
            '_input_charset' => $this->config->input_charset,
            'notify_url' => SERVICE_NOTIFY . $this->config->notify_url,
            'return_url' => Helpers::transHttpsUrl(Helpers::url($this->config->return_url)),
            /* 业务参数 */
            'payment_type' => $this->config->payment_type,
            'seller_id' => $this->config->partner,
            'it_b_pay'  =>  $params->payExpireMinute . 'm',
            'payment_type' => $this->config->payment_type,
            'out_trade_no' => $params->orderCode,
            'subject' => $params->goodsName,
            'total_fee' => $params->totalFee / 100,   //单位为元
            "show_url" => Helpers::transHttpsUrl(Helpers::url('/home/orderdetail', array('order_code' => $params->orderCode))),
        );

        ksort($parameter);
        reset($parameter);
        $param = '';
        $sign = '';

        $parameter = Helpers::paraFilter($parameter);

        foreach ($parameter AS $key => $val) {
            $param .= "$key=" . urlencode($val) . "&";
            $sign .= "$key=$val&";
        }

        $param = substr($param, 0, -1);
        $sign = substr($sign, 0, -1) . $this->config->alipay_key;

        $result = array(
            'pay_url' => $this->config->pay_url,
            'pars' => $param . "&sign=" . md5($sign) . "&sign_type=" . $this->config->sign_type,
            'reqType' => 'get'
        );

        UdpLog::info("【{$this->logProjectPrefix}-支付宝支付】,function:getPayRequestPars,请求参数", $result);
        return $result;
    }

    /**
     * 解析结果
     * 
     */
    public function parseResponse(Array $arrResponse)
    {
        UdpLog::info("【{$this->logProjectPrefix}-支付宝支付】,function:parseResponse,回调参数", $arrResponse);
        if (isset($arrResponse['q'])) {
            unset($arrResponse['q']);
        }
        $rsp = new AliwapRspparams();
        if (!$this->checkResponse($arrResponse)) {
            //验证不成功
            $rsp->payResult = -1;
        } else {
            $rsp->bankName = "";
            $outTradeNo = $arrResponse["out_trade_no"];
            $rsp->orderCode = $outTradeNo;
            $rsp->payResult = $this->convertResult($arrResponse["trade_status"]);
            $rsp->payTime = isset($arrResponse["gmt_payment"]) ? $arrResponse["gmt_payment"] : '';
            $rsp->totalFee = $arrResponse["total_fee"];
            $rsp->resultMsg = $arrResponse["notify_type"];
            //添加支付订单号和交易号
            $rsp->payOrderCode = $outTradeNo;
            $rsp->tradeNo = $arrResponse['trade_no'];
            $rsp->bankBillNo = "";
        }

        UdpLog::info("【{$this->logProjectPrefix}-支付宝支付】,function:parseResponse,回调参数的结果", $arrResponse);
        return $rsp;
    }

    protected function convertResult($resultCode)
    {
        if ($resultCode == "TRADE_SUCCESS") {
            return 200;
        }
        return 400;
    }
    
    /**
     * 验证回复的正确性
     * @see QPay_Utils_Abstract::verifResponse()
     */
    protected function checkResponse(array $arrResponse)
    {
        if (empty($arrResponse['sign'])) {
            return false;
        }
        ksort($arrResponse);
        reset($arrResponse);
        $sign = '';
        foreach ($arrResponse AS $key => $val) {
            if ($key === 'sign' || $key === 'sign_type' || $key === 'code' || $val === '') {
                continue;
            }
            $sign .= "$key=$val&";
        }
        $sign = substr($sign, 0, -1) . $this->config->alipay_key;

        return md5($sign) != $arrResponse['sign'] ? false : true;
    }
}