<?php
// session_start();
include "../db.php";
require '../auth.php';
restrictAccess(['Student']);
require("razorpay-php/Razorpay.php");

use Razorpay\Api\Api;

if (!isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype'])) {
    header("Location: logout.php");
    exit;
}

if (!isset($_SESSION["amount"])) {
    header("Location: index.php");
    header("Location: index.php");
    exit;
}

$amount2 = $_SESSION["amount"];
$description = $_SESSION["description"];

date_default_timezone_set('Asia/Kolkata');
$client_id = $_SESSION["client_id"];
$product_id = $_SESSION["product_id"];
$service_name = $_SESSION["service_name"];

$rechargeBy = $_SESSION["rechargeBy"];
$fran_id = $_SESSION["fran_id"];
$subFran_id = $_SESSION["subFran_id"];
$rByType = $_SESSION["rByType"];
$recycle = $_SESSION["recycle"];
$uniqueCode = $_SESSION["uniqueCode"];

$client_sql = mysqli_query($con, "SELECT * FROM users WHERE uid = '$client_id'");
if ($client_row = mysqli_fetch_assoc($client_sql)) {
    $client_name = $client_row["uname"];
    $client_contact = $client_row["ucontact"];
    $client_email = $client_row["uemail"];
    $client_address = $client_row["saddress"];
}

$sFran_sql = mysqli_query($con, "SELECT * FROM users WHERE uid = '$rechargeBy'");
if ($sFran_row = mysqli_fetch_assoc($sFran_sql)) {
    $sFran_name = $sFran_row["uname"];
}

include("getway-config.php");

$api = new Api($keyId, $keySecret);

$webtitle = "ayushApplication";
$displayCurrency = "INR";
$imgUrl = "";  // Set the appropriate image URL

$orderData = [
    'receipt'         => "3456",
    'amount'          => $amount2 * 100, // Convert to paise
    'currency'        => 'INR',
    'payment_capture' => 1 // Auto capture
];

$razorpayOrder = $api->order->create($orderData);
$razorpayOrderId = $razorpayOrder['id'];
$_SESSION['razorpay_order_id'] = $razorpayOrderId;
$amount = $orderData['amount'];
$displayAmount = $amount;

if ($displayCurrency !== 'INR') {
    $url = "https://openexchangerates.org/api/latest.json?app_id=your_app_id&symbols=$displayCurrency&base=INR";
    $exchangeRates = json_decode(file_get_contents($url), true);
    if ($exchangeRates && isset($exchangeRates['rates'][$displayCurrency])) {
        $displayAmount = $exchangeRates['rates'][$displayCurrency] * $amount / 100;
    } else {
        $displayCurrency = 'INR';
        $displayAmount = $amount / 100;
    }
} else {
    $displayAmount = $amount / 100;
}

$data = [
    "key"               => $keyId,
    "amount"            => $amount,
    "name"              => $webtitle,
    "description"       => $rByType,
    "image"             => $imgUrl,
    "prefill"           => [
        "name"          => $client_name,
        "contact"       => $client_contact,
    ],
    "notes"             => [
        "address"       => $client_address,
        "merchant_order_id" => "12312321",
    ],
    "theme"             => [
        "color"         => "#F37254"
    ],
    "order_id"          => $razorpayOrderId,
];

if ($displayCurrency !== 'INR') {
    $data['display_currency']  = $displayCurrency;
    $data['display_amount']    = $displayAmount;
}

$json = json_encode($data);
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include "include/meta.php"; ?>
    <?php include "include/css.php"; ?>
</head>
<style>
.razorpay-payment-button {
    display: inline-block;
    font-weight: 400;
    line-height: 1.5;
    color: #212529;
    text-align: center;
    text-decoration: none;
    vertical-align: middle;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    user-select: none;
    background-color: transparent;
    border: 1px solid transparent;
    padding: 0.375rem 0.75rem;
    font-size: 1rem;
    border-radius: 0.25rem;
    transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
    color: #fff;
    background-color: #198754;
    border-color: #198754;
}
</style>

<body>
    <?php include "include/header.php"; ?>

    <section id="inner-body">
        <div class="body-content">
            <div class="container-fluid">
                <div class="card">
                    <div class="card-header">
                        <h4 class="m-0 text-center">Payment Details</h4>
                    </div>
                    <div class="card-body">
                        <div class="row justify-content-center g-5">
                            <div class="col-lg-6">
                                <div>
                                    <p class="m-0"><strong>Payment For :</strong> <?php echo $service_name; ?></p>
                                    <p class="m-0"><strong>Payment Amount :</strong> <?php echo $amount2;?></p>
                                    <p class="m-0"><strong>Client Name :</strong> <?php echo $client_name;?></p>
                                    <p class="m-0"><strong>Recharge By :</strong> <?php echo $sFran_name;?></p>
                                </div>
                                <form action="verify.php" method="POST">
                                    <script src="https://checkout.razorpay.com/v1/checkout.js"
                                        data-key="<?php echo $data['key']?>" data-amount="<?php echo $data['amount']?>"
                                        data-currency="INR" data-name="<?php echo $data['name']?>"
                                        data-image="<?php echo $data['image']?>"
                                        data-description="<?php echo $data['description']?>"
                                        data-prefill.name="<?php echo $data['prefill']['name']?>"
                                        data-prefill.contact="<?php echo $data['prefill']['contact']?>"
                                        data-notes.address="<?php echo $data['notes']['address']?>"
                                        data-notes.merchant_order_id="<?php echo $data['notes']['merchant_order_id']?>"
                                        data-order_id="<?php echo $data['order_id']?>"
                                        <?php if ($displayCurrency !== 'INR') { ?>
                                        data-display_amount="<?php echo $data['display_amount']?>"
                                        data-display_currency="<?php echo $data['display_currency']?>" <?php } ?>>
                                    </script>
                                    <!-- Any extra fields to be submitted with the form but not sent to Razorpay -->
                                    <!--<input type="submit" value="Pay Now"
                                        class="razorpay-payment-button btn btn-success">-->
                                    <input type="hidden" name="shopping_order_id" value="<?php echo $uniqueCode;?>">
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <?php include "include/footer.php"; ?>
    </section>

    <?php include "include/js.php"; ?>
</body>

</html>