38283-vm/checkout.php
2026-02-08 08:34:40 +00:00

136 lines
5.7 KiB
PHP

<?php
$page_title = '提交订单 - 豪软世界';
require_once 'includes/header.php';
// Handle direct purchase from product.php
$direct_id = $_GET['direct_id'] ?? null;
// Handle POST request to create order
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$db = db();
$payment_method = $_POST['payment_method'] ?? 'USDT (TRC20)';
$cart_data = json_decode($_POST['cart_data'], true);
if (empty($cart_data)) {
header("Location: cart.php");
exit;
}
// Calculate total
$total = 0;
foreach ($cart_data as $item) {
$total += $item['price'] * $item['qty'];
}
// Generate order number
$order_no = 'HR' . date('YmdHis') . rand(100, 999);
// Create order - contact_info is now optional or empty
$stmt = $db->prepare("INSERT INTO orders (order_no, total_amount, payment_method, contact_info, status) VALUES (?, ?, ?, '', 'pending')");
$stmt->execute([$order_no, $total, $payment_method]);
$order_id = $db->lastInsertId();
// Create order items
foreach ($cart_data as $item) {
$stmt = $db->prepare("INSERT INTO order_items (order_id, product_id, quantity, price_usdt) VALUES (?, ?, ?, ?)");
$stmt->execute([$order_id, $item['id'], $item['qty'], $item['price']]);
}
// Redirect to payment
header("Location: payment.php?order_no=" . $order_no);
exit;
}
?>
<main class="py-5">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-6">
<div class="glass-card p-5 bg-white shadow-sm">
<h2 class="fw-bold text-dark mb-4"><i class="bi bi-file-earmark-text text-primary me-2"></i> 确认订单信息</h2>
<p class="text-muted mb-5">请核对您的订单商品,确认无误后点击下方按钮进入支付页面。</p>
<form id="checkout-form" method="POST">
<input type="hidden" name="cart_data" id="cart-data-input">
<div class="mb-5">
<label class="form-label text-dark fw-bold">支付方式</label>
<div class="row g-3">
<div class="col-12">
<div class="payment-option p-4 rounded-4 border border-primary bg-primary bg-opacity-10 d-flex align-items-center gap-3">
<i class="bi bi-currency-bitcoin fs-2 text-primary"></i>
<div>
<div class="text-dark fw-bold fs-5">USDT (TRC20)</div>
<div class="text-muted small">推荐使用,区块链自动确认</div>
</div>
<i class="bi bi-check-circle-fill ms-auto text-primary fs-4"></i>
</div>
<input type="hidden" name="payment_method" value="USDT (TRC20)">
</div>
</div>
</div>
<div class="order-summary-box mb-5 p-4 bg-light rounded-4">
<h6 class="text-dark fw-bold mb-4">订单详情</h6>
<div id="checkout-items-list">
<!-- JS Populated -->
</div>
<hr class="my-4">
<div class="d-flex justify-content-between align-items-center">
<span class="text-dark fw-bold">应付总额</span>
<span class="fs-2 fw-bold text-primary" id="checkout-total">0.00 USDT</span>
</div>
</div>
<button type="submit" class="btn btn-primary btn-lg w-100 py-3 rounded-pill shadow-lg fw-bold">
确认下单并去支付 <i class="bi bi-arrow-right ms-2"></i>
</button>
</form>
</div>
</div>
</div>
</div>
</main>
<script>
document.addEventListener('DOMContentLoaded', function() {
let cart = JSON.parse(localStorage.getItem('cart') || '[]');
// If direct purchase from product.php
<?php if ($direct_id): ?>
// If coming from direct_id, we might want to override cart for this session or just add it
// For simplicity, let's assume the user already clicked "Add to Cart" or we fetch product details
// In this app, we'll fetch from the DOM or just rely on the cart if addToCart was called.
// If cart is empty, we should probably handle it.
<?php endif; ?>
if (cart.length === 0) {
window.location.href = 'index.php';
return;
}
document.getElementById('cart-data-input').value = JSON.stringify(cart);
let html = '';
let total = 0;
cart.forEach(item => {
const subtotal = item.price * item.qty;
total += subtotal;
html += `
<div class="d-flex justify-content-between text-muted mb-3 fs-6">
<span>${item.name} <span class="badge bg-secondary bg-opacity-10 text-secondary ms-2">x${item.qty}</span></span>
<span class="text-dark fw-bold">${subtotal.toFixed(2)} USDT</span>
</div>
`;
});
document.getElementById('checkout-items-list').innerHTML = html;
document.getElementById('checkout-total').textContent = total.toFixed(2) + ' USDT';
});
</script>
<style>
.payment-option { border-width: 2px !important; }
</style>
<?php require_once 'includes/footer.php'; ?>