<?php session_start();
require_once("./../../config/config.php");
//require_once("../config/commonsetting.php");

$myformId=SESS_MASTER;
$myIp= ipCheck() ;
$dateceated=date("Y-m-d G:i:s");
$createdby='0';
$client_id		=   secureSuperGlobalPOST($_REQUEST[SESS_MASTER.'client_id']);
$base64code		=   secureSuperGlobalPOST($_REQUEST[SESS_MASTER.'base64code']);
$stat	=   secureSuperGlobalPOST($_REQUEST[SESS_MASTER.'stat']);
$api_type	=   secureSuperGlobalPOST($_REQUEST[SESS_MASTER.'api_type']);


 if($_POST){
  try{
      if($stat=='0'){
          $olgaquery="INSERT INTO api_coupan SET createdIp='$myIp',datecreated='$dateceated',client_id='$client_id',api_type='$api_type',base64code='$base64code'"; 
		$stmt = $conn->prepare($olgaquery);
		if($stmt->execute()){
			$last_insert_id = $conn->lastInsertId();

			$_SESSION["created"] = $last_insert_id ;
			$json['status'] = true;
            $json['message'] =  "Save Successfully";
			// echo "<script>window.location='../affiliatefuture.php';</script>";;
		}
          
      }else{
          $olgaquery="UPDATE api_coupan SET createdIp='$myIp',datecreated='$dateceated',client_id='$client_id',base64code='$base64code' WHERE api_type='$api_type' "; 
		$stmt = $conn->prepare($olgaquery);
		if($stmt->execute()){
			$last_insert_id = $conn->lastInsertId();
            $curl = curl_init();

            curl_setopt_array($curl, array(
              CURLOPT_URL => 'https://api.admitad.com/token/',
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_ENCODING => '',
              CURLOPT_MAXREDIRS => 10,
              CURLOPT_TIMEOUT => 0,
              CURLOPT_FOLLOWLOCATION => true,
              CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST => 'POST',
              CURLOPT_POSTFIELDS => 'grant_type=client_credentials&client_id='.$client_id.'&scope=advcampaigns%20banners%20websites%20coupons%20public_data',
              CURLOPT_HTTPHEADER => array(
                'Authorization: Basic '.$base64code,
                'Content-Type: application/x-www-form-urlencoded',
                'Cookie: path_language=en'
              ),
            ));
            
            $response = curl_exec($curl);
            
            curl_close($curl);
          //  echo $response;
            $result = json_decode($response);
            if($result != ""){
              $access_token =  $result->access_token;
              $olgaquery="UPDATE api_coupan SET datecreated='$dateceated',access_token='$access_token' WHERE api_type='$api_type' "; 
              $stmt = $conn->prepare($olgaquery);
              if($stmt->execute()){

			$_SESSION["created"] = $last_insert_id ;
			$json['status'] = true;
            $json['message'] =  "Save Successfully";
			// echo "<script>window.location='../affiliatefuture.php';</script>";;
              }
            }
            else{
              echo "sometime is wrong";
            }
		}
          
      }
		
 
 
   }
         
        // show error
        catch(PDOException $exception){
            die('ERROR: ' . $exception->getMessage());
        }
    }

    echo json_encode($json);
?>