<?php
header("Content-Type: application/json");

$input = json_decode(file_get_contents("php://input"), true);

if (!isset($input['amount']) || $input['amount'] < 1) {
    echo json_encode(["error" => "Invalid amount"]);
    exit;
}

$amount = intval($input['amount']);

// Razorpay amount in paise
$amount_paise = $amount * 100;

// 🔐 Razorpay LIVE Keys
$key_id = "rzp_live_S0fNQ2MMsgsPq4";
$key_secret = "1d5DGs1Nn1bbDjK4ebhAfKAL";

$data = [
    "amount" => $amount_paise,
    "currency" => "INR",
    "receipt" => "RBV_" . time()
];

$ch = curl_init("https://api.razorpay.com/v1/orders");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, $key_id . ":" . $key_secret);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);

if ($error) {
    echo json_encode(["error" => $error]);
    exit;
}

$order = json_decode($response, true);

if (!isset($order['id'])) {
    echo json_encode(["error" => "Order not created"]);
    exit;
}

echo json_encode([
    "order_id" => $order['id'],
    "amount"   => $amount_paise
]);
