<?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        // fetch RAW input
        $json = file_get_contents('php://input');

        // decode json
        $charge = json_decode($json);

        function getApiToken(){
			$tokens = array("sk_1x1_69909f2997c8e3bce083e3b5ed144694ab2178f6be2266a3d6972c2215f19df7","sk_2x1_1f3f299a07e8cc4fccabaca0e315ab667af1743c74775a43887eb5f9ec7a083b","sk_2x1_05c998ecfb5f623713b4f19f45d7f994b742e55bb8049ca83bae2ad5b0aa3593");
			$k = array_rand($tokens);
			return $token = $tokens[$k];
		}


		function orderUpdate($order_id,$date){
			$token = getApiToken();
	        $ch = curl_init("https://api.rechargeapps.com/orders/".$order_id."/change_date");
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	        curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','x-recharge-access-token: '.$token));
	        curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($date));
	        $response1 = json_decode(curl_exec($ch));
	        if(isset($response1->error)){
	            $result = orderUpdateError($order_id,$date);
	            if(isset($result->error)){
	              	$result1 = orderUpdateError($order_id,$date);
	              	if(isset($result1->error)){
	                	$result2 = orderUpdateError($order_id,$date);
	                	if(isset($result2->error)){
	                  		$result3 = orderUpdateError($order_id,$date);
	                  		return $result3;
	                	}else{
	                  		return $result2;
	                	}
	              	}else{
	                	return $result1;
	              	}
	            }else{
	              	return $result;
	            }
          	}else{
            	return $response1;
          	}
	    }
        function orderUpdateError($order_id,$date){

        	$token = getApiToken();
          	$ch = curl_init("https://api.rechargeapps.com/orders/".$order_id."/change_date");
          	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
          	curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','x-recharge-access-token: '.$token));
          	curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($date));
          	$response1 = json_decode(curl_exec($ch));

          	return $response1;

        }
		foreach ($charge->charge->line_items as $key => $value) {

			$token = getApiToken();
			$ch = curl_init("https://api.rechargeapps.com/subscriptions/".$value->subscription_id);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
			curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','x-recharge-access-token: '.$token));
			$subscription = json_decode(curl_exec($ch));
			$checkReplace = '';
			foreach ($subscription->subscription->properties as $k => $v) {
				if($v->name=="trial"){
					$checkReplace = $v->value;
				}
			}

			file_put_contents('trial.txt', "===========Paid=====No=======",FILE_APPEND);
        	file_put_contents('trial.txt', $charge->charge->email,FILE_APPEND);

			if($checkReplace=="replace"){
				file_put_contents('trial.txt', "===========Paid============",FILE_APPEND);
        		file_put_contents('trial.txt', $charge->charge->email,FILE_APPEND);
				$month = date("M");
				$day = date("d");
				$year = date("Y");
				$month = date("M");
	            if($month=="Sep" || $month=="Oct" || $month=="Nov"){
	                $nextDate = date('Y', strtotime('+1 year'))."-08-01";
	            }elseif($month=="Dec") {
	                $nextDate = date('Y', strtotime('+1 year'))."-11-01";
	            }elseif($month=="Mar" || $month=="Apr" || $month=="May"){
	                $nextDate = date('Y', strtotime('+1 year'))."-02-01";
	            }elseif($month=="Jun" || $month=="Jul" || $month=="Aug"){
	                $nextDate = date('Y', strtotime('+1 year'))."-05-01";
	            }elseif($month=="Jan" || $month=="Feb"){
	                  $nextDate = date('Y')."-11-01";
	            }
				if($charge->charge->id!=''){
					$ch = curl_init("https://api.rechargeapps.com/orders?charge_id=".$charge->charge->id);
					//$ch = curl_init("https://api.rechargeapps.com/orders?charge_id=309430138");
	                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
	                curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','x-recharge-access-token: '.$token));
	                $response = json_decode(curl_exec($ch));

	                $err = curl_error($curl);

	                curl_close($ch);

	                if ($err) {
	                    echo "cURL Error #:" . $err;
	                } else {
                      	foreach ($response->orders as $count=>$order) {
	                        if($count==0){
	                          if($month=="Sep" || $month=="Oct" || $month=="Nov"){
	                              $nextDate1 = date("Y",strtotime('+1 year'))."-05-01";
	                           }elseif($month=="Dec") {
	                              $nextDate1 = date("Y",strtotime('+1 year'))."-08-01";
	                           }elseif($month=="Mar" || $month=="Apr" || $month=="May"){
	                              $nextDate1 = date('Y')."-11-01";
	                           }elseif($month=="Jun" || $month=="Jul" || $month=="Aug"){
	                              $nextDate1 = date('Y', strtotime('+1 year'))."-02-01";
	                           }elseif($month=="Jan" || $month=="Feb"){
	                                $nextDate1 = date('Y')."-08-01";
	                           }
	                        }
	                        if($count==1){
	                          if($month=="Sep" || $month=="Oct" || $month=="Nov"){
	                              $nextDate1 = date("Y",strtotime('+1 year'))."-02-01";
	                           }elseif($month=="Dec") {
	                              $nextDate1 = date("Y",strtotime('+1 year'))."-05-01";
	                           }elseif($month=="Mar" || $month=="Apr" || $month=="May"){
	                              $nextDate1 = date('Y')."-08-01";
	                           }elseif($month=="Jun" || $month=="Jul" || $month=="Aug"){
	                              $nextDate1 = date('Y')."-11-01";
	                           }elseif($month=="Jan" || $month=="Feb"){
	                                $nextDate1 = date('Y')."-05-01";
	                           }
	                        }
	                        if($count==2){
	                          if($month=="Sep" || $month=="Oct" || $month=="Nov"){
	                              $nextDate1 = date('Y')."-11-01";
	                           }elseif($month=="Dec") {
	                              $nextDate1 = date("Y",strtotime('+1 year'))."-02-01";
	                           }elseif($month=="Mar" || $month=="Apr" || $month=="May"){
	                              $nextDate1 = date('Y')."-05-01";
	                           }elseif($month=="Jun" || $month=="Jul" || $month=="Aug"){
	                              $nextDate1 = date('Y')."-08-01";
	                           }elseif($month=="Jan" || $month=="Feb"){
	                                $nextDate1 = date('Y')."-02-01";
	                           }
	                        }
	                        $data = array("scheduled_at" => $nextDate1);
                          	$res = orderUpdate($order->id,$data);
                          	echo "<pre>"; print_r($res);
                      	}
	                 }
				}
				$token = getApiToken();
				$data = array("date" => $nextDate);
	          	$ch = curl_init("https://api.rechargeapps.com/subscriptions/".$value->subscription_id."/set_next_charge_date");
	          	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	          	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
	          	curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','x-recharge-access-token: '.$token));
	          	curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data));
	          	$response = curl_exec($ch);
	          	//echo "<pre>"; print_r($response);
	          	curl_close($ch);
				//echo "<pre>"; print_r($subscription);
			}
			
		}
    }else{
    	echo "empty";
    }
	
	