<?php
if (!defined('BASEPATH')) 
	exit('No direct script access allowed');


class Tuniu_callback extends CI_Controller{
    public function __construct(){
        // header("Content-Type: text/html;charset=utf-8");
        parent::__construct();
		$this->load->library('Des');
        $this->load->model("BIZ_train_model");
		$this->load->model("tuniu_model");
    }
	/*
	接收占位回调
	*/
	public function book(){
		$back_json = file_get_contents('php://input');
		log_message('error','预定占座回调：'.$back_json);
		$back_data_one = json_decode(base64_decode($back_json));
		$back_data = array();
		$crypt = new DES();
		$mstr = $crypt->decrypt($back_data_one->data,TUNIU_KEY);
		$back_data_two = json_decode($mstr);

		$back_data['errorMsg'] = $back_data_one->errorMsg;
		$back_data['returnCode'] = $back_data_one->returnCode;
		$back_data['retailOrderId'] = $back_data_two->retailOrderId;
		$back_data['orderId'] = $back_data_two->orderId;
		$back_data['orderAmount'] = $back_data_two->orderAmount;
		$back_data['fromStationCode'] = $back_data_two->fromStationCode;
		$back_data['fromStationName'] = $back_data_two->fromStationName;
		$back_data['toStationCode'] = $back_data_two->toStationCode;
		$back_data['toStationName'] = $back_data_two->toStationName;
		$back_data['cheCi'] = $back_data_two->cheCi;
		$back_data['backtxt'] = $mstr;
		$back_data['status'] = '2';
		//更新预定异步回调信息
		$this->tuniu_model->book_tuniu_order($back_data);
		if($back_data['returnCode'] == '231000'){
			$url = 'http://www.mycht.cn/info.php/apps/train/tuniu_train/confirm_ticket/'.$back_data['retailOrderId'].'/'.$back_data['orderId'];
			echo $url;
			$this->get_http($url,'GET');
		}
	}
	
	public function test(){
		$back_json = "eyJlcnJvck1zZyI6IuihjOeoi+WGsueqgSIsInJldHVybkNvZGUiOjMwMywiZGF0YSI6IjBwZE4zaWlUWE1ISzFPRndGL2Evei9vZzc1dVZsSVpwVzBKTFdnS3dybUlaYWRUSnhEVmNZeW5ib1BZWFBNaWhJazVEVzBhYlBQbDhcbitXdWFCUUVsbmlzcWhBN1ZJSndEZEVvN0JCR0t4RXZ2K0wya090cEkvV01aK0JGTEFJc1hyYi9ZMWM5MTZnUjhIOUROYTdYdXpUV29cbkpzdmI0eTF6aUI5U3BIYWFPM2pQWXZyRHAvMUJCZndPanRuQVNVK2plcGNyMkZoekVJRDRMOHpRV0hMSFNRc2ZoVzVDeHpoQ1J0VUhcbmNFc0tpL212ZEVRcGFEb0diZE1JOWxlWUp4TFZWT0xrNUdCbEh0cGVSNTVBNTNtckVJbExiYU9TNGlRMURCQjUrUjAydzNDYldreHpcblcwWXFFT2U0Znc4R2U2QksyczFlVlYwc1VMSU90YzBZTU00TU4xeUpITHFMdGxieHFKclhjZTJjNi9WYTNjMnJDSk5DN1ltZ004NWVcbi9wYTk2VHNhaytoYUtSNUFncUQ4OXd4aUhETkNlQmEzRHpXMlh2NUZiYVRUc3RJcHRYbTZEaHo5U1Q3ZkJkcTlzYkhSMHdqMlY1Z25cbkV0VWVSR1F5a1hadTJqUDBaZjc5YTFHaCJ9";
		//print_r(base64_encode($back_json));
		$back_data = $this->tuniu_strdecrypt($back_json);
		print_r($back_data);	
		//echo (count($back_data->data->passengers)*5);
	}
	
	/*
	接收取消占位回调
	*/
	public function cancelbook(){
		$back_json = file_get_contents('php://input');
		$sn = 5830;
		log_message('error','取消站位'.$back_json);
	}
	
