<?php

session_start();
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");


if(isset($_POST['user_id']) && isset($_POST['auth']) && isset($_POST['ref']))
{
    
include 'config.php';
$user_id=$_POST['user_id'];
$auth=$_POST['auth'];
$ref=$_POST['ref'];
$amount=$_POST['amount'];

$user_id=stripslashes($user_id);
$user_id=mysqli_escape_string($con, $user_id);

$auth=stripslashes($auth);
$auth=mysqli_escape_string($con, $auth);

$ref=stripslashes($ref);
$ref=mysqli_escape_string($con, $ref);

$amount=stripslashes($amount);
$amount=mysqli_escape_string($con, $amount);

$date_t = date("d-m-Y");
$date_t2 = date("m/d/Y");
$date_t2 = strtotime($date_t);



  $sqlx=$con->query("INSERT INTO reg_trans  (user_id, auth, amount, reference, date_t) 
      VALUES ('$user_id', '$auth', '$amount', '$ref',  '$date_t')") or die("Error: ".mysqli_error($con));





  $curl = curl_init();
  
  curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.paystack.co/transaction/verify/".$ref,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
      "Authorization: Bearer sk_test_25596118ee315eb93dd6186bd81aab6578e2b487",
      "Cache-Control: no-cache",
    ),
  ));
  
  $response = curl_exec($curl);
  $err = curl_error($curl);

  curl_close($curl);
  
  if ($err) {
    echo "cURL Error #:" . $err;
  } 
  
  else {
//echo $response;
    
     $resp = json_decode($response, true);

        $paymentStatus = $resp['data']['status'];
        $chargeResponsecode = $resp['data']['chargecode'];
        $chargeAmount = ($resp['data']['amount'])/100;
        $chargeCurrency = $resp['data']['currency'];
        
        
       
$sqlx=$con->query("SELECT * FROM users WHERE  user_id = '$user_id' AND auth = '$auth'") or die("Error2 : ". mysqli_error($con));

if($sqlx)
{
$rowx=mysqli_fetch_array($sqlx);
$email = $rowx['email'];
$balance = $rowx['balance'];
$fname = $rowx['firstname'];
$lname = $rowx['lastname'];
$fulname = $fname." ".$lname;
$currency = "NGN";
$new_bal = $balance + $amount;

$expiry = date('d-m-Y', strtotime($period, time()));


 if (($paymentStatus == "success") && ($chargeAmount == $amount)  && ($chargeCurrency == $currency)) {

     
$sqlx = $con->query("UPDATE users SET balance = '$new_bal' WHERE user_id = '$user_id' AND auth = '$auth'") or die("Error2 : ". mysqli_error($con));

$pro_pic= $p_url."/profiles/".$user_id.".png?time=".time();
$notificaton = "Your wallet has been credited with ₦".number_format($amount);

  $sqlx=$con->query("INSERT INTO notifications  (user_id, name, pro_pic, post_id, notification, posted_by, date_t) 
      VALUES ('$user_id', '$fulname', '$pro_pic', 'ref', '$notificaton', '$user_id', '$date_t')") or die("Error: ".mysqli_error($con));


      $suc = "Successful";
     $status = true;


 }
 else
 {
     $suc = "Invalid Transaction";
     $status = false;
 }

}
 
} 

 $con->close();

  
$output = array('status' => $status, 'success'=>$suc, 'user_id' => $user_id, 'amount'=> $chargeAmount);
echo json_encode($output); //output JSON data

}


?>

