<?php
namespace member\controller;

/**
 * 106云盘自动发卡平台网盘系统
 * ============================================================================
 * 版权所有 2016-2028 扎兰屯市姝妍计算机服务有限公司，并保留所有权利。
 * 网站地址: http://www.106ym.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 */
class Pay extends \source\Controller {
	
	protected function _initialize(){
		/* 读取站点配置 */
        $config = api('Config/lists');
        C($config); //添加配置
    }
	
	/**
	 * 模板解析
	 */
	function view($data){
		foreach ($data as $k => $v) {
			$this->$k=$v;
		}
		$this->display($data['tpl']);
	}
	
	/**
	 * 会员充值
	 */
	public function recharge(){
		$pay_way=I('pay_way');
		$url = "http://{$_SERVER['HTTP_HOST']}";
		$orderno=date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
		//额外参数
		$amount = I('amount');
		$uid=session("user_auth.id");
		$passback_params=array(
			'uid'=>$uid,
			'no'=>$orderno,
			'amount'=>$amount,
			'ctime'=>time(),
			'pay_time'=>time(),
			'status'=>1,
			'pay_way'=>$pay_way,
		);
		$passback_params=json_encode($passback_params);
		//额外参数名称
		$passback_params_name=md5($passback_params);
		S($passback_params_name,$passback_params);
		$param= array(
			'title'=>'网盘续费',
			'orderno'=>$orderno,
			'amount'=>$amount,
			'notify_url'=>"http://{$_SERVER['HTTP_HOST']}/pay/recharge_notify/pay_way/".$pay_way,
			'return_url'=>"http://{$_SERVER['HTTP_HOST']}/v/lists/model_id/35/uid/".$uid,
			'other'=>$passback_params_name,
			'model'=>'recharge',
		);
		$pay_name=M('zhifutongdao')->where(array('id'=>$pay_way))->getField('name');
		$class  =   "\pay\\".ucfirst($pay_name);
		$pay=new $class();
		$pay->pc($param);
	}
	
	/**
	 * 会员充值异步回调
	 */
	public function recharge_notify(){
		$info_pay=M('zhifutongdao')->where(array('id'=>$_GET['pay_way']))->find();
		$class  =   "\pay\\".ucfirst($info_pay['name']);
		$pay=new $class();
		$redata=$pay->check();
		if($redata){
			$other=S($redata['other']);
			$other=json_decode($other,true);
			$other['pay_no']=$redata['pay_id'];
			$other['price']=$redata['amount']-sprintf("%.2f",$redata['amount']*$info_pay['feilv']);
			$other['pay_price']=sprintf("%.2f",$redata['amount']*$info_pay['feilv']);
			$other['pay_way']=$redata['pay_way'];
			if($other['uid']>0){
			    D('pub/Recharge')->create($other);
				D('pub/Recharge')->add();
			}
			$re=$pay->notifyReturn();
			echo $re;
		}
    }
	
	/**
	 * 网盘续费下单
	 */
	public function wpxf(){
		$pay_way=I('pay_way');
		$orderno=date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
		$uid=session("user_auth.id");
		$user = M('member')->find($uid);
		$type =  M('wangpan_price')->find(I('shichang'));
		$amount = $type['price'];
		$data_two = array(
			'uid'=>$uid,
			'ctime'=>time(),
			'wangpan_id'=>I('wangpan_id'),
			'no'=>$orderno,
			'status'=> 0,
			'days'=>$type['days'],
			'price'=>$amount,
			'shichang'=>$type['shichang'],
			'type'=>I('type')
		);
		$re = D('pub/WangpanLog')->add($data_two);
		$param= array(
			'title'=>'网盘续费',
			'orderno'=>$orderno,
			'amount'=>$amount,
			'notify_url'=>"http://{$_SERVER['HTTP_HOST']}/pay/wpxf_notify/pay_way/".$pay_way,
			'return_url'=>"http://{$_SERVER['HTTP_HOST']}/wangpan_config/",
			'model'=>'WangpanLog',
		);
		$pay_name=M('zhifutongdao')->where(array('id'=>$pay_way))->getField('name');
		$class  =   "\pay\\".ucfirst($pay_name);
		$pay=new $class();
		$pay->pc($param);
	}
	
