D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home3
/
encodto1
/
siliguritoys.com
/
Filename :
checkout.php
back
Copy
<?php include 'config.php'; // Database connection session_start(); if (!isset($_SESSION['user_id'])) { echo "<script>alert('Please log in to continue.'); window.location.href='login.php';</script>"; exit; } $user_id = $_SESSION['user_id']; $total_price = 0; $additional_charge = 500; $gst_rate = 18; // GST percentage // Fetch cart items from the database $cart_query = "SELECT c.product_id, c.quantity, p.name, p.price, p.image_1 FROM cart c JOIN slgtoys_products p ON c.product_id = p.id WHERE c.user_id = '$user_id'"; $result = mysqli_query($conn, $cart_query); $products = []; if (mysqli_num_rows($result) == 0) { echo "<script>alert('Your cart is empty!'); window.location.href='shop.php';</script>"; exit; } while ($row = mysqli_fetch_assoc($result)) { $products[] = $row; $total_price += $row['price'] * $row['quantity']; } $gst_amount = ($total_price + $additional_charge) * ($gst_rate / 100); $grand_total = $total_price + $additional_charge + $gst_amount; if ($_SERVER['REQUEST_METHOD'] == "POST") { $payment_method = $_POST['payment_method']; // Only process COD orders here - Razorpay orders are handled in payment_verify.php if ($payment_method === 'cash') { // Insert order $order_query = "INSERT INTO orders (customer_id, total_price, additional_charge, gst, grand_total, payment_method) VALUES ('$user_id', '$total_price', '$additional_charge', '$gst_amount', '$grand_total', '$payment_method')"; if (mysqli_query($conn, $order_query)) { $order_id = mysqli_insert_id($conn); // Insert order items foreach ($products as $product) { $product_id = $product['product_id']; $product_price = $product['price']; $quantity = $product['quantity']; mysqli_query($conn, "INSERT INTO order_items (order_id, product_id, price, quantity) VALUES ('$order_id', '$product_id', '$product_price', '$quantity')"); } // Clear cart after successful order mysqli_query($conn, "DELETE FROM cart WHERE user_id = '$user_id'"); // Redirect to account page echo "<script>alert('Order placed successfully!'); window.location='my_account.php?user_id=$user_id';</script>"; exit; } else { echo "Error: " . mysqli_error($conn); } } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Checkout</title> <script src="https://cdn.tailwindcss.com"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet" /> </head> <body class="bg-gray-100"> <div class="container mx-auto px-4 py-8"> <h1 class="text-4xl font-bold text-center text-blue-600 mb-8">Checkout</h1> <div class="grid grid-cols-1 lg:grid-cols-2 gap-8"> <div class="bg-white p-6 shadow-lg rounded-lg"> <h2 class="text-2xl font-semibold text-gray-800 mb-4">Order Summary</h2> <div class="space-y-4"> <?php foreach ($products as $product) { ?> <div class="flex items-center border-b pb-4"> <img src="slgadmintoys/<?php echo $product['image_1']; ?>" class="w-20 h-20 object-cover rounded-md mr-4"> <div> <h3 class="text-lg font-semibold text-gray-800"><?php echo $product['name']; ?></h3> <p class="text-sm text-gray-600">₹<?php echo number_format($product['price'], 2); ?> x <?php echo $product['quantity']; ?></p> </div> <p class="ml-auto font-bold text-gray-800">₹<?php echo number_format($product['price'] * $product['quantity'], 2); ?></p> </div> <?php } ?> </div> <div class="mt-6 border-t pt-4"> <p class="flex justify-between text-gray-700"><span>Subtotal:</span> <span>₹<?php echo number_format($total_price, 2); ?></span></p> <p class="flex justify-between text-gray-700"><span>Additional Charge:</span> <span>₹<?php echo number_format($additional_charge, 2); ?></span></p> <p class="flex justify-between text-gray-700"><span>GST (<?php echo $gst_rate; ?>%):</span> <span>₹<?php echo number_format($gst_amount, 2); ?></span></p> <p class="flex justify-between text-xl font-bold text-gray-900 mt-4"><span>Total:</span> <span>₹<?php echo number_format($grand_total, 2); ?></span></p> </div> </div> <div class="bg-white p-6 shadow-lg rounded-lg"> <h2 class="text-2xl font-semibold text-gray-800 mb-4">Payment Method</h2> <form method="POST" id="paymentForm" class="space-y-4"> <label for="payment_method" class="block text-gray-600 text-sm font-medium">Choose Payment Method:</label> <select name="payment_method" id="payment_method" required class="w-full p-2 border rounded-lg focus:ring-2 focus:ring-blue-500"> <option value="cash">Cash on Delivery</option> <option value="razorpay">Online Payment (Razorpay)</option> </select> <button type="submit" name="payment_type" value="cod" class="bg-yellow-500 text-white font-bold py-3 px-6 rounded-lg w-full hover:bg-yellow-600 transition"> Place Order (COD) </button> <button type="button" id="payNowBtn" class="bg-blue-500 text-white font-bold py-3 px-6 rounded-lg w-full hover:bg-blue-600 transition"> Pay Now (₹<?php echo number_format($grand_total, 2); ?>) </button> </form> </div> </div> </div> <script src="https://checkout.razorpay.com/v1/checkout.js"></script> <script> $(document).ready(function() { // Hide Pay Now button if COD is selected $('#payment_method').change(function() { if ($(this).val() === 'cash') { $('#payNowBtn').hide(); $('button[type="submit"]').show(); } else { $('#payNowBtn').show(); $('button[type="submit"]').hide(); } }).trigger('change'); // Initialize state // Handle form submission for COD $('#paymentForm').submit(function(e) { if ($('#payment_method').val() === 'razorpay') { e.preventDefault(); return false; } // Allow normal submission for COD }); // Handle Razorpay payment $('#payNowBtn').click(function(e) { e.preventDefault(); var amount = <?php echo $grand_total * 100; ?>; // Amount in paise $.ajax({ type: "POST", url: "order.php", data: { amount: amount / 100 // Send amount in rupees }, success: function(response) { if (response.error) { alert("Error: " + response.error); return; } var options = { key: "rzp_test_HyfJRoGb09WlFo", amount: response.amount, currency: response.currency, name: "Your Store Name", description: "Order Payment", image: "https://example.com/logo.png", order_id: response.id, handler: function(payment) { // On successful payment $.post("payment_verify.php", { razorpay_payment_id: payment.razorpay_payment_id, razorpay_order_id: payment.razorpay_order_id, razorpay_signature: payment.razorpay_signature, amount: amount, user_id: "<?php echo $user_id; ?>", payment_method: "razorpay" }, function(data) { if (data.success) { alert("Payment successful! Order ID: " + data.order_id); window.location.href = "my_account.php?user_id=<?php echo $user_id; ?>"; } else { alert("Payment verification failed: " + data.error); } }, "json"); }, prefill: { name: "Customer Name", email: "customer@example.com", contact: "9999999999" }, theme: { color: "#3399cc" } }; var rzp = new Razorpay(options); rzp.open(); }, error: function(xhr, status, error) { alert("Error creating payment order: " + error); } }); }); }); </script> </body> </html>