<?php

Booking in progress .......
<br>
Data: 
<?php

	$pnrcode =  $_GET['pnrcode'];

	if($pnrcode){
		print_r($pnrcode);
		exit;
	}

	$api_key = do_shortcode('');
	$api_url = do_shortcode('');


	$period_id = $_GET['period_id'];
	$email = $_GET['email'];
	$last_name = $_GET['last_name'];
	$first_name = $_GET['first_name'];
	$phone_number = $_GET['phone_number'];
	$more_info = $_GET['more_info'];
	$grand_total = $_GET['grand_total'];
	$sum_child_nums = $_GET['sum_child_nums'];
	$sum_nums = $_GET['sum_nums'];
	$nobed_nums = $_GET['nobed_nums'];
	$child_nums = $_GET['child_nums'];
	$triple_nums = $_GET['triple_nums'];
	$single_nums = $_GET['single_nums'];
	$twin_nums = $_GET['twin_nums'];
	$code = $_GET['code'];

	
	/*
	echo "API KEY: {$api_key}  <br>";
	echo "API Endpoint: {
	} <br><br>";
	echo "period_id = {$period_id} <br>
			email = {$email} <br>
			last_name = {$last_name} <br>first_name = {$first_name} <br>phone_number = {$phone_number} <br>more_info = {$more_info} <br>grand_total = {$grand_total} <br>sum_child_nums = {$sum_child_nums} <br>sum_nums = {$sum_nums} <br>nobed_nums = {$nobed_nums} <br>child_nums = {$child_nums} <br>triple_nums = {$triple_nums} <br>single_nums = {$single_nums} <br>twin_nums = {$twin_nums} <br>code = {$code} <br>";
	*/

	$body = array(
		'apikey' => sanitize_text_field( $api_key ),
		'mode'	=> 'bookingdetails',
		'period_id' => $period_id ,
		'adults_double' => $twin_nums,
		'adults_single' => $single_nums,
		'adults_triple' => $triple_nums,
		'child_withbed' => $child_nums,
		'child_nobed' => $nobed_nums,
		'first_name' => sanitize_text_field( $first_name ),
		'last_name' => sanitize_text_field( $last_name ),
		'email'   => sanitize_email( $email ),
		'phone_number' => sanitize_text_field( $phone_number ),
	);

	$args = array(
		'body'        => $body,
		'timeout' => 30,
		'sslverify' => false
	);


	echo '<pre>'.print_r($args,true).'</pre>';
	
	echo '<pre>'.print_r($api_url,true).'</pre>';
// // Debug
	// do_action('wow_insert_debug_log','Submit Booking', $api_url.': '.print_r($body,true) );

 	$response = wp_remote_post( $api_url, $args );
	echo '<pre>'.print_r($response,true).'</pre>';

	$body = wp_remote_retrieve_body( $response );

	$responceData = ( ! is_wp_error( $response ) ) ? json_decode( $body, true ) : null;

	$url_redirect = get_permalink( get_page_by_path( '/wow-tour/error', OBJECT, 'page' ));	

	echo '<pre>'.print_r($responceData,true).'</pre>';

	if(is_null($responceData)){
		// error
		// print_r(get_permalink( get_page_by_path( '/processing/', OBJECT, 'page' ) ));
		// print_r(get_permalink( get_page_by_path( '/wow-tour/thank-you', OBJECT, 'page' ) ));
		echo "<br><br>";
		// print_r($_SERVER['REQUEST_URI']);	
		
	}else{
		// success
		if($responceData['success'] == 'True'){
			// Booking Success
			// 			
			// print_r($responceData);
			$url_thankyou = get_permalink( get_page_by_path( '/thank-you', OBJECT, 'page' ));
			if(strpos($url_thankyou,'processing') > 0){
				$url_thankyou = '/thank-you';
			}
			$url_params = explode('?', $_SERVER['REQUEST_URI']);
			$url_redirect = "{$url_thankyou}?{$url_params[1]}&pnrcode={$responceData['data']['pnrcode']}";
			// echo $url_redirect;
			//wp_redirect( $url_redirect ); exit; 
			
			
			
		}else{
			// Booking Failed
			 print_r($responceData);
		}
	}

	//wp_redirect( $url_redirect ); exit; 
// 	print_r( '>>> redirect to : '.$url_redirect.' <<<<<<<');