	/**
	 * 网盘续费回调
	 */
	public function wpxf_notify(){
		$info_pay=M('zhifutongdao')->where(array('id'=>$_GET['pay_way']))->find();
		$class  =   "\pay\\".ucfirst($info_pay['name']);
		$pay=new $class();
		$redata=$pay->check();
		if($redata){
			$where=array(
				'no'=>$redata['orderno'],
				'amount'=>$redata['amount'],
				'status'=>0,
			);
			D('pub/WangpanLog')->where($where)->save(array('status'=>1,'no'=>$redata['orderno'],'pay_way'=>$redata['pay_way']));
			$re=$pay->notifyReturn();
			echo $re;
		}
	}
	
	
	/**
	 * 网盘升级下单
	 */
	public function wpsj(){
		$pay_way=I('pay_way');
		$orderno=date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
		$uid=session("user_auth.id");
		$userconfig=userconfig_get($uid);
		$wangpan_type_old=$userconfig['wangpan_id'];
		$wangpan_type_new=I('wangpan_id');
		
		//终身
		$wangpan_config=M('wangpan_config')->find($wangpan_type_new);
		if($wangpan_config['iszhongshen']==1){
			$amount = M('wangpan_price')->where(array('type_id'=>$wangpan_type_new))->getField('price');
			$data_two = array(
				'uid'=>I('uid'),
				'ctime'=>time(),
				'wangpan_id'=>I('wangpan_id'),
				'no'=>$orderno,
				'status'=> 0,
				'days'=>0,
				'price'=>$amount,
				'pay_way'=>I('pay_way'),
				'shichang'=>2145888000,
				'type'=>I('type')
			);
			$re = M('wangpan_log')->add($data_two);
		}else{
			//距离过期天数
			$edays = floor(($userconfig['wangpan_etime']-time())/86400);
			if( $edays > 0){
				//升级网盘的类型
				$wangpan_days = M('wangpan_price')->where(array('type_id'=>$wangpan_type_new))->getField('days',true);
				
				$count=count($wangpan_days);
				$arr=array();
				for ($i=0; $i <$count ; $i++) { 
					$arr[$i]=abs($edays-$wangpan_days[$i]);
				}
				$min= min($arr);
				for ($i=0; $i <$count ; $i++) { 
					if ($min==$arr[$i]) {
						$days=$wangpan_days[$i];
					}
				}
				$price_old=M('wangpan_price')->where(array('days'=>$days,'type_id'=>$wangpan_type_old))->find();
				$price_old=$price_old['price']/$price_old['days'];
				$price_new=M('wangpan_price')->where(array('days'=>$days,'type_id'=>$wangpan_type_new))->find();
				$price_new=$price_new['price']/$price_new['days'];
				$amount=($price_new-$price_old)*$edays;
				//差价
				$amount=sprintf("%.2f",$amount);
				
				$data_one = array(
					'uid'=>$uid,
					'ctime'=>time(),
					'wangpan_id'=>$wangpan_type_new,
					'no'=>$orderno,
					'status'=> 0,
					'days'=>$edays,
					'price'=>$amount,
					'pay_way'=>I('pay_way'),
					'shichang'=>'',
					'type'=>I('type')
				);
				$re = M('wangpan_log')->add($data_one);
				
			}else{
				//当前为免费型，免费型升级  
				$type =  M('wangpan_price')->find(I('shichang'));
				$amount = $type['price'];
				$data_two = array(
					'uid'=>I('uid'),
					'ctime'=>time(),
					'wangpan_id'=>I('wangpan_id'),
					'no'=>$orderno,
					'status'=> 0,
					'days'=>$type['days'],
					'price'=>$amount,
					'pay_way'=>I('pay_way'),
					'shichang'=>$type['shichang'],
					'type'=>I('type')
				);
				$re = M('wangpan_log')->add($data_two);
			}
		}
		
		$param= array(
			'title'=>'网盘升级',
			'orderno'=>$orderno,
			'amount'=>$amount,
			'notify_url'=>"http://{$_SERVER['HTTP_HOST']}/pay/wpsj_notify/pay_way/".$pay_way,
			'return_url'=>"http://{$_SERVER['HTTP_HOST']}/wangpan_config/",
			'model'=>'WangpanLog',
		);
		$pay_name=M('zhifutongdao')->where(array('id'=>$pay_way))->getField('name');
		$class  =   "\pay\\".ucfirst($pay_name);
		$pay=new $class();
		$pay->pc($param);
	}
	
	/**
	 * 网盘升级回调
	 */
	public function wpsj_notify(){
		$info_pay=M('zhifutongdao')->where(array('id'=>$_GET['pay_way']))->find();
		$class  =   "\pay\\".ucfirst($info_pay['name']);
		$pay=new $class();
		$redata=$pay->check();
		if($redata){
			$where=array(
				'no'=>$redata['orderno'],
				'amount'=>$redata['amount'],
				'status'=>0,
			);
			D('pub/WangpanLog')->where($where)->save(array('status'=>1,'no'=>$redata['orderno'],'pay_way'=>$redata['pay_way']));
			$re=$pay->notifyReturn();
			echo $re;
		}
	}
    
    
	
	/**
	 * 微信支付检测是否支付成功
	 * $orderno 订单号
	 * $model 订单存储模型
	 */
	public function wxpay_check($orderno,$model){
		$where=array(
			'no'=>$orderno,
		);
		$status=D($model)->where($where)->getField('status');
		$this->ajaxReturn(array('status'=>$status));
    }
}