	/*
	接收确认出票回调
	*/
	public function confirm(){
		$back_json = file_get_contents('php://input');
		//$back_json = 'eyJlcnJvck1zZyI6IuWkhOeQhuaIluaTjeS9nOaIkOWKnyIsInJldHVybkNvZGUiOjIzMTAwMCwiZGF0YSI6eyJyZXRhaWxPcmRlcklkIjoiNDg4MDkzNDQ4XzE1MzM3OTQwMDIiLCJvcmRlcklkIjoiMTE4NDUxMjM5NyJ9fQ==';
		$back_data = json_decode(base64_decode($back_json));
		//print_r($back_data);
		//die();
		log_message('error','确认出票回调：'.$back_json);
		$data = array();
		$data['errorMsg'] = $back_data->errorMsg;
		$data['returnCode'] = $back_data->returnCode;
		$data['retailOrderId'] = $back_data->data->retailOrderId;
		$data['orderId'] = $back_data->data->orderId;
		$data['confirmtxt'] = $back_json;
		
		if($back_data->returnCode != '231000'){
			$data['status'] = '1';
		}else{
			$data['status'] = '4';
			//通过订单号去获取预定时返回的信息
			$bookobj = $this->tuniu_model->get_tuniuorder_info($data['retailOrderId'],$data['orderId']);
			$bookinfo = json_decode($bookobj[0]->tol_booktxt);
			$obj = explode('_',$back_data->data->retailOrderId);
			$add_train_order_data->TOC_COLD_SN = $obj[0];
			$add_train_order_data->TOC_Memo = $back_data->data->orderId;
			$add_train_order_data->TOC_TrainNumber = $bookinfo->cheCi;
			$add_train_order_data->TOC_DepartureDate = $bookinfo->trainDate;
			$add_train_order_data->TOC_TicketCost = $bookinfo->orderAmount;
			$add_train_order_data->FOI_TrainNetOrderNo = $bookinfo->orderNumber;
			$add_train_order_data->poundage = (count($bookinfo->passengers)*3)."";
			$this->tuniu_model->add_grab_order($add_train_order_data);
		}
		$this->tuniu_model->confirm_tuniu_order($data);
	}
	
	/*
	接收退票回调
	*/
	public function return_ticket(){
		$back_json = file_get_contents('php://input');
		log_message('error','退票回调：'.$back_json);
		$back_data = $this->tuniu_strdecrypt($back_json);
		//更新途牛订单列表信息
		$updata_data = array();
		$updata_data['retailOrderId'] = $back_data->data->retailOrderId;
		$updata_data['returnCode'] = $back_data->returnCode;
		$updata_data['errorMsg'] = $back_data->errorMsg;
		$updata_data['returntxt'] = json_encode($back_data);
		$this->tuniu_model->return_tuniu_order($updata_data);
		
		//添加瀚特信息(有问题)
		/*$add_train_order_data = new stdClass();
		$obj = explode('_',$back_data->data->retailOrderId);
		$add_train_order_data->TOC_COLD_SN = $obj[0];
		$add_train_order_data->TOC_Memo = $back_data->data->orderId." ".$back_data->data->returnTickets->passportNo;
		$add_train_order_data->TOC_TrainNumber = $back_data->data->cheCi;
		$add_train_order_data->TOC_TicketCost = $back_data->data->returnMoney;
		$add_train_order_data->FOI_TrainNetOrderNo = null;
		$this->tuniu_model->add_return_order($add_train_order_data);*/
	}
	
	/*
	接收线下退款回调
	*/
	public function return_cash(){
		echo '回调接收线下退款数据';
	}
	
