<?php
    require_once("../../autoloader.php");
    header('Access-Control-Allow-Origin: *');
    header('Content-Type: application/json');
    header("Access-Control-Allow-Methods: POST");
    header("Allow: POST, OPTIONS, PUT, DELETE");
    header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Access-Control-Allow-Origin");

    $headers = apache_request_headers();
    $response = array();
    $controller = new SubscriberModel;

   
    date_default_timezone_set('Africa/Lagos');
          

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    

    $id = $_POST['user_id'];
   $phone = $_POST['phone'];
   $network = $_POST['network'];
      $name = $_POST['name'];
   
  
   
    




 $result =  $controller->saveBeneficiary($id,$network,$phone,$name);
    file_put_contents("airtime_fail_log.txt",json_encode($result, JSON_UNESCAPED_SLASHES));
    
  $ress = Json_decode($result);
   
 if($ress->status=="fail"){
        header('HTTP/1.0 400 Invalid Network Id');
        $response = array();
        $response['status']="fail";
        $response['msg'] = $ress->msg;
        file_put_contents("airtime_fail_log.txt",json_encode($response))
        ;
        echo json_encode($response);
        exit();
    }
    else{
         header('HTTP/1.0 200 Invalid Network Id');
       $response = array('status' => 'success');
      $data = array(
     
    "status" => "success",
     
   
);

//file_put_contents("airtime_fail_log.txt",json_encode($result, JSON_UNESCAPED_SLASHES));
$jsonString = json_encode($data, JSON_UNESCAPED_SLASHES);
echo $jsonString;
exit();
    
    }

    
} else {
    
    $response = array('status' => 'error', 'message' => 'Invalid request method');
    file_put_contents("airtime_fail_log.txt",json_encode($response))
        ;
    echo json_encode($response);
}


?>