<?php
include('common.php');
switch($act){	
	case 'getpost':				
		$_p=$_p_order;
		$name_money = explode('|',$_p_name_money);
		$_p['order_name']=$name_money[0];
		$_p['order_money']=$name_money[1];
		$_p['order_state']='notpay';
		$_p['user_id']=$_SESSION['user_id'];
		$_SESSION['temp_a']=base64_encode(serialize($_p));		
		if($_p['order_paytype']=='alipay'){		
			pe_lead('include/class/pay.class.php');
			$pay= new pay();
			if(!$pay)pe_success('支付方式错误！');
			$order['email']=$_p['user_email'];
			$order['contactname']=$_p['user_tname'];
			$order['remark1']=$_p['order_text'];
			$order['telephone']=$_p['user_phone'];
			$order['order_amount']=sprintf("%.2f",round($_p['order_money']*(1+$pay->payment['alipay_rate']/100),2));
			$order['order_sn']=date('YmdHis',TIME).random(6);
			$pay_online = $pay->get_code($order);
			include(pe_tpl('send_alipay.html'));
		}
		else{
			header('location:'.PAY_SITE.'pay.php?act=postalipay');
		}
	break;
	case 'geturl':
		$_p=unserialize(base64_decode($_SESSION['temp_a']));
		pe_lead('include/class/pay.class.php');
		$payment = new pay();
		if(isset($payment))
		{
			$result = $payment->respond();
			if($result)
			{
            	
				$_p['order_state']='paid';
				$_p['order_ptime']=TIME;
				$pro=$db->pe_select('product',array('product_name'=>$_p['order_name']));
				if($pro['downurl']){
					$_p['order_state']='send';
					$_p['order_stime']=TIME;
					$_p['downurl']=$pro['downurl'];
				}
				
			}	
		}
		$_SESSION['temp_a']=base64_encode(serialize($_p));
		header('location:'.PAY_SITE.'pay.php?act=postalipay');
	break;
	case 'postalipay':
		$_p=unserialize(base64_decode($_SESSION['temp_a']));
		$_p['order_atime']=TIME();
		$downurl=$_p['downurl'];
		unset($_p['downurl']);	
		$order_id = $db->pe_insert('order', pe_dbhold($_p));
		$_SESSION['temp_a']=NULL;
		//$order_id=10;
		//$_p=$db->pe_select('order',array('order_id'=>$order_id));
		if($order_id){
			//订单邮件
			$info['order_name'] = $_p['order_name'];  	 	
    		$info['order_money']= $_p['order_money'];
    		$info['order_text']= $_p['order_text'];
    		$info['user_tname']= $_p['user_tname'];
    		$info['user_email']= $_p['user_email'];
    		$info['user_address']=$_p['user_address'];
			
			if($_p['order_state']=='send'){
				if($downurl){
					$st='sendv';
					$info['downurl']=$downurl;
				}
				else $st='send';
			}
			elseif($_p['order_state']=='paid')$st='pay';
			else $st='add';
			pe_lead('hook/order.hook.php');
			$email['qunfa_name'] = orderemail($st,'name',$info);
			$email['qunfa_text'] = orderemail($st,'text',$info);
			
			$user['user_name'] = $info['user_tname'];
			$user['user_email'] = $info['user_email'];
			pe_lead('hook/qunfa.hook.php');
			qunfa_emailone($email, $user);
			pe_success('订单提交成功！','index.php');
		}
		pe_error('订单提交失败！','index.php');	
	break;
	default :
		header('location:'.PAY_SITE);
}
?>