150 lines
8.1 KiB
PHP
150 lines
8.1 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
require_once __DIR__ . '/store.php';
|
||
|
||
$summary = store_cart_summary();
|
||
if (!$summary['lines']) {
|
||
store_flash('warning', 'Keranjang kosong. Tambahkan produk terlebih dahulu.');
|
||
header('Location: cart.php');
|
||
exit;
|
||
}
|
||
|
||
$currentUser = store_current_user();
|
||
if (!$currentUser) {
|
||
store_flash('warning', 'Silakan login terlebih dahulu untuk melanjutkan checkout.');
|
||
header('Location: auth.php?mode=login&redirect_to=checkout.php');
|
||
exit;
|
||
}
|
||
|
||
$formData = store_checkout_defaults();
|
||
$errors = [];
|
||
|
||
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST') {
|
||
$result = store_create_order($_POST);
|
||
if (!empty($result['success'])) {
|
||
store_flash('success', 'Pesanan berhasil dibuat. Gunakan kode pesanan untuk melacak status.');
|
||
header('Location: order_status.php?order=' . urlencode((string)$result['order_number']) . '&created=1');
|
||
exit;
|
||
}
|
||
|
||
$formData = $result['data'] ?? $formData;
|
||
$errors = $result['errors'] ?? [];
|
||
}
|
||
|
||
store_page_start('Checkout', 'Lengkapi data pelanggan, alamat, dan metode pembayaran untuk membuat pesanan.', ['noindex' => true]);
|
||
?>
|
||
<section class="section-block pt-0">
|
||
<div class="section-heading mb-4">
|
||
<span class="eyebrow">Checkout</span>
|
||
<h1 class="section-title">Simpan pesanan ke sistem.</h1>
|
||
<p class="section-copy mb-0">Status awal pesanan adalah <strong>Menunggu Pembayaran</strong>. Instruksi pembayaran akan tampil setelah order berhasil dibuat.</p>
|
||
</div>
|
||
|
||
<?php if (isset($errors['cart'])): ?>
|
||
<div class="alert alert-warning border-0 shadow-sm mb-4" role="alert">
|
||
<?= h($errors['cart']) ?>
|
||
</div>
|
||
<?php endif; ?>
|
||
|
||
<div class="row g-4 align-items-start">
|
||
<div class="col-lg-7">
|
||
<form action="checkout.php" method="post" class="surface-panel p-4 p-lg-5" data-auto-disable>
|
||
<div class="row g-3">
|
||
<div class="col-md-6">
|
||
<label class="form-label" for="customer_name">Nama penerima</label>
|
||
<input id="customer_name" name="customer_name" type="text" class="form-control<?= store_input_class($errors, 'customer_name') ?>" value="<?= h($formData['customer_name']) ?>" required>
|
||
<?php if (isset($errors['customer_name'])): ?><div class="invalid-feedback"><?= h($errors['customer_name']) ?></div><?php endif; ?>
|
||
</div>
|
||
<div class="col-md-6">
|
||
<label class="form-label" for="phone">Nomor telepon</label>
|
||
<input id="phone" name="phone" type="tel" class="form-control<?= store_input_class($errors, 'phone') ?>" value="<?= h($formData['phone']) ?>" required>
|
||
<?php if (isset($errors['phone'])): ?><div class="invalid-feedback"><?= h($errors['phone']) ?></div><?php endif; ?>
|
||
</div>
|
||
<div class="col-12">
|
||
<label class="form-label" for="email">Email</label>
|
||
<input id="email" name="email" type="email" class="form-control<?= store_input_class($errors, 'email') ?>" value="<?= h($formData['email']) ?>" required>
|
||
<?php if (isset($errors['email'])): ?><div class="invalid-feedback"><?= h($errors['email']) ?></div><?php endif; ?>
|
||
</div>
|
||
<div class="col-12">
|
||
<label class="form-label" for="address">Alamat lengkap</label>
|
||
<textarea id="address" name="address" rows="4" class="form-control<?= store_input_class($errors, 'address') ?>" required><?= h($formData['address']) ?></textarea>
|
||
<?php if (isset($errors['address'])): ?><div class="invalid-feedback"><?= h($errors['address']) ?></div><?php endif; ?>
|
||
</div>
|
||
<div class="col-12">
|
||
<label class="form-label" for="note">Catatan pesanan <span class="text-muted">(opsional)</span></label>
|
||
<textarea id="note" name="note" rows="3" class="form-control"><?= h($formData['note']) ?></textarea>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="section-divider"></div>
|
||
|
||
<div>
|
||
<div class="d-flex align-items-center justify-content-between flex-wrap gap-2 mb-3">
|
||
<div>
|
||
<h2 class="h5 mb-1">Metode pembayaran</h2>
|
||
<p class="text-muted mb-0">Pilih salah satu. Instruksi lengkap muncul di halaman status pesanan.</p>
|
||
</div>
|
||
</div>
|
||
<div class="row g-3">
|
||
<?php foreach (store_payment_methods() as $value => $method): ?>
|
||
<div class="col-md-4">
|
||
<div class="payment-option">
|
||
<input class="btn-check" type="radio" name="payment_method" id="payment-<?= h($value) ?>" value="<?= h($value) ?>" <?= $formData['payment_method'] === $value ? 'checked' : '' ?>>
|
||
<label class="choice-card" for="payment-<?= h($value) ?>">
|
||
<span class="choice-title"><?= h($method['label']) ?></span>
|
||
<span class="choice-copy"><?= h($method['description']) ?></span>
|
||
</label>
|
||
</div>
|
||
</div>
|
||
<?php endforeach; ?>
|
||
</div>
|
||
<?php if (isset($errors['payment_method'])): ?>
|
||
<div class="text-danger small mt-2"><?= h($errors['payment_method']) ?></div>
|
||
<?php endif; ?>
|
||
</div>
|
||
|
||
<div class="d-flex flex-column flex-sm-row justify-content-between gap-2 mt-4">
|
||
<a class="btn btn-outline-secondary" href="cart.php">Kembali ke keranjang</a>
|
||
<button class="btn btn-dark btn-lg" type="submit">Buat pesanan</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
<div class="col-lg-5">
|
||
<aside class="summary-card sticky-summary">
|
||
<div class="card-kicker">Order summary</div>
|
||
<h2 class="summary-title">Ringkasan item yang akan disimpan.</h2>
|
||
<div class="receipt-card receipt-card--flat">
|
||
<?php foreach ($summary['lines'] as $line): ?>
|
||
<div class="receipt-line">
|
||
<span><?= h($line['product']['name']) ?> × <?= h((string)$line['quantity']) ?></span>
|
||
<strong><?= h(store_money((float)$line['line_total'])) ?></strong>
|
||
</div>
|
||
<?php endforeach; ?>
|
||
<div class="receipt-line">
|
||
<span>Subtotal</span>
|
||
<strong><?= h(store_money((float)$summary['subtotal'])) ?></strong>
|
||
</div>
|
||
<div class="receipt-line">
|
||
<span>Ongkir</span>
|
||
<strong><?= h(store_money((float)$summary['shipping_fee'])) ?></strong>
|
||
</div>
|
||
<div class="receipt-line receipt-line--total">
|
||
<span>Total pembayaran</span>
|
||
<strong><?= h(store_money((float)$summary['grand_total'])) ?></strong>
|
||
</div>
|
||
</div>
|
||
<div class="info-box mt-4 mb-0">
|
||
<div class="card-kicker">Setelah checkout</div>
|
||
<ul class="list-clean compact-list mb-0">
|
||
<li><span class="list-index">A</span><span>Sistem membuat order number unik.</span></li>
|
||
<li><span class="list-index">B</span><span>Data alamat dan item pesanan tersimpan di MySQL.</span></li>
|
||
<li><span class="list-index">C</span><span>Pelanggan bisa membuka status pesanan kapan saja.</span></li>
|
||
</ul>
|
||
</div>
|
||
</aside>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<?php store_page_end(); ?>
|