<?php
class alipay { 
	public function __construct(){
		$this->gateway_url='https://www.alipay.com/cooperate/gateway.do?_input_charset=utf-8';
		$this->url_r=PAY_SITE.'pay_alipay.php.php';
		$this->url_n=PAY_SITE.'pay_alipay.php.php';
		$this->payment=$this->get_payment();
		if(empty($this->payment))return false;
		/*
	0 => '纯担保交易接口 create_partner_trade_by_buyer',
    1 => '标准实物双接口 trade_create_by_buyer',
    2 => '即时到账接口	 create_direct_pay_by_user',
*/
		if ($this->payment['service_type']==1)
        {
            $this->service = 'trade_create_by_buyer';
        }
		elseif($this->payment['service_type']==2)
		{
			$this->service = 'create_direct_pay_by_user';
		}
        else
        {
            $this->service = 'create_partner_trade_by_buyer';
        }
	}
	public function get_code($order)
    {
        $parameter = array(
            '_input_charset'    => 'utf-8',
			'body'				=> '['.$order['username'].']'.$order['order_sn'],
			'buyer_email' 		=> $order['email'],
			'logistics_fee'		=>	0,
			'logistics_payment'	=>	'SELLER_PAY',
			'logistics_type'	=>	'EXPRESS',
			'notify_url'        =>$this->url_n,
			'out_trade_no'      => $order['order_sn'],
			'partner'           => $this->payment['alipay_pid'],
			'payment_type'      => 1,
			'price'             => $order['price'],
			'quantity'          => 1,
			'return_url'        =>$this->url_r,
			'seller_email'      => $this->payment['alipay_id'],
			'service'           => $this->service,
            'subject'           => '会员['.$order['username'].']在线充值', 
        );
       	pe_lead('include/function/alipay.func.php');
	    $parameter['sign']=build_mysign($parameter,$this->payment['alipay_key'],'MD5');
		$_SESSION['confirm']['body']=$parameter['body'];
		$_SESSION['confirm']['out_trade_no']=$parameter['out_trade_no'];
		$_SESSION['confirm']['payment_type']=$parameter['payment_type'];
		$_SESSION['confirm']['seller_email']=$parameter['seller_email'];
		$_SESSION['confirm']['subject']=$parameter['subject'];
		$_SESSION['confirm']['total_fee']=$parameter['price'];
		$str='<form action="' . $this->gateway_url . '" method="POST" target="_blank">';
		foreach ($parameter as $key => $value) $str .= '<input type="hidden" name="' . $key . '" value="' . $value . '" />';
		$str .= '<input type="submit" value="确认支付" class="tjbtn" />';
		$str .= '</form>';
		return $str;
    }
	 /**
     * 响应操作
     */
    public function respond() {
		$_res = $_POST ? $_POST : $_GET;
		if($_res){
			if($_res['is_success']=='T'){
				$confirm=$_SESSION['confirm'];
				if($confirm['body']==$_res['body'] and $confirm['out_trade_no']==$_res['out_trade_no'] and $confirm['payment_type']==$_res['payment_type'] and $confirm['seller_email']==$_res['seller_email'] and $confirm['subject']==$_res['subject'] and $confirm['total_fee']==$_res['total_fee']){
					unset($_SESSION['confirm']);
					return true;
				}
			}
		}
		return false;
	}
	public function get_payment(){
		global $db;
		$a = $db->pe_select('setting',array('setting_key'=>'alipay_id'),'setting_value');
		$info['alipay_id']=$a['setting_value'];
		$a = $db->pe_select('setting',array('setting_key'=>'alipay_key'),'setting_value');
		$info['alipay_key']=$a['setting_value'];
		$a = $db->pe_select('setting',array('setting_key'=>'alipay_pid'),'setting_value');
		$info['alipay_pid']=$a['setting_value'];
		$a = $db->pe_select('setting',array('setting_key'=>'service_type'),'setting_value');
		$info['service_type']=$a['setting_value'];
		$a = $db->pe_select('setting',array('setting_key'=>'alipay_rate'),'setting_value');
		$info['alipay_rate']=$a['setting_value'];
		return $info;
	/*
		Array
		(
    		[alipay_id] => thurlin1980@gmail.com
    		[alipay_key] => plwnpdmoo4ebm75br2f9xjltiwjogm7q
    		[alipay_pid] => 2088202548889181
    		[service_type] => 2
		)

	*/
	}
}
?>