龚哥哥 爱生活、做自己!
PHP支付宝接口类 WEB版 即时到帐接口
发表于 2015-9-1 | 浏览(1909) | PHP
<?php

/**
 * 支付宝支付驱动
 * @author  Devil
 * @version v_1.0.0
 */
class Alipay
{
    private $config;

    /**
     * [__construct 构造方法, 初始化配置信息]
     */
    public function __construct()
    {
        $this->config = array(
            'alipay_key'        =>  '', //key
            'alipay_partner'    =>  '', //partner
            'alipay_account'    =>  '', //支付宝账户名称
            'notify_url'        =>  '', //异步通知地址
            'call_back_url'     =>  '', //同步返回地址
        );
    }

    /**
     * [Payment 生成即时到帐支付信息]
     * @param  [array] $order [订单数据]
     */
    public function Payment($order)
    {
        $param = array(
            'service'           => 'create_direct_pay_by_user',
            'partner'           => $this->config['alipay_partner'],
            '_input_charset'    => 'utf-8',
            'notify_url'        => $this->config['notify_url'],
            'return_url'        => $this->config['call_back_url'],

            /* 业务参数 */
            'subject'           => $order['name'],
            'out_trade_no'      => $order['number_id'],
            'price'             => $order['total_fee'],
            'quantity'          => 1,
            'payment_type'      => 1,

            /* 物流参数 */
            'logistics_type'    => 'EXPRESS',
            'logistics_fee'     => 0,
            'logistics_payment' => 'BUYER_PAY_AFTER_RECEIVE',

            /* 买卖双方信息 */
            'seller_email'      => $this->config['alipay_account']
        );
        ksort($param);
        $string = '';
        $sign  = '';

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

        $string = substr($string, 0, -1);
        $sign  = md5(substr($sign, 0, -1).$this->config['alipay_key']);
        header('location:https://mapi.alipay.com/gateway.do?'.$string. '&sign='.$sign.'&sign_type=MD5');
    }

    /**
     * [Respond 异步请求处理]
     * @return [string] [成功success, 失败其它]
     */
    public function Respond()
    {
        /* 参数处理 */
        if (!empty($_POST))
        {
            foreach($_POST as $key => $val)
            {
                $_GET[$key] = $val;
            }
        }
        $param = $_GET;
        ksort($param);

        /* 判断是否已经处理过 */
        $this->IsRespond($param);

        /* 签名校验 */
        $sign = '';
        foreach($param AS $key=>$val)
        {
            if($key != 'sign' && $key != 'sign_type' && $key != 'code')
            {
                $sign .= "$key=$val&";
            }
        }
        $sign = md5(substr($sign, 0, -1).$this->config['alipay_key']);
        if($Sign == $param['sign'] && $param['trade_status'] == 'TRADE_SUCCESS')
        {
            //$param['out_trade_no'] 参数是唯一标识符
            exit('success');
        }
    }

    /**
     * [IsRespond 是否处理过操作]
     * @param [array] $param [参数]
     */
    private function IsRespond($param)
    {
        //$param['out_trade_no'] 参数是唯一标识符
        //如果处理过了可以直接exit('success');
    }
}

?>

阅读全文

PHP支付宝支付接口pc+wap MD5加密方式
发表于 2015-8-31 | 浏览(4081) | PHP
<?php

/**
 * 支付宝支付驱动
 * @author  Devil
 * @version V_1.0.0
 */
class AlipayLibrary
{
	/**
	 * [__construct 构造方法]
	 */
	public function __construct(){}

	/**
	 * [SoonPay 立即支付]
	 * @param [array] $data [支付信息]
	 */
	public function SoonPay($data, $config)
	{
		if(empty($data) || empty($config)) return false;

                //这里判断是否是手机访问,自己写一个方法接口。根据访问终端类型进行区分pc或wap
		if(IsMobile())
		{
			$this->SoonPayMobile($data, $config);
		} else {
			$this->SoonPayWeb($data, $config);
		}
	}

	/**
	 * [SoonPayMobile wap支付]
	 * @param  [array] $data   [参数列表]
	 * @param  [array] $config [配置信息]	
	 */
	private function SoonPayMobile($data, $config)
	{
		$request_token = $this->GetRequestToken($data, $config);

		$req_data = '<auth_and_execute_req><request_token>'.$request_token.'</request_token></auth_and_execute_req>';
		$parameter = array(
			'service'				=>	'alipay.wap.auth.authAndExecute',
			'format'				=>	'xml',
			'v'						=>	'2.0',
			'partner'				=>	$config['id'],
			'sec_id'				=>	'MD5',
			'req_data'				=>	$req_data,
			'request_token'			=>	$request_token
		);

		$param = $this->GetParamSign($parameter, $config);
		header('location:http://wappaygw.alipay.com/service/rest.htm?'.$param['param']. '&sign='.md5($param['sign']));

	}

