<?php
	!defined('DIRCMS_MEMBER') && exit('Access Denied!');
	$action=empty($action)?'payonline':trim($action);
	switch($action)
	{
		case 'payonline':
			$r=cache_read('cache_paymethod.cache.php',DIRCMS_ROOT.'data/cache/');
			$paymentidselect='';
			$i=0;
			if($r)foreach($r as $_r)
			{
				$ischecked=!$i?'checked="checked"':'';
				if($_r['is_online']==1)$paymentidselect.='<input type="radio" '.$ischecked.' value="'.$_r['id'].'" name="paymentid"/>&nbsp;'.$_r['name'].'<br />';
				$i++;
			}
			include member_tlp('account_payonline');
			unset($r,$paymentidselect);
			break;
		case 'online':
			if(!is_email($email) || $email == '')  $email = '';		
			$order['email'] = $email;
			if(empty($amount))
			{
				showmsg('请输入正确的金额!');
			}
			else
			{
				$amount=floatval($amount);
			}
			if(!$paymentid)
			{
				showmsg('请输选择支付方式!');
			}
			else
			{
				$paymentid=intval($paymentid);
			}

			set_cookie('orderid', $forward);
			session_start();
			$order['contactname']=$contactname=htmlspecialchars($contactname);
			$order['remark1']=htmlspecialchars($usernote);		
			$order['telephone']=$telephone=safe_replace($telephone);

			//订单号
			$_SESSION['order_sn']=$order['order_sn']=create_sn();
			$_SESSION['order_sn_time']=TIME;

			$payment=cache_read('cache_paymethod'.$paymentid.'.cache.php',DIRCMS_ROOT.'data/cache/');
			if(!$payment)showmsg('无效的支付方式!');
			$payment['config']=string2array($payment['config']);
			$payment['fee']= pay_fee($amount,$payment['fee'].'%');//手续费
			$order['order_amount']=$amount+$payment['fee'];//实际要支付的金额
			
			//支付记录
			$online['amount'] = $order['order_amount'];
			$online['moneytype'] = 'CNY';//支付币种

			/* 变量初始化 */
			$surplus=array();
			$surplus = array(
					'userid'      => $_userid,
					'username'    => $_username,
					'amount'      => $order['order_amount'], //实际要支付的金额
					'quantity'    => $amount,
					'telephone'   => $telephone,
					'contactname' => $contactname,
					'email'		  => $email,
					'addtime'	  => TIME,
					'type'		  => '1', //  在线充值
					'payment'     => $payment['name'],
					'ispay'	      => '0',
					'usernote'    => isset($usernote)?htmlspecialchars($usernote) : '',
					'sn'		  => $order['order_sn'],
					'ip'		  => IP
			);
			$order['orderid']=$db->insert(DB_PRE.'pay_user_account',$surplus);
			include_once(DIRCMS_ROOT.'api/' . $payment['code'] . '.php');
			$pay_obj= new $payment['code'];
			$pay_online = $pay_obj->get_code($order,$payment['config']);
			
			$order['desc']=$payment['desc'];
			echo $pay_online;
			break;
		case 'paybycard': /////////////////////// 点卡是买来后用于购买点数的，不能用于代替现金
			if(isset($do_submit))
			{
				if(!preg_match('/^[a-z_0-9]{2,32}$/i',$cardid))showmsg('该点卡不存在或者已失效!',$forward);
				if($_SESSION['checkcode']!=$inputcheckcode)showmsg('验证码不正确!',$forward);
				$r=$db->fetch_one("SELECT `".DB_PRE."card`.`point`,`".DB_PRE."card`.`status`,`".DB_PRE."card`.`endtime` FROM `".DB_PRE."card` WHERE `".DB_PRE."card`.`cardid`='{$cardid}'");
				if($r['status']==0)showmsg('该点卡已禁用!',$forward);
				if($r['status']==2)showmsg('该点卡已使用!',$forward);
				if($r['endtime']<TIME)showmsg('该点卡已失效!',$forward);
				$db->query("UPDATE `".DB_PRE."member` SET `".DB_PRE."member`.`point`=`".DB_PRE."member`.`point`+{$r['point']} WHERE `".DB_PRE."member`.`id`={$_userid}",true);
				$db->query("UPDATE `".DB_PRE."member_cache` SET `".DB_PRE."member_cache`.`point`=`".DB_PRE."member_cache`.`point`+{$r['point']} WHERE `".DB_PRE."member_cache`.`id`={$_userid}",true);
				$db->query("UPDATE `".DB_PRE."card` SET `".DB_PRE."card`.`username`='{$_username}',`".DB_PRE."card`.`userid`={$_userid},`".DB_PRE."card`.`status`=2 WHERE `".DB_PRE."card`.`cardid`='{$cardid}'",true);
				$buylog=array();
				$buylog = array(
					'userid'      => $_userid,
					'username'    => $_username,
					'type'		  => 'point',
					'blance'      => $_point+$r['point'],////////////  点数余额
					'number'      => $r['point'], 				
					'inputid'     => $_userid,
					'inputer'     => $_username,
					'ip'		  => IP,
					'note'		  => '充值卡充值点数',
					'time'	      => TIME
				);
				$order['orderid']=$db->insert(DB_PRE.'pay_exchange',$buylog);
				showmsg('充值成功!',$DIRCMS['site_url'].'member/index.php?file=account&action=buylog');
			}	
			include member_tlp('account_paybycard');
			break;
		case 'buypoint':
			if(isset($do_submit))
			{
				if(!preg_match('/^[0-9]+$/i',$point))showmsg('输入必须为数字!',$forward);
				if($_SESSION['checkcode']!=$inputcheckcode)showmsg('验证码不正确!',$forward);
				if(floatval($point*0.5)>floatval($_amount))showmsg('账户余额不足!',$forward);
				$amount=round(floatval(floatval($_amount)-floatval($point*0.5)),2);
				$blance=intval($_point+$point);
				$db->query("UPDATE `".DB_PRE."member` SET `".DB_PRE."member`.`amount`={$amount},`".DB_PRE."member`.`point`={$blance} WHERE `".DB_PRE."member`.`id`={$_userid}",true);
				$db->query("UPDATE `".DB_PRE."member_cache` SET `".DB_PRE."member_cache`.`amount`={$amount},`".DB_PRE."member_cache`.`point`={$blance} WHERE `".DB_PRE."member_cache`.`id`={$_userid}",true);
				$buylog=array();
				$buylog = array(
					'userid'      => $_userid,
					'username'    => $_username,
					'type'		  => 'point',
					'blance'      => $blance,////////////  点数余额
					'number'      => $point, 				
					'inputid'     => $_userid,
					'inputer'     => $_username,
					'ip'		  => IP,
					'note'		  => '在线购买点数',
					'time'	      => TIME
			);
				$order['orderid']=$db->insert(DB_PRE.'pay_exchange',$buylog);
				showmsg('操作成功!',$DIRCMS['site_url'].'member/index.php?file=account&action=buylog');
			}
			include member_tlp('account_buypoint');
			break;

		case 'paylog':
			require_once(DIRCMS_ROOT.'include/list.class.php');
			$l=new dircmslist();
			$where='userid='.intval($_userid);
			$orderby="id DESC";
			$page=max(isset($page)?intval($page):1,1);
			$result=$l->getlist(DB_PRE.'pay_user_account',$where,$orderby,$page,14);
			$pages=$l->pages;
			include member_tlp('account_paylog');
			break;
		case 'buylog':
			if(isset($type) && !in_array($type,array('amount','point')))showmsg('非法请求!');
			require_once(DIRCMS_ROOT.'include/list.class.php');
			$l=new dircmslist();
			$where='userid='.intval($_userid);
			$where=isset($type)?$where." AND type='{$type}'":$where;
			$orderby="id DESC";
			$page=max(isset($page)?intval($page):1,1);
			$result=$l->getlist(DB_PRE.'pay_exchange',$where,$orderby,$page,14);
			$pages=$l->pages;
			include member_tlp('account_buylog');
			break;
		default:
			showmsg('参数有误,返回网站首页!',$DIRCMS['site_url']);
			break;
	}
?>