<?php
header('Access-Control-Allow-Origin: *');
header("Content-type: application/json; charset=utf-8");

include '../../utils/database.php';
include '../../utils/helper.php';
include '../../utils/tokens.php';



// showErrors();

$needVerify = "false";


$rand = rand(10001, 99999);

$phoneFirst = $_POST['phoneNumber'];


// $codeFirst = $_POST['code'];
// $verificationFirst = $_POST['verification'];
// if (sizeof($_POST) == 0) {
//   $phoneFirst = $_GET['phoneNumber'];
//   $codeFirst = $_GET['code'];
//   $verificationFirst = $_GET['verification'];
// }

$phoneFirst = $_GET['phoneNumber'];
$codeFirst = $_GET['code'];
$verificationFirst = $_GET['verification'];


$phone = convertPersianToEnglish($phoneFirst);

$verification = convertPersianToEnglish($verificationFirst);
$phone = str_replace(" ", "", $phone);

if (substr($phone, 0, 3) == "009") {

  $phone = str_replace("009", "09", $phone);
}

$phone = phoneFormatter($phone);
//$phone = str_replace(" " , "" , $phone);
if (is_numeric($phone)) {
  $phone = phoneFormatter($phone);
} else {


  // if ($phone == "direct"){

  // }
  // else {
  //     if(strpos($phone, '@') !== false){
  //         $phone = "gg".$phone;
  //     }
  //     else {
  //         $response['success'] = 'false';
  //         $response['message'] = 'لطفا آدرس ایمیل را کامل وارد کنید. مثال: matcha@gmail.com';

  //         $json_response = json_encode($response);
  //         echo $json_response;
  //         die();
  //     }
  // }
}

$payCode = convertPersianToEnglish($codeFirst);
$payCode = str_replace(" ", "", $payCode);
$persona = null;
$payment = null;

$code = "";

$currentTime = time();
$token = null;
$headers = apache_request_headers();

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname_process);
mysqli_set_charset($conn, "utf8");
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
$checkMobile = false;



$result = $conn->query("SELECT * FROM mobiles WHERE `phone`='$phone'");

if ($result->num_rows > 0) {

  while ($row = $result->fetch_assoc()) {
    $codeTime = $row["timestamp"];
    if ($row["token"] != $verification) {
      $response['success'] = 'false';
      $response['message'] = 'کد وارد شده اشتباه است';
      $json_response = json_encode($response);
      echo $json_response;
      die();
    }

    if ((($currentTime - $codeTime) / 60) > 3) {
      $response['success'] = 'false';
      $response['message'] = 'کد ورود منقضی شده است';
      $json_response = json_encode($response);
      echo $json_response;
      die();
    }
  }
} else {

  $response['success'] = 'false';
  $response['message'] = 'شماره تلفن یافت نشد';
  $json_response = json_encode($response);
  echo $json_response;
  die();
}


$conn->query("UPDATE payment SET `phone`='$phone' WHERE `code`='$payCode'");

$data_sk = array(
  'id' => null
);
$post_data = json_encode($data_sk);

//echo 'https://api.matchadiet.com/process/payment/confirmpay.php' . '?id=' . $payCode;

// Prepare new cURL resource
$crl = curl_init('https://api.matchadiet.com/process/payment/confirmpay.php' . '?id=' . $payCode);
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($crl, CURLINFO_HEADER_OUT, true);
curl_setopt($crl, CURLOPT_POST, true);
curl_setopt($crl, CURLOPT_POSTFIELDS, $post_data);

// Set HTTP Header for POST request
curl_setopt(
  $crl,
  CURLOPT_HTTPHEADER,
  array(
    'Content-Type: application/json'
  )
);

// Submit the POST request
$result_uid = curl_exec($crl);


$data_sk = json_decode($result_uid, true);

if ($data_sk["success"] == 'true') {

  $response1['success'] = 'true';
  $response1['message'] = '200';
  $response1['result'] = array(
    'resultLink' => "https://api.matchadiet.com/gateway/irsrv17/resultLink.php?id=" . $code,
  );
  $response1['data'] = array(
    'resultLink' => "https://api.matchadiet.com/gateway/irsrv17/resultLink.php?id=" . $code,
  );
  $json_response = json_encode($response1);
  echo $json_response;
  die();
} else {
  $response1['success'] = 'false';
  $response1['message'] = $data_sk['message'];

  $json_response = json_encode($response1);
  echo $json_response;
  die();
}



$conn->close();
