🤩 File Manager - Mr.X
PHP:
8.3.33
Server:
Apache
OS:
Linux 5.14.0-611.49.1.el9_7.x86_64
User:
websparkit
Navigate
Upload:
Upload
New File
New Folder
Editing:checkout.php
<?php require_once __DIR__ . '/../includes/config.php'; require_once __DIR__ . '/../includes/db.php'; require_once __DIR__ . '/../includes/functions.php'; $page = 'checkout'; $page_title = 'Checkout'; require_once '../includes/header.php'; require_once '../includes/navbar.php'; $cart_items = get_cart_items(); if (count($cart_items) == 0) { header('Location: ' . SITE_URL . 'pages/cart.php'); exit; } // Process Order if ($_SERVER['REQUEST_METHOD'] == 'POST' && verify_csrf($_POST[CSRF_TOKEN_NAME] ?? '')) { $first_name = trim($_POST['first_name']); $last_name = trim($_POST['last_name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); $address_line1 = trim($_POST['address_line1']); $address_line2 = trim($_POST['address_line2'] ?? ''); $city = trim($_POST['city']); $state = trim($_POST['state']); $zip_code = trim($_POST['zip_code']); $country = trim($_POST['country'] ?? 'India'); $notes = trim($_POST['notes'] ?? ''); $payment_method = $_POST['payment_method'] ?? 'cod'; $same_as_billing = isset($_POST['same_as_billing']); $shipping_first_name = $same_as_billing ? $first_name : trim($_POST['shipping_first_name'] ?? $first_name); $shipping_last_name = $same_as_billing ? $last_name : trim($_POST['shipping_last_name'] ?? $last_name); $shipping_address_line1 = $same_as_billing ? $address_line1 : trim($_POST['shipping_address_line1'] ?? $address_line1); $shipping_address_line2 = $same_as_billing ? $address_line2 : trim($_POST['shipping_address_line2'] ?? $address_line2); $shipping_city = $same_as_billing ? $city : trim($_POST['shipping_city'] ?? $city); $shipping_state = $same_as_billing ? $state : trim($_POST['shipping_state'] ?? $state); $shipping_zip_code = $same_as_billing ? $zip_code : trim($_POST['shipping_zip_code'] ?? $zip_code); $shipping_country = $same_as_billing ? $country : trim($_POST['shipping_country'] ?? $country); // Calculate totals $subtotal = get_cart_total(); $shipping_cost = $subtotal >= FREE_SHIPPING_MIN ? 0 : SHIPPING_COST; $tax = ($subtotal * TAX_RATE) / 100; $discount = 0; $coupon_code = null; if (isset($_SESSION['coupon'])) { $discount = calculate_discount($_SESSION['coupon'], $subtotal); $coupon_code = $_SESSION['coupon']['code']; } $total = $subtotal + $shipping_cost + $tax - $discount; $order_number = generate_order_number(); try { $db->beginTransaction(); $user_id = is_logged_in() ? $_SESSION['user_id'] : null; $stmt = $db->prepare("INSERT INTO orders (order_number, user_id, first_name, last_name, email, phone, address_line1, address_line2, city, state, zip_code, country, shipping_first_name, shipping_last_name, shipping_address_line1, shipping_address_line2, shipping_city, shipping_state, shipping_zip_code, shipping_country, shipping_cost, subtotal, discount, coupon_code, tax, total, payment_method, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$order_number, $user_id, $first_name, $last_name, $email, $phone, $address_line1, $address_line2, $city, $state, $zip_code, $country, $shipping_first_name, $shipping_last_name, $shipping_address_line1, $shipping_address_line2, $shipping_city, $shipping_state, $shipping_zip_code, $shipping_country, $shipping_cost, $subtotal, $discount, $coupon_code, $tax, $total, $payment_method, $notes]); $order_id = $db->lastInsertId(); foreach ($cart_items as $item) { $price = $item['sale_price'] > 0 ? $item['sale_price'] : $item['regular_price']; $stmt = $db->prepare("INSERT INTO order_items (order_id, product_id, product_name, product_image, variation_details, quantity, price, total) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$order_id, $item['id'], $item['name'], $item['thumbnail'], $item['variation_details'], $item['quantity'], $price, $price * $item['quantity']]); // Update stock $db->prepare("UPDATE products SET stock_quantity = stock_quantity - ? WHERE id = ?")->execute([$item['quantity'], $item['id']]); } $db->commit(); // Clear cart and coupon unset($_SESSION['cart']); unset($_SESSION['coupon']); // Update coupon usage if ($coupon_code) { $db->prepare("UPDATE coupons SET used_count = used_count + 1 WHERE code = ?")->execute([$coupon_code]); } header('Location: ' . SITE_URL . 'pages/order-success.php?order=' . $order_number); exit; } catch (Exception $e) { $db->rollBack(); set_flash('error', 'Order placement failed. Please try again.'); } } $cart_total = get_cart_total(); $shipping_cost = $cart_total >= FREE_SHIPPING_MIN ? 0 : SHIPPING_COST; $tax = ($cart_total * TAX_RATE) / 100; $discount = isset($_SESSION['coupon']) ? calculate_discount($_SESSION['coupon'], $cart_total) : 0; $grand_total = $cart_total + $shipping_cost + $tax - $discount; $current_user = is_logged_in() ? get_user_data() : null; ?> <main> <!-- Breadcrumb --> <section style="background: var(--light-marble); padding: 20px 0;"> <div class="container"> <nav aria-label="breadcrumb"> <ol class="breadcrumb mb-0"> <li class="breadcrumb-item"><a href="<?= SITE_URL ?>">Home</a></li> <li class="breadcrumb-item"><a href="<?= SITE_URL ?>pages/cart.php">Cart</a></li> <li class="breadcrumb-item active">Checkout</li> </ol> </nav> </div> </section> <!-- Checkout Section --> <section class="checkout-section" style="padding: 60px 0;"> <div class="container"> <?php display_flash(); ?> <h1 style="font-size: 2.5rem; margin-bottom: 40px;">Checkout</h1> <form method="POST" class="needs-validation" novalidate> <?= csrf_field() ?> <div class="row g-4"> <!-- Checkout Form --> <div class="col-lg-7"> <!-- Billing Details --> <div class="checkout-form" style="background: var(--white); border-radius: 16px; padding: 30px; box-shadow: var(--shadow-sm); margin-bottom: 25px;"> <h3 style="font-size: 1.3rem; margin-bottom: 25px; font-family: var(--font-primary); font-weight: 600;"> <i class="fas fa-user" style="color: var(--primary-rose-gold);"></i> Billing Details </h3> <div class="row g-3"> <div class="col-md-6"> <label class="form-label">First Name *</label> <input type="text" name="first_name" class="form-control" required value="<?= htmlspecialchars($current_user['first_name'] ?? '') ?>" style="border-radius: 25px; padding: 10px 20px;"> <div class="invalid-feedback">Please enter your first name.</div> </div> <div class="col-md-6"> <label class="form-label">Last Name *</label> <input type="text" name="last_name" class="form-control" required value="<?= htmlspecialchars($current_user['last_name'] ?? '') ?>" style="border-radius: 25px; padding: 10px 20px;"> <div class="invalid-feedback">Please enter your last name.</div> </div> <div class="col-md-6"> <label class="form-label">Email Address *</label> <input type="email" name="email" class="form-control" required value="<?= htmlspecialchars($current_user['email'] ?? '') ?>" style="border-radius: 25px; padding: 10px 20px;"> <div class="invalid-feedback">Please enter a valid email.</div> </div> <div class="col-md-6"> <label class="form-label">Phone Number *</label> <input type="tel" name="phone" class="form-control" required value="<?= htmlspecialchars($current_user['phone'] ?? '') ?>" style="border-radius: 25px; padding: 10px 20px;"> <div class="invalid-feedback">Please enter your phone number.</div> </div> <div class="col-md-12"> <label class="form-label">Address *</label> <input type="text" name="address_line1" class="form-control" required placeholder="Street address" style="border-radius: 25px; padding: 10px 20px;"> <div class="invalid-feedback">Please enter your address.</div> </div> <div class="col-md-12"> <label class="form-label">Apartment, suite, etc. (optional)</label> <input type="text" name="address_line2" class="form-control" style="border-radius: 25px; padding: 10px 20px;"> </div> <div class="col-md-6"> <label class="form-label">City *</label> <input type="text" name="city" class="form-control" required style="border-radius: 25px; padding: 10px 20px;"> <div class="invalid-feedback">Please enter your city.</div> </div> <div class="col-md-6"> <label class="form-label">State *</label> <input type="text" name="state" class="form-control" required style="border-radius: 25px; padding: 10px 20px;"> <div class="invalid-feedback">Please enter your state.</div> </div> <div class="col-md-6"> <label class="form-label">ZIP / Postal Code *</label> <input type="text" name="zip_code" class="form-control" required style="border-radius: 25px; padding: 10px 20px;"> <div class="invalid-feedback">Please enter your ZIP code.</div> </div> <div class="col-md-6"> <label class="form-label">Country *</label> <select name="country" class="form-control" required style="border-radius: 25px; padding: 10px 20px;"> <option value="India">India</option> <option value="USA">United States</option> <option value="UK">United Kingdom</option> <option value="UAE">UAE</option> </select> </div> </div> </div> <!-- Shipping Address --> <div class="checkout-form" style="background: var(--white); border-radius: 16px; padding: 30px; box-shadow: var(--shadow-sm); margin-bottom: 25px;"> <h3 style="font-size: 1.3rem; margin-bottom: 25px; font-family: var(--font-primary); font-weight: 600;"> <i class="fas fa-truck" style="color: var(--primary-rose-gold);"></i> Shipping Address </h3> <div class="form-check mb-3"> <input class="form-check-input" type="checkbox" id="same_as_billing" name="same_as_billing" checked> <label class="form-check-label" for="same_as_billing"> Same as billing address </label> </div> <div class="shipping-fields" style="display: none;"> <div class="row g-3"> <div class="col-md-6"> <label class="form-label">First Name *</label> <input type="text" name="shipping_first_name" class="form-control" style="border-radius: 25px; padding: 10px 20px;"> </div> <div class="col-md-6"> <label class="form-label">Last Name *</label> <input type="text" name="shipping_last_name" class="form-control" style="border-radius: 25px; padding: 10px 20px;"> </div> <div class="col-md-12"> <label class="form-label">Address *</label> <input type="text" name="shipping_address_line1" class="form-control" style="border-radius: 25px; padding: 10px 20px;"> </div> <div class="col-md-6"> <label class="form-label">City *</label> <input type="text" name="shipping_city" class="form-control" style="border-radius: 25px; padding: 10px 20px;"> </div> <div class="col-md-6"> <label class="form-label">State *</label> <input type="text" name="shipping_state" class="form-control" style="border-radius: 25px; padding: 10px 20px;"> </div> <div class="col-md-6"> <label class="form-label">ZIP Code *</label> <input type="text" name="shipping_zip_code" class="form-control" style="border-radius: 25px; padding: 10px 20px;"> </div> <div class="col-md-6"> <label class="form-label">Country *</label> <select name="shipping_country" class="form-control" style="border-radius: 25px; padding: 10px 20px;"> <option value="India">India</option> <option value="USA">United States</option> <option value="UK">United Kingdom</option> </select> </div> </div> </div> </div> <!-- Order Notes --> <div class="checkout-form" style="background: var(--white); border-radius: 16px; padding: 30px; box-shadow: var(--shadow-sm); margin-bottom: 25px;"> <h3 style="font-size: 1.3rem; margin-bottom: 25px; font-family: var(--font-primary); font-weight: 600;"> <i class="fas fa-sticky-note" style="color: var(--primary-rose-gold);"></i> Order Notes </h3> <textarea name="notes" class="form-control" rows="4" placeholder="Notes about your order, e.g. special notes for delivery." style="border-radius: 15px; padding: 15px 20px;"></textarea> </div> <!-- Payment Method --> <div class="checkout-form" style="background: var(--white); border-radius: 16px; padding: 30px; box-shadow: var(--shadow-sm);"> <h3 style="font-size: 1.3rem; margin-bottom: 25px; font-family: var(--font-primary); font-weight: 600;"> <i class="fas fa-credit-card" style="color: var(--primary-rose-gold);"></i> Payment Method </h3> <div class="payment-methods"> <div class="form-check payment-method-item" style="padding: 15px 20px; border: 2px solid var(--gray-200); border-radius: 12px; margin-bottom: 10px; cursor: pointer;"> <input class="form-check-input" type="radio" name="payment_method" id="cod" value="cod" checked> <label class="form-check-label" for="cod" style="display: flex; align-items: center; gap: 10px; cursor: pointer; width: 100%;"> <i class="fas fa-money-bill-wave" style="color: var(--primary-rose-gold); font-size: 1.3rem;"></i> <div> <strong>Cash on Delivery</strong> <div style="font-size: 0.85rem; color: var(--gray-600);">Pay with cash upon delivery</div> </div> </label> </div> <div class="form-check payment-method-item" style="padding: 15px 20px; border: 2px solid var(--gray-200); border-radius: 12px; margin-bottom: 10px; cursor: pointer;"> <input class="form-check-input" type="radio" name="payment_method" id="online" value="online"> <label class="form-check-label" for="online" style="display: flex; align-items: center; gap: 10px; cursor: pointer; width: 100%;"> <i class="fas fa-credit-card" style="color: var(--primary-rose-gold); font-size: 1.3rem;"></i> <div> <strong>Online Payment</strong> <div style="font-size: 0.85rem; color: var(--gray-600);">Pay securely with card / UPI / Net Banking</div> </div> </label> </div> </div> </div> </div> <!-- Order Summary --> <div class="col-lg-5"> <div class="order-summary" style="background: var(--white); border-radius: 16px; padding: 30px; box-shadow: var(--shadow-sm); position: sticky; top: 100px;"> <h3 style="font-size: 1.3rem; margin-bottom: 25px; font-family: var(--font-primary); font-weight: 600;">Your Order</h3> <div style="max-height: 300px; overflow-y: auto; margin-bottom: 20px;"> <?php foreach ($cart_items as $item): $price = $item['sale_price'] > 0 ? $item['sale_price'] : $item['regular_price']; $item_total = $price * $item['quantity']; $image_url = $item['thumbnail'] ? UPLOADS_URL . 'products/' . $item['thumbnail'] : ASSETS_PATH . 'images/no-image.jpg'; ?> <div style="display: flex; gap: 15px; padding: 12px 0; border-bottom: 1px solid var(--gray-200);"> <img src="<?= $image_url ?>" alt="<?= htmlspecialchars($item['name']) ?>" style="width: 60px; height: 60px; border-radius: 8px; object-fit: cover;"> <div style="flex: 1;"> <h6 style="font-family: var(--font-primary); font-size: 0.9rem; margin-bottom: 5px; font-weight: 500;"><?= htmlspecialchars($item['name']) ?></h6> <small class="text-muted">Qty: <?= $item['quantity'] ?> × <?= format_currency($price) ?></small> </div> <div style="font-weight: 600; color: var(--primary-rose-gold); font-size: 0.9rem;"> <?= format_currency($item_total) ?> </div> </div> <?php endforeach; ?> </div> <div style="border-top: 1px solid var(--gray-200); padding-top: 20px;"> <div style="display: flex; justify-content: space-between; margin-bottom: 12px;"> <span>Subtotal</span> <span style="font-weight: 600;"><?= format_currency($cart_total) ?></span> </div> <div style="display: flex; justify-content: space-between; margin-bottom: 12px;"> <span>Shipping</span> <span style="font-weight: 600;"><?= $shipping_cost > 0 ? format_currency($shipping_cost) : '<span style="color: var(--success);">FREE</span>' ?></span> </div> <div style="display: flex; justify-content: space-between; margin-bottom: 12px;"> <span>Tax (<?= TAX_RATE ?>%)</span> <span style="font-weight: 600;"><?= format_currency($tax) ?></span> </div> <?php if ($discount > 0): ?> <div style="display: flex; justify-content: space-between; margin-bottom: 12px; color: var(--success);"> <span>Discount</span> <span style="font-weight: 600;">-<?= format_currency($discount) ?></span> </div> <?php endif; ?> <div style="display: flex; justify-content: space-between; margin-top: 20px; padding-top: 20px; border-top: 1px solid var(--gray-200); font-size: 1.2rem;"> <strong>Total</strong> <strong style="color: var(--primary-rose-gold);"><?= format_currency($grand_total) ?></strong> </div> </div> <button type="submit" class="btn-luxury w-100" style="margin-top: 25px; justify-content: center;"> <i class="fas fa-lock"></i> Place Order </button> <p style="text-align: center; margin-top: 15px; font-size: 0.85rem; color: var(--gray-600); margin-bottom: 0;"> <i class="fas fa-shield-alt"></i> Your payment information is processed securely </p> </div> </div> </div> </form> </div> </section> <script> document.querySelectorAll('.payment-method-item').forEach(function(item) { item.addEventListener('click', function() { document.querySelectorAll('.payment-method-item').forEach(function(i) { i.style.borderColor = 'var(--gray-200)'; }); this.style.borderColor = 'var(--primary-rose-gold)'; }); }); document.getElementById('same_as_billing')?.addEventListener('change', function() { document.querySelector('.shipping-fields').style.display = this.checked ? 'none' : 'block'; }); </script> </main> <?php require_once '../includes/footer.php'; ?>
Save Changes
Cancel
Create New File
Create New Folder