	/*
	接收抢票预定(占位)
	*/
	public function grabTicketBook(){
		$back_json = file_get_contents('php://input');
		$back_data = $this->tuniu_strdecrypt($back_json);
		log_message('error','抢票预定:'.$back_json);
		$update_data = array();
		if($back_data->returnCode == '231000'){
			$update_data['errorMsg'] = $back_data->errorMsg;
			$update_data['returnCode'] = $back_data->returnCode;
			$update_data['retailOrderId'] = $back_data->data->retailOrderId;
			$update_data['orderId'] = $back_data->data->orderId;
			$update_data['fromStationCode'] = $back_data->data->fromStationCode;
			$update_data['fromStationName'] = $back_data->data->fromStationName;
			$update_data['toStationCode'] = $back_data->data->toStationCode;
			$update_data['toStationName'] = $back_data->data->toStationName;
			$update_data['cheCi'] = $back_data->data->cheCi;
			$update_data['orderAmount'] = $back_data->data->orderAmount;
			$update_data['booktxt'] = json_encode($back_data);
			//更新数据库信息
			$this->tuniu_model->grab_tuniu_order($update_data);
			
			//添加瀚特信息
			$add_train_order_data = new stdClass();
			$obj = explode('_',$back_data->data->retailOrderId);
			$add_train_order_data->TOC_COLD_SN = $obj[0];
			$add_train_order_data->TOC_Memo = $back_data->data->orderId;
			$add_train_order_data->TOC_TrainNumber = $back_data->data->cheCi;
			$add_train_order_data->TOC_DepartureDate = $back_data->data->trainDate;
			$add_train_order_data->TOC_TicketCost = $back_data->data->orderAmount;
			$add_train_order_data->FOI_TrainNetOrderNo = $back_data->data->orderNumber;
			$add_train_order_data->poundage = (count($back_data->data->passengers)*5)."";
			$this->tuniu_model->add_grab_order($add_train_order_data);
			//print_r($update_data['booktxt']);
		}else{
			$update_data['retailOrderId'] = $back_data->data->retailOrderId;
			$update_data['errorMsg'] = $back_data->errorMsg;
			$update_data['returnCode'] = $back_data->returnCode;
			$this->tuniu_model->update_status($update_data);
		}
	}
	
	/*
	接收取消抢票
	*/
	public function cancelTicketBook(){
		$back_json = file_get_contents('php://input');
		log_message('error','取消抢票:'.$back_json);
		$back_data = json_decode(base64_decode($back_json));
		$update_data = array();
		$update_data['errorMsg'] = $back_data->errorMsg;
		$update_data['returnCode'] = $back_data->returnCode;
		$update_data['orderId'] = $back_data->data->orderId;
		$update_data['retailOrderId'] = $back_data->data->retailOrderId;
		$this->tuniu_model->cancelgragticket($update_data);
	}
	
	/*
	接收改签预定
	*/
	public function change_occupy(){
		echo '回调接收改签预定数据';
	}
	
	/*
	接收改签确认
	*/
	public function change_confirm(){
		echo '回调接收改签确认数据';
	}
	
	/*
	接收改签预定
	*/
	public function change_cancel(){
		echo '回调接收改签取消数据';
	}
	
	//解密方法
	public function tuniu_strdecrypt($str){
		$back_data_one = json_decode(base64_decode($str));
		$back_data = array();
		$crypt = new DES();
		$mstr = $crypt->decrypt($back_data_one->data,TUNIU_KEY);
		$back_data_one->data = json_decode($mstr);
		return $back_data_one;
	}
	
	
	
	//发送请求函数
	public function get_http($url, $data = '', $method = 'GET') {
        $curl = curl_init(); // 启动一个CURL会话  
		curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址  
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查  
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在  
		curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器  
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转  
		curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer  
		if ($method == 'POST' && !empty($data)) {
			curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求  
			curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
			curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
		}
		curl_setopt($curl, CURLOPT_TIMEOUT, 45); // 设置超时限制防止死循环  
		curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容  
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回  
		$tmpInfo = curl_exec($curl); // 执行操作  
		$errno = curl_errno($curl);
		if ($errno !== 0) {
			return false;
			echo $errno . curl_error($curl); //记录错误日志
		}
		curl_close($curl); //关闭CURL会话  
		return $tmpInfo; //返回数据
    }
}   