<?php
session_start();
require_once "../SystemController/DbConnection.php";
if(!isset($_SESSION['id'])){
    header("Location: ../login.php");
}

$id = $_SESSION['id'];

$msg = "";


$db_handle = new Dbconnection();
$user = $db_handle->runBaseQuery("SELECT * FROM users WHERE id = $id ");
$fname = $user[0]['first_name'];
$surname = $user[0]['surname'];
$email = $user[0]['email'];
if(!isset($_SESSION['id'])){
    header("location: login");
}

$id = $_SESSION['id'];

$amount = $db_handle->sanitize($_POST['amount']);



if(isset($_POST['amount'])){
    

  $url = "https://api.paystack.co/transaction/initialize";

  $fields = [
    'email' => $email,
    'amount' => $amount * 1000,
    'currency' => "NGN",
    'ref' => mt_rand(100,1000),
    'callback_url' => "https://app.ovohinitiative/callback",
  ];

  $fields_string = http_build_query($fields);
//sk_test_6553d 9f67e
  //  02aaf 88341 f5596 abdd30 f78e57 53d
  //open connection
  $ch = curl_init();
  
  //set the url, number of POST vars, POST data
  curl_setopt($ch,CURLOPT_URL, $url);
  curl_setopt($ch,CURLOPT_POST, true);
  curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Authorization: Bearer sk_test_6553d9f67e02aaf88341f5596abdd30f78e5753d",
    "Cache-Control: no-cache",
  ));
  
  //So that curl_exec returns the contents of the cURL; rather than echoing it
  curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
  
  //execute post
  $result = curl_exec($ch);
  echo $result;
}
?>