	/**
	 * [GetRequestToken 获取临时token]
	 * @param  [array] $data   [参数列表]
	 * @param  [array] $config [配置信息]
	 * @return [string]        [返回临时token]
	 */
	private function GetRequestToken($data, $config)
	{
		$parameter = array(
			'service'				=>	'alipay.wap.trade.create.direct',
			'format'				=>	'xml',
			'v'						=>	'2.0',
			'partner'				=>	$config['id'],
			'req_id'				=>	$data['order_sn'],
			'sec_id'				=>	'MD5',
			'req_data'				=>	$this->GetReqData($data, $config),
			'subject'				=>	$data['name'],
			'out_trade_no'			=>	$data['order_sn'],
			'total_fee'				=>	$data['total_price'],
			'seller_account_name'	=>	$config['name'],
			'call_back_url'			=>	$data['call_back_url'],
			'notify_url'			=>	$data['notify_url'],
			'out_user'				=>	$data['out_user'],
			'merchant_url'			=>	$data['merchant_url'],
		);
		
		$param = $this->GetParamSign($parameter, $config);
        $ret = urldecode(file_get_contents('http://wappaygw.alipay.com/service/rest.htm?'.$param['param'].'&sign='.md5($param['sign'])));

        $para_split = explode('&',$ret);
        //把切割后的字符串数组变成变量与数值组合的数组
        foreach ($para_split as $item) {
            //获得第一个=字符的位置
            $nPos = strpos($item,'=');
            //获得字符串长度
            $nLen = strlen($item);
            //获得变量名
            $key = substr($item,0,$nPos);
            //获得数值
            $value = substr($item,$nPos+1,$nLen-$nPos-1);
            //放入数组中
            $para_text[$key] = $value;
        }

        $req = Xml_Array($para_text['res_data']);
        if(empty($req['request_token']))
        {
        	exit(header('location:'.__ROOT__.'index.php?g=Info&c=Prompt&f=PromptInfo&state=error&content=支付宝异常错误&url='.__ROOT__));
        }

        return $req['request_token'];
	}

	private function GetReqData($data, $config)
	{
		return '<direct_trade_create_req>
					<subject>'.$data['name'].'</subject>
					<out_trade_no>'.$data['order_sn'].'</out_trade_no>
					<total_fee>'.$data['total_price'].'</total_fee>
					<seller_account_name>'.$config['name'].'</seller_account_name>
					<call_back_url>'.$data['call_back_url'].'</call_back_url>
					<notify_url>'.$data['notify_url'].'</notify_url>
					<out_user>'.$data['out_user'].'</out_user>
					<merchant_url>'.$data['merchant_url'].'</merchant_url>
					<pay_expire>3600</pay_expire>
					<agent_id>0</agent_id>
				</direct_trade_create_req>';
	}

	/**
	 * [SoonPayWeb web支付]
	 * @param [array] $data   [订单信息]
	 * @param [array] $config [配置信息]
	 */
	private function SoonPayWeb($data, $config)
	{
        $parameter = array(
            'service'           => 'create_direct_pay_by_user',
            'partner'           => $config['id'],
            '_input_charset'    => ML_CHARSET,
            'notify_url'        => $data['notify_url'],
            'return_url'        => $data['call_back_url'],

            /* 业务参数 */
            'subject'           => $data['name'],
            'out_trade_no'      => $data['order_sn'],
            'price'             => $data['total_price'],

            'quantity'          => 1,
            'payment_type'      => 1,

            /* 物流参数 */
            'logistics_type'    => 'EXPRESS',
            'logistics_fee'     => 0,
            'logistics_payment' => 'BUYER_PAY_AFTER_RECEIVE',

            /* 买卖双方信息 */
            'seller_email'      => $config['name']
        );

        $param = $this->GetParamSign($parameter, $config);
		header('location:https://mapi.alipay.com/gateway.do?'.$param['param']. '&sign='.md5($param['sign']).'&sign_type=MD5');
	}

	/**
	 * [GetParamSign 生成参数和签名]
	 * @param  [array] $data   [待生成的参数]
	 * @param  [array] $config [配置信息]
	 * @return [array]         [生成好的参数和签名]
	 */
	private function GetParamSign($data, $config)
	{
        $param = '';
        $sign  = '';
        ksort($data);

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

        return array(
        	'param'	=>	substr($param, 0, -1),
        	'sign'	=>	substr($sign, 0, -1).$config['key']
        );
	}

	/**
	 * [Respond 异步处理]
	 * @param  [array] $config [配置信息]
	 * @return [array|string] [成功返回数据列表,失败返回no]
	 */
	public function Respond($config)
	{
		if(empty($config)) return 'no';

        $data = empty($_POST) ? $_GET :  array_merge($_GET, $_POST);
		ksort($data);

		$sign = '';
		$sec = isset($data['sec_id']) ? $data['sec_id'] : '';
		if($sec == 'MD5')
		{
            $data_xml = json_decode(json_encode((array) simplexml_load_string($data['notify_data'])), true);
            $data = array_merge($data, $data_xml);
            $sign = 'service='.$data['service'].'&v='.$data['v'].'&sec_id='.$data['sec_id'].'&notify_data='.$data['notify_data'];
		} else {
	        foreach($data AS $key=>$val)
	        {
	            if ($key != 'sign' && $key != 'sign_type' && $key != 'code')
	            {
	                $sign .= "$key=$val&";
	            }
	        }
	        $sign = substr($sign, 0, -1);
	    }
        if(!isset($data['sign']) || md5($sign.$config['key']) != $data['sign']) return 'no';

        /* 支付状态 */
        $state = isset($data['trade_status']) ? $data['trade_status'] : $data['result'];
        switch($state)
        {
            case 'TRADE_SUCCESS':
                return $data;
                break;

            case 'TRADE_FINISHED':
                return $data;
                break;

               case 'success':
                return $data;
                break;
        }
        return 'no';
	}

}
?>

阅读全文

TOP