<?php
class pay { 
	function __construct(){
		$this->url_r=PAY_SITE.'pay.php?act=geturl';
		$this->url_n= PAY_SITE.'pay.php?act=posturl';
		$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';
        }
	}
	function get_code($order)
    {
        $parameter = array(
            'service'           => $this->service,
            'partner'           => $this->payment['alipay_pid'],
            '_input_charset'    => 'utf-8',
            'return_url'        =>$this->url_r,
            'notify_url'        =>$this->url_n,
            /* 业务参数 */
            'subject'           => 'Order SN:'.$order['order_sn'],
            'out_trade_no'      => $order['order_sn'], //
            'price'             => round($order['order_amount'],2),
            'quantity'          => 1,
            'payment_type'      => 1,
            /* 物流参数 */
            'logistics_type'    => 'EXPRESS',
            'logistics_fee'     => 0,
            'logistics_payment' => 'BUYER_PAY_AFTER_RECEIVE',
            /* 买卖双方信息 */
            'seller_email'      => $this->payment['alipay_id']
        );
        ksort($parameter);
        reset($parameter);
        $param = '';
        $sign  = '';
        foreach ($parameter as $key => $val)
        {
            $param .= "$key=$val&";
            $sign  .= "$key=$val&";
        }

        $param = substr($param, 0, -1);
        $sign  = substr($sign, 0, -1). $this->payment['alipay_key'];

        $button = '<div style="text-align:left"><input type="button" onclick="window.open(\'https://www.alipay.com/cooperate/gateway.do?'.$param. '&sign='.md5($sign).'&sign_type=MD5\')" value="立即使用支付宝支付" /></div>';
        return $button;
    }
	 /**
     * 响应操作
     */
    function respond()
    {
		if ($this->payment['alipay_pid'] != $_GET['seller_id'] or $this->payment['alipay_id'] != $_GET['seller_email'])
        {
			$this->err = '校验失败，若您的确已经在网关处被扣了款项，请及时联系店主，并且请不要再次点击支付按钮(原因：错误的签名)';
            return false;
        }
		/*
		WAIT_BUYER_PAY 交易创建
		WAIT_SELLER_SEND_GOODS 买家付款成功
		WAIT_BUYER_CONFIRM_GOODS 卖家发货成功
		TRADE_FINISHED 交易成功结束
		TRADE_CLOSED 交易关闭
		modify.tradeBase.totalFee 修改交易价格
		*/
        if ( $_GET['trade_status'] == "WAIT_SELLER_SEND_GOODS" || $_GET['trade_status'] == "TRADE_FINISHED" || $_GET['trade_status'] == "TRADE_SUCCESS")
        {           
         	return true;
        }
		else return false;
    }
	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
		)

	*/
	}
}
?>