<?php
$data = file_get_contents('php://input');
$orderDetails = $data;

$fp = fopen('data.txt', 'w');
		fwrite($fp, $orderDetails);
		fclose($fp);

exit;
respondOK();
// Order detail has data in JSON format. 
// Do you activity below this






function respondOK($text = null) {
	// check if fastcgi_finish_request is callable
	if(is_callable('fastcgi_finish_request')) {
		if($text !== null) {
			echo $text;
		$fp = fopen('data.txt', 'w');
		fwrite($fp, $text);
		fclose($fp);
		}
		/*
		 * http://stackoverflow.com/a/38918192
		 * This works in Nginx but the next approach not
		 */
		session_write_close();
		fastcgi_finish_request();

		return;
	}

	ignore_user_abort(true);

	ob_start();

	if($text !== null) {
		echo $text;
		$fp = fopen('data.txt', 'w');
		fwrite($fp, $text);
		fclose($fp);
	
	}

	$serverProtocol = filter_input(INPUT_SERVER, 'SERVER_PROTOCOL', FILTER_SANITIZE_STRING);
	header($serverProtocol . ' 200 OK');
	// Disable compression (in case content length is compressed).
	header('Content-Encoding: none');
	header('Content-Length: ' . ob_get_length());

	// Close the connection.
	header('Connection: close');

	ob_end_flush();
	ob_flush();
	flush();
}



die('here');

$orderItemTemp = array('rowNo'=>1,'itemId'=>"133",'itemReferenceCode'=>"133",'salePrice'=>280,
'quantity'=>1,'suppliedQty'=>1,'itemAmount'=>280,'taxPercentage'=>"0.0000",'discountPercentage'=>0,
'itemRemarks'=>'','freeQty'=>0);

$orderItems = array($orderItemTemp);

$salesOrder = array();
$salesOrder['salesOrder'] = array("onlineReferenceNo"=>"100000002",
      "onlineChildReferenceNo"=>"100000002",
      "createdAt"=>"1483945201",
      "status"=>"pending",
      "totalQuantity"=>2,
      "totalFreeQty"=>0,
      "totalAmount"=>380,
      "paymentMode"=>1,
      "totalTaxAmount"=>0,
      "totalDiscountAmount"=>0,
      "shippingId"=>"2",
      "shippingName"=>"Dileep M",
      "shippingAddress1"=>"Velachery",
      "shippingAddress2"=>"",
      "shippingPlace"=>"Chennai",
      "shippingState"=>"",
      "shippingCountry"=>"IN",
      "shippingPincode"=>"600042",
      "shippingMobile"=>"8015172175",
      "outletId"=>"225",
      "shippingEmail"=>"dileepan.m@gofrugal.com",
      "shippingCharge"=>10,
      "shippingMethod"=>"Flat Rate - Fixed",
      "shipmentItems"=>2,
      "shipmentWeight"=>0,
      "customerId"=>null,
      "customerName"=>"Dileep M",
      "customerAddressLine1"=>"Velachery",
      "customerAddressLine2"=>"",
      "customerArea"=>"",
      "customerCity"=>"Chennai",
      "customerState"=>"",
      "customerCountry"=>"IN",
      "customerPincode"=>"600042",
      "customerMobile"=>"8015172175",
      "customerPhone"=>"8015172175",
      "customerEmail"=>"dileepan.m@gofrugal.com",
      "orderItems"=>$orderItems);

/*echo '<pre>';
print_r($salesOrder);
exit;*/

$salesJson = '{
   "salesOrder":{
      "onlineReferenceNo":"100000002",
      "onlineChildReferenceNo":"100000002",
      "createdAt":"1483945201",
      "status":"pending",
      "totalQuantity":2,
      "totalFreeQty":0,
      "totalAmount":380,
      "paymentMode":1,
      "totalTaxAmount":0,
      "totalDiscountAmount":0,
      "shippingId":"2",
      "shippingName":"Dileep M",
      "shippingAddress1":"Velachery",
      "shippingAddress2":"",
      "shippingPlace":"Chennai",
      "shippingState":"",
      "shippingCountry":"IN",
      "shippingPincode":"600042",
      "shippingMobile":"8015172175",
      "outletId":"225",
      "shippingEmail":"dileepan.m@gofrugal.com",
      "shippingCharge":10,
      "shippingMethod":"Flat Rate - Fixed",
      "shipmentItems":2,
      "shipmentWeight":0,
      "customerId":null,
      "customerName":"Dileep M",
      "customerAddressLine1":"Velachery",
      "customerAddressLine2":"",
      "customerArea":"",
      "customerCity":"Chennai",
      "customerState":"",
      "customerCountry":"IN",
      "customerPincode":"600042",
      "customerMobile":"8015172175",
      "customerPhone":"8015172175",
      "customerEmail":"dileepan.m@gofrugal.com",
      "orderItems":[
         {
            "rowNo":1,
            "itemId":"133",
            "itemReferenceCode":"133",
            "salePrice":280,
            "quantity":1,
            "suppliedQty":1,
            "itemAmount":280,
            "taxPercentage":"0.0000",
            "discountPercentage":0,
            "itemRemarks":"",
            "freeQty":0
         },
         {
            "rowNo":2,
            "itemId":"387",
            "itemReferenceCode":"387",
            "salePrice":90,
            "quantity":1,
            "suppliedQty":1,     
            "itemAmount":90,
            "taxPercentage":"0.0000",
            "discountPercentage":0,
            "itemRemarks":"",
            "freeQty":0
         }
      ]
   }
}';

echo '<pre>';
print_r($salesJson);
//exit;


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://mybooster.true-pos.com/TruePOS/api/v1/salesOrders?mode=new");
curl_setopt($ch, CURLOPT_POSTFIELDS,$salesJson);  //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$headers = [
    'Content-Type=> application/json',
    'X-Auth-Token=> 59B2D9AB5F23F0B583158882F1B2E09B63B98B494B541C44504FE65F9190781F5A9E95755166E523',
    'Accept=> application/json'    
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$server_output = curl_exec ($ch);
echo $server_output;

if (curl_error($ch)) {
    echo curl_error($ch);
}

curl_close ($ch);

$storesArray = json_decode($server_output,true);

echo '<pre>';
print_r($storesArray);
?>