AliwapService.php 5.57 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;

/**
 * 支付宝手机网页支付
 */
class AliwapService
{

    var $config;
    var $alipay_config;

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

        $this->alipay_config = array(
            'partner' => $this->config->partner,
            'key' => $this->config->alipay_key,
            'sign_type' => 'MD5',
            'input_charset' => 'utf-8',
            'cacert' => dirname(__FILE__) . '/cacert.pem',
            'transport' => 'http'
        );
        require_once("lib/alipay_core.function.php");
        require_once("lib/alipay_rsa.function.php");
        require_once("lib/alipay_md5.function.php");
        require_once("lib/alipay_notify.class.php");
        require_once("lib/alipay_submit.class.php");
    }

    /**
     * 构造请求参数
     * @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' => Yohobuy::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('')),
        );

        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'
        );
        return $result;
    }

    /**
     * 解析结果
     * 
     * @see AliwapInterface::parseResponse()
     */
    public function parseResponse(Array $arrResponse)
    {
        $rsp = new AliwapRspparams();
        if ($arrResponse['type'] == 'return') {
            //return 返回
            $alipayNotify = new \AlipayNotify($this->alipay_config);
            $verify_result = $alipayNotify->verifyReturn();
            if ($verify_result) {
                //商户订单号
                $rsp->orderCode = $_GET['out_trade_no'];
                $rsp->payResult = 200;
                //支付宝交易号
                $trade_no = $_GET['trade_no'];
            } else {
                $rsp->payResult = 400;
            }
        } else {
            //服务器返回
            $alipayNotify = new \AlipayNotify($this->alipay_config);
            $verify_result = $alipayNotify->verifyNotify();
            if ($verify_result) {
                //解密(如果是RSA签名需要解密,如果是MD5签名则下面一行清注释掉)
                // $notify_data = $alipayNotify->decrypt($_POST['notify_data']);
                //解析notify_data
                //注意:该功能PHP5环境及以上支持,需开通curl、SSL等PHP配置环境。建议本地调试时使用PHP开发软件
                $doc = new \DOMDocument();
                $doc->loadXML($_POST['notify_data']);
                if (!empty($doc->getElementsByTagName("notify")->item(0)->nodeValue)) {
                    //商户订单号
                    $out_trade_no = $doc->getElementsByTagName("out_trade_no")->item(0)->nodeValue;
                    //支付宝交易号
                    $trade_no = $doc->getElementsByTagName("trade_no")->item(0)->nodeValue;
                    //交易状态
                    $trade_status = $doc->getElementsByTagName("trade_status")->item(0)->nodeValue;
                    if ($trade_status == 'TRADE_FINISHED' || $trade_status == 'TRADE_SUCCESS') {
                        $rsp->orderCode = $out_trade_no;
                        $rsp->payResult = 200;
                    } else {
                        $rsp->payResult = 400;
                    }
                }
            } else {
                $rsp->payResult = 401;
            }
        }
        return $rsp;
    }

    private function getTokenReqData($out_trade_no, $subject, $total_fee)
    {
        return '<direct_trade_create_req><notify_url>' . $this->config->notify_url .
            '</notify_url><call_back_url>' . $this->config->return_url .
            '</call_back_url><seller_account_name>' . $this->config->sellerMail .
            '</seller_account_name><out_trade_no>' . $out_trade_no . '</out_trade_no><subject>' . 'yoho order:' . $out_trade_no .
            '</subject><total_fee>' . $total_fee . '</total_fee><merchant_url>' . $this->config->merchant_url . $out_trade_no .
            '</merchant_url></direct_trade_create_req>';
    }

}