<?php
include "dbconnect.php";  // Database Connection

// Get user details from the URL parameters and sanitize input
$username = isset($_GET['username']) ? htmlspecialchars($_GET['username']) : '';
$email = isset($_GET['email']) ? htmlspecialchars($_GET['email']) : '';
$mobile = isset($_GET['mobile']) ? htmlspecialchars($_GET['mobile']) : '';

// Razorpay API keys
$key_id = 'rzp_live_x80H4fOx5AICH1';  // Replace with your Razorpay key id
$secret_key = 'FFaibhB1IGG67p7tHSukZRMX';  // Replace with your Razorpay secret key

// The fixed amount in paise (₹29,500 including 18% GST)
$amount = 29500 * 100;  // ₹29,500 in paise

// Setup cURL to make the request to Razorpay's API for creating an order
$url = "https://api.razorpay.com/v1/orders";
$headers = [
    "Content-Type: application/json"
];
$data = [
    "amount" => $amount,  // Amount in paise
    "currency" => "INR",
    "receipt" => "order_receipt_" . time(),
    "payment_capture" => 1
];

// Initialize cURL session
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $key_id . ":" . $secret_key);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Execute the cURL request
$response = curl_exec($ch);

// Check for cURL errors
if ($error = curl_error($ch)) {
    die("Error: $error");
}

// Decode the response
$responseData = json_decode($response, true);
curl_close($ch);

// Extract the order_id from the response
$order_id = $responseData['id'];

// Calculate the GST amount (18% GST)
$gst_percentage = 18;
$gst_amount = ($amount / 100) * $gst_percentage;
$total_amount = $amount / 100; // in INR (₹29,500)
?>

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Payment Page</title>
  <script src="https://checkout.razorpay.com/v1/checkout.js"></script>
  <style>
    body {
        font-family: Arial, sans-serif;
        background: linear-gradient(45deg, #3498db, #2ecc71); /* Gradient Background */
        background-size: cover;
        background-position: center;
        height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .container {
        background-color: rgba(255, 255, 255, 0.9); /* Transparent background */
        padding: 30px;
        border-radius: 10px;
        max-width: 600px;
        margin: 20px;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    }

    h3, h4 {
        text-align: center;
        color: #333;
        font-weight: bold;
    }

    .btn {
        display: inline-block;
        background-color: #4CAF50;
        color: white;
        padding: 12px 24px;
        margin-top: 20px;
        border: none;
        cursor: pointer;
        border-radius: 5px;
        font-size: 16px;
        transition: 0.3s;
    }

    .btn:hover {
        background-color: #45a049;
        transform: scale(1.05);
    }

    table {
        width: 100%;
        margin-top: 20px;
        border-collapse: collapse;
    }

    th, td {
        padding: 12px;
        text-align: center;
        border: 1px solid #ddd;
    }

    th {
        background-color: #4CAF50;
        color: white;
    }

    tr:nth-child(even) {
        background-color: #f2f2f2;
    }

    .gst-info {
        font-size: 14px;
        color: #333;
        margin-top: 10px;
        text-align: center;
    }
  </style>
</head>
<body>

<div class="container">
    <h4 class="text-uppercase font-weight-bold my-4">ChemTechie Engineering & Consulting Services Pvt.Ltd</h4>
    <h3>Complete Your Payment</h3>

    <!-- Table displaying user details and amount -->
    <table>
        <tr>
            <th>Username</th>
            <th>Email</th>
            <th>Mobile</th>
            <th>Amount</th>
        </tr>
        <tr>
            <td><?php echo $username; ?></td>
            <td><?php echo $email; ?></td>
            <td><?php echo $mobile; ?></td>
            <td>₹29,500</td>
        </tr>
    </table>

    <!-- Message with GST info -->
    <div class="gst-info">
        <p><strong>Amount includes 18% GST</strong></p>
    </div>

    <!-- Razorpay Payment Button -->
    <div class="d-flex justify-content-center">
        <button class="btn" id="razorpay-button">Pay Now</button>
    </div>

    <script type="text/javascript">
        var options = {
            "key": "<?php echo $key_id; ?>", // Razorpay key_id
            "amount": "<?php echo $amount; ?>", // Amount in paise (₹29,500 in paise)
            "currency": "INR",
            "name": "ChemTechie Engineering & Consulting Services Pvt.Ltd",
            "description": "Registration Payment",
            "image": "img/logo.jpg", // Replace with your logo URL
            "order_id": "<?php echo $order_id; ?>", // Razorpay order_id
            "handler": function (response) {
                // On success, redirect to the success page and pass the necessary details
                window.location.href = "payment_success.php?razorpay_payment_id=" + response.razorpay_payment_id +
                                        "&razorpay_order_id=" + response.razorpay_order_id +
                                        "&razorpay_signature=" + response.razorpay_signature +
                                        "&username=" + "<?php echo $username; ?>" +  // Pass user details
                                        "&email=" + "<?php echo $email; ?>" +
                                        "&mobile=" + "<?php echo $mobile; ?>";
            },
            "theme": {
                "color": "#528FF0"
            }
        };

        var rzp1 = new Razorpay(options);
        document.getElementById('razorpay-button').onclick = function (e) {
            rzp1.open();
            e.preventDefault();
        };
    </script>
</div>

</body>
</html>
