<?php error_reporting(E_ALL ^ E_NOTICE);
session_start();

/*
This page runs an API request to Twilio to verify the code provided by the user
*/

$_SESSION['code'] = trim($_POST['code']);

require __DIR__ . '/vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . "/../");
$dotenv->load();

$isTwillio = $_POST['isTwillio'];
$sid = $_SERVER['SID'];
$token = $_SERVER['TOKEN'];
$serviceid = $_SERVER['SERVICEID'];

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://'.$_SERVER['HOST_URL'].'/api/customer/verifyOtp',
  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 => array('code' => $_SESSION['code'],'phone_number' => $_SESSION['phone'],'is_twilio' => $isTwillio),
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json'
  ),
));
$response = curl_exec($curl);

curl_close($curl);
echo $response;
$result = json_decode($response);
echo $result->success;

if ($result->success == "true") {
    header("Location: verify_pass.php");
} else {
    header("Location: verify_pass.php");
}

?>
