<?php
// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Start session
session_start();
 $hostname = "localhost";
 $username = "u459483528_tiranga1";
 $password ="x]dNR:;se|K1";
 $database = "u459483528_tiranga1";


 $conn = mysqli_connect($hostname,$username,$password,$database);
if (!$conn) {
    echo "Database Not Connected: " . mysqli_connect_error();
    exit; // Exit if database connection fails
}

// Get session email id
if (isset($_SESSION['emailid'])) {
    $emailid = $_SESSION['emailid'];
} else {
    echo "Email ID not found in session";
    exit; // Exit if session email ID is not set
}

// Get session phone number
if (isset($_SESSION['phone'])) {
    $phone = $_SESSION['phone'];
} else {
    echo "Phone number not found in session";
    exit; // Exit if session phone number is not set
}

// Generate transaction number
$txn_number = 'UCL_'. $phone . rand(1000,99999);

;

// UPI from POST data
$upi = "";
date_default_timezone_set("Asia/Kolkata");   //India time (GMT+5:30)
$date = date('d-m-y h:i:s');
$time = date("d-m-Y");
// Amount from POST data
$amount = isset($_POST['amount']) ? mysqli_real_escape_string($conn, $_POST['amount']) : '';

// Insert transaction into database
$res = mysqli_query($conn, "INSERT INTO `upitxn`(`userid`, `order_id`, `amount`,  `status`, `txn_date`, `date`) VALUES ('$phone','$txn_number','$amount','pending','$date','$time')");
if (!$res) {
    echo "Error inserting transaction into database: " . mysqli_error($conn);
    exit; // Exit if transaction insertion fails
}

// Set cookie for transaction number
setcookie("transaction_number", $txn_number, time() + 3600, "/"); // Expires in 1 hour

// URL of the PHP page
 $key = "0f61e67c7465cc40d26726b6143d6731";    // you can get your key from https://shivajigateway.com/    
                            
                                 $data = array(
                            	 	"user_token"=> $key,
                            	 	"order_id"=> "$txn_number", // order id or your own transaction id
                            	 	"amount"=> "$amount", 
                            	 	"customer_mobile"=> "$phone", // customer mobile number
                            	 	"redirect_url"=> "https://digitalsevaucl.online/admin/pay/txnResponse.php", // redirect url after payment, with ?client_txn_id=&txn_id=
                            	 	"remark1"=> "", // udf1, udf2 and udf3 are used to save other order related data, like customer id etc.
                            	 	"remark2"=> "",
                            	 	
                            	 );
                            	  $url = "https://shivajigateway.com/api/create-order";
                            	  $ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute cURL session and store the response
$response = curl_exec($ch);

    // Check for cURL errors
    if (curl_errno($ch)) {
        echo 'cURL error: ' . curl_error($ch);
    } else {
        // Parse the JSON response
        $result = json_decode($response, true);

        if ($result !== null && isset($result['result']['payment_url'])) {
            // Redirect the user to the payment URL
            $paymentUrl = $result['result']['payment_url'];
            header('Location: ' . $paymentUrl);
            exit;
        } elseif ($result !== null) {
            // Plan expired or other failure
            echo 'Status: ' . $result['status'] . '<br>';
            echo 'Message: ' . $result['message'];
        } else {
            // Invalid response (not JSON)
            echo 'Invalid API response: ' . htmlentities($response);
        }
    }

    // Close cURL session
    curl_close($ch);
    ?>
