<?php 
class unionpay {
	private $config;
	private $url;
	// 主入口
	public function config($config=null){
		$this->config = $config;
		if($config['type'] == 1){
			$this->config_upop();
		}
		if($config['type'] == 2){
			$this->config_dbjy();
		}
	}
	
	//及时到账
	private function config_upop(){
		$config = $this->config;
		require_once ('unionpay/quickpay_service.php');
		
		$param = array (
				'transType' => quickpay_conf::CONSUME, // 交易类型，CONSUME 为担保支付 / PRE_AUTH 为预授权交易
				'orderNumber' => $config['code'], // 订单号
				'orderAmount' => $config['money'] * 100, // 交易金额
				'orderTime' => date('Ymdhis'), // 交易时间
				'orderCurrency' => quickpay_conf::CURRENCY_CNY, // 交易币种
				'customerIp' => $_SERVER ['REMOTE_ADDR'], // 用户IP
				'frontEndUrl' => $config['ReturnUrl'], // 前台回调URL
				'backEndUrl' => $config['NotifyUrl'],  // 后台回调URL
				'merId'     => $config['id'], //商户填写
		);
		$pay_service = new quickpay_service ( $param, quickpay_conf::FRONT_PAY, $config['key']);
		$html = $pay_service->create_html ();
		echo $html;
	}
	
	//担保交易
	private function config_dbjy(){
		
		$config = $this->config;
		$payment_type = "1"; //支付类型 必填，不能修改
        $notify_url = $config['NotifyUrl'];
        $return_url = $config['ReturnUrl'];
        $seller_email = $config['pay_type_data']['user']['val']; //卖家支付宝帐户 必填
        $out_trade_no = $config['code']; //商户订单号 必填
        $subject = $config['title']; //订单名称 必填  
        $price = $config['money']; //付款金额 必填

        $quantity = "1";    //商品数量 必填，建议默认为1，不改变值，把一次交易看成是一次下订单而非购买一件商品
        $logistics_fee = "0.00";   //物流费用 必填，即运费  
        $logistics_type = "EXPRESS";  //物流类型 必填，三个值可选：EXPRESS（快递）、POST（平邮）、EMS（EMS）
    
        //物流支付方式 必填，两个值可选：SELLER_PAY（卖家承担运费）、BUYER_PAY（买家承担运费）
        $logistics_payment = "SELLER_PAY";
        
        $body = '';   //订单描述   
        $show_url = ''; //商品展示地址

        $receive_name = ''; //收货人姓名
        $receive_address = ''; //收货人地址
        $receive_zip = '';  //收货人邮编 
        $receive_phone = ''; //收货人电话号码
        $receive_mobile = ''; //收货人手机号码
		
		//签名方式 不需修改
		$alipay_config_sign_type = strtoupper('MD5');		
		//字符编码格式 目前支持 gbk 或 utf-8
		$alipay_config_input_charset = strtolower('utf-8');
		//ca证书路径地址，用于curl中ssl校验
		//请保证cacert.pem文件在当前文件夹目录中
		//$alipay_config_cacert    = getcwd().'\\cacert.pem';	
		$alipay_config_cacert =  dirname(__FILE__).DIRECTORY_SEPARATOR."alipay".DIRECTORY_SEPARATOR."cacert.pem";		
		$alipay_config_transport   = 'http';
		
		$alipay_config_id = $config['id'];                                  	//合作身份者id，以2088开头的16位纯数字
		$alipay_config_key = $config['key'];									//安全检验码，以数字和字母组成的32位字符
		$alipay_config_input_charset = strtolower('utf-8');
		
		
		$alipay_config=array(
			"partner"      =>$alipay_config_id,
			"key"          =>$alipay_config_key,
			"sign_type"    =>$alipay_config_sign_type,
			"input_charset"=>$alipay_config_input_charset,
			"cacert"       =>$alipay_config_cacert,
			"transport"    =>$alipay_config_transport
		);
		

		//构造要请求的参数数组，无需改动
		$parameter = array(
					"service" => "create_partner_trade_by_buyer",
					"partner" => $alipay_config_id,
					"payment_type"	=> $payment_type,
					"notify_url"	=> $notify_url,
					"return_url"	=> $return_url,
					"seller_email"	=> $seller_email,
					"out_trade_no"	=> $out_trade_no,
					"subject"	=> $subject,
					"price"	=> $price,
					"quantity"	=> $quantity,
					"logistics_fee"	=> $logistics_fee,
					"logistics_type"	=> $logistics_type,
					"logistics_payment"	=> $logistics_payment,
					"body"	=> $body,
					"show_url"	=> $show_url,
					"receive_name"	=> $receive_name,
					"receive_address"	=> $receive_address,
					"receive_zip"	=> $receive_zip,
					"receive_phone"	=> $receive_phone,
					"receive_mobile"	=> $receive_mobile,
					"_input_charset"	=> trim(strtolower($alipay_config['input_charset']))
		);
		$alipaySubmit = new AlipaySubmit($alipay_config);
		
		//$this->url =  urldecode($alipaySubmit->buildRequestGet($parameter));		
		$this->url =  $alipaySubmit->buildRequestForm($parameter,'POST','submit');
		
			
	}
	
	//发送
	public function send_pay(){
		 echo  $this->url;
		 exit;
		//header("Location: $url");	
	}
}

?>
