127 lines
4.0 KiB
PHP
127 lines
4.0 KiB
PHP
<?php
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
// Redirect to login if user is not logged in
|
|
if (!isset($_SESSION["user_id"])) {
|
|
header("Location: login.php");
|
|
exit();
|
|
}
|
|
|
|
require_once 'db/config.php';
|
|
require_once 'razorpay-config.php';
|
|
require_once 'vendor/autoload.php';
|
|
|
|
use Razorpay\Api\Api;
|
|
|
|
$product_id = isset($_GET['product_id']) ? (int)$_GET['product_id'] : 0;
|
|
$product = null;
|
|
$user = null;
|
|
|
|
if ($product_id > 0) {
|
|
try {
|
|
// Fetch product details
|
|
$stmt = db()->prepare("SELECT * FROM products WHERE id = ?");
|
|
$stmt->execute([$product_id]);
|
|
$product = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
// Fetch user details
|
|
$stmt = db()->prepare("SELECT * FROM users WHERE id = ?");
|
|
$stmt->execute([$_SESSION["user_id"]]);
|
|
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $e) {
|
|
error_log("Database error: " . $e->getMessage());
|
|
// You might want to show a generic error page here
|
|
die("Error fetching data. Please try again later.");
|
|
}
|
|
}
|
|
|
|
if (!$product || !$user) {
|
|
// Handle case where product or user is not found
|
|
die("Product or user not found.");
|
|
}
|
|
|
|
$api = new Api($razorpay_key_id, $razorpay_key_secret);
|
|
|
|
// Create Razorpay Order
|
|
$orderData = [
|
|
'receipt' => 'rcptid_' . uniqid(),
|
|
'amount' => $product['price'] * 100, // amount in paise
|
|
'currency' => 'INR',
|
|
'payment_capture' => 1 // auto capture
|
|
];
|
|
|
|
$razorpayOrder = $api->order->create($orderData);
|
|
$razorpayOrderId = $razorpayOrder['id'];
|
|
|
|
$_SESSION['razorpay_order_id'] = $razorpayOrderId;
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Checkout - EcoGrow</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body>
|
|
|
|
<?php include 'includes/header.php'; ?>
|
|
|
|
<div class="container my-5">
|
|
<div class="row">
|
|
<div class="col-md-6 offset-md-3">
|
|
<div class="card">
|
|
<div class="card-header bg-success text-white">
|
|
<h2 class="mb-0">Order Summary</h2>
|
|
</div>
|
|
<div class="card-body">
|
|
<h5 class="card-title"><?php echo htmlspecialchars($product['name']); ?></h5>
|
|
<p class="card-text"><?php echo htmlspecialchars($product['description']); ?></p>
|
|
<p class="card-text fw-bold fs-4">Amount: $<?php echo htmlspecialchars($product['price']); ?></p>
|
|
<hr>
|
|
<p><strong>Billed to:</strong></p>
|
|
<p>Name: <?php echo htmlspecialchars($user['name']); ?></p>
|
|
<p>Email: <?php echo htmlspecialchars($user['email']); ?></p>
|
|
<button id="rzp-button1" class="btn btn-primary w-100">Pay with Razorpay</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
|
|
<script>
|
|
var options = {
|
|
"key": "<?php echo $razorpay_key_id; ?>",
|
|
"amount": "<?php echo $product['price'] * 100; ?>",
|
|
"currency": "INR",
|
|
"name": "EcoGrow",
|
|
"description": "Purchase of <?php echo htmlspecialchars($product['name']); ?>",
|
|
"image": "https://example.com/your_logo.jpg", // Optional
|
|
"order_id": "<?php echo $razorpayOrderId; ?>",
|
|
"handler": function (response){
|
|
window.location.href = `success.php?payment_id=${response.razorpay_payment_id}&order_id=${response.razorpay_order_id}&signature=${response.razorpay_signature}`;
|
|
},
|
|
"prefill": {
|
|
"name": "<?php echo htmlspecialchars($user['name']); ?>",
|
|
"email": "<?php echo htmlspecialchars($user['email']); ?>"
|
|
},
|
|
"theme": {
|
|
"color": "#28a745"
|
|
}
|
|
};
|
|
var rzp1 = new Razorpay(options);
|
|
document.getElementById('rzp-button1').onclick = function(e){
|
|
rzp1.open();
|
|
e.preventDefault();
|
|
}
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|