<?php
$items = cart_items();
if (!$items) {
flash('Sepetiniz boş.', 'error');
go(url('sepet'));
}
$methods = payment_methods();
$customer = current_customer();
$last = $customer ? db_one('SELECT * FROM orders WHERE customer_id = ? ORDER BY id DESC LIMIT 1', [$customer['id']]) : null;
$form = [
'name' => post('name', $last['name'] ?? $customer['name'] ?? ''),
'email' => post('email', $customer['email'] ?? ''),
'phone' => post('phone', $last['phone'] ?? $customer['phone'] ?? ''),
'city' => post('city', $last['city'] ?? ''),
'district' => post('district', $last['district'] ?? ''),
'address' => post('address', $last['address'] ?? ''),
'note' => post('note', ''),
'payment' => post('payment', array_key_first($methods) ?? ''),
];
$error = null;
if (is_post()) {
csrf_check();
// Şehir değişince kargo ve toplam yeniden hesaplanır
if (post('action') === 'quote') {
$fee = $methods[$form['payment']][2] ?? 0;
reply(true, '', ['totals_html' => render('totals', ['totals' => cart_totals($items, $form['city']), 'fee' => $fee])]);
}
$totals = cart_totals($items, $form['city']);
$error = match (true) {
!$methods => 'Şu an ödeme yöntemi aktif değil, lütfen bizimle iletişime geçin.',
in_array('', [$form['name'], $form['phone'], $form['city'], $form['district'], $form['address']], true)
=> 'Lütfen teslimat bilgilerini eksiksiz doldurun.',
!filter_var($form['email'], FILTER_VALIDATE_EMAIL) => 'Geçerli bir e-posta adresi girin.',
!isset($methods[$form['payment']]) => 'Lütfen bir ödeme yöntemi seçin.',
!checked('agreement') => 'Devam etmek için satış sözleşmesini onaylayın.',
default => null,
};
if (!$error) {
try {
$order_id = order_create($form, $items, $totals, $form['payment']);
cart_clear();
sess_set('last_order', $order_id);
if ($form['payment'] === 'kart') {
finish('', payment_url(db_find('orders', $order_id)));
}
finish('', url('siparis-tamam'));
} catch (RuntimeException $e) {
$error = $e->getMessage();
}
}
if (is_ajax()) {
reply(false, $error);
}
}
$totals = cart_totals($items, $form['city']);
$fee = $methods[$form['payment']][2] ?? 0;
view_header('Ödeme');
?>
<section class="container-x pt-8">
<?php breadcrumb(['Sepetim' => url('sepet'), 'Ödeme' => null]) ?>
<h1 class="font-display mt-6 text-4xl font-semibold tracking-tight text-ink sm:text-5xl">Ödeme</h1>
<?php if (!$customer): ?>
<p class="mt-4 text-sm text-zinc-600">Üye misiniz? <a href="<?= url('giris') ?>" class="font-semibold text-ink underline underline-offset-4">Giriş yapın</a>, siparişlerinizi hesabınızdan takip edin.</p>
<?php endif ?>
<form method="post" action="<?= url('odeme') ?>" data-ajax class="mt-10 grid gap-10 lg:grid-cols-12 lg:gap-14">
<?= csrf_field() ?>
<div class="space-y-10 lg:col-span-7 xl:col-span-8">
<?= form_error($error) ?>
<fieldset>
<legend class="flex items-center gap-3 text-lg font-semibold text-ink"><span class="flex size-7 items-center justify-center rounded-full bg-ink text-xs text-white">1</span>İletişim bilgileri</legend>
<div class="mt-6 grid gap-5 sm:grid-cols-2">
<div class="sm:col-span-2">
<label class="label">Ad soyad</label>
<input name="name" value="<?= e($form['name']) ?>" class="input" autocomplete="name" required>
</div>
<div>
<label class="label">E-posta</label>
<input name="email" type="email" value="<?= e($form['email']) ?>" class="input" autocomplete="email" required>
</div>
<div>
<label class="label">Telefon</label>
<input name="phone" type="tel" value="<?= e($form['phone']) ?>" class="input" autocomplete="tel" placeholder="05xx xxx xx xx" required>
</div>
</div>
</fieldset>
<fieldset>
<legend class="flex items-center gap-3 text-lg font-semibold text-ink"><span class="flex size-7 items-center justify-center rounded-full bg-ink text-xs text-white">2</span>Teslimat adresi</legend>
<div class="mt-6 grid gap-5 sm:grid-cols-2">
<div>
<label class="label">İl</label>
<input name="city" value="<?= e($form['city']) ?>" data-quote class="input" autocomplete="address-level1" required>
</div>
<div>
<label class="label">İlçe</label>
<input name="district" value="<?= e($form['district']) ?>" class="input" autocomplete="address-level2" required>
</div>
<div class="sm:col-span-2">
<label class="label">Açık adres</label>
<textarea name="address" rows="3" class="input" autocomplete="street-address" placeholder="Mahalle, sokak, bina ve daire no" required><?= e($form['address']) ?></textarea>
</div>
<div class="sm:col-span-2">
<label class="label">Sipariş notu <span class="font-normal text-zinc-400">(isteğe bağlı)</span></label>
<input name="note" value="<?= e($form['note']) ?>" class="input" placeholder="Örneğin: Kapıya bırakın">
</div>
</div>
</fieldset>
<fieldset>
<legend class="flex items-center gap-3 text-lg font-semibold text-ink"><span class="flex size-7 items-center justify-center rounded-full bg-ink text-xs text-white">3</span>Ödeme yöntemi</legend>
<div class="mt-6 grid gap-3 sm:grid-cols-2">
<?php foreach ($methods as $key => [$label, $text, $method_fee, $name]): ?>
<label class="relative cursor-pointer">
<input type="radio" name="payment" value="<?= $key ?>" data-fee="<?= $method_fee ?>" class="peer sr-only" <?= $form['payment'] === $key ? 'checked' : '' ?>>
<span class="flex h-full gap-4 rounded-2xl border border-zinc-200 p-5 transition peer-checked:border-ink peer-checked:ring-1 peer-checked:ring-ink hover:border-zinc-300">
<span class="flex size-10 shrink-0 items-center justify-center rounded-full bg-zinc-100 text-ink"><?= icon($name, 'size-5') ?></span>
<span>
<span class="block text-sm font-semibold text-ink"><?= $label ?></span>
<span class="mt-1 block text-[13px] text-zinc-500"><?= $text ?></span>
<?php if ($method_fee > 0): ?>
<span class="mt-2 block text-xs font-medium text-zinc-700">+<?= money($method_fee) ?> hizmet bedeli</span>
<?php endif ?>
</span>
</span>
<span class="absolute top-5 right-5 hidden text-ink peer-checked:block"><?= icon('check-circle', 'size-5', '2') ?></span>
</label>
<?php endforeach ?>
</div>
</fieldset>
</div>
<aside class="lg:col-span-5 xl:col-span-4">
<div class="rounded-3xl bg-zinc-50 p-6 sm:p-8 lg:sticky lg:top-28">
<h2 class="font-display text-xl font-semibold tracking-tight">Siparişiniz</h2>
<ul class="mt-6 space-y-4">
<?php foreach ($items as $item): ?>
<li class="flex gap-4">
<span class="relative h-20 w-16 shrink-0 overflow-hidden rounded-xl bg-white ring-1 ring-zinc-200">
<?= product_image($item, 'h-full w-full object-cover', 'text-2xl') ?>
<span class="absolute -top-0 -right-0 flex size-5 items-center justify-center rounded-bl-lg bg-ink text-[10px] font-bold text-white"><?= $item['qty'] ?></span>
</span>
<div class="min-w-0 flex-1">
<p class="line-clamp-2 text-sm font-medium text-ink"><?= e($item['name']) ?></p>
<?php if ($item['variant']): ?>
<p class="mt-0.5 text-xs text-zinc-500"><?= e(product_variant_label($item)) ?></p>
<?php endif ?>
</div>
<p class="text-sm font-semibold"><?= money($item['line_total']) ?></p>
</li>
<?php endforeach ?>
</ul>
<div class="mt-6 border-t border-zinc-200 pt-2">
<div data-totals><?php include PARTS . '/totals.php' ?></div>
</div>
<label class="mt-6 flex cursor-pointer items-start gap-3 text-[13px] leading-5 text-zinc-600">
<input type="checkbox" name="agreement" value="1" class="mt-0.5 size-4 shrink-0 rounded accent-ink" required>
<span>Ön bilgilendirme formunu ve mesafeli satış sözleşmesini okudum, onaylıyorum.</span>
</label>
<button class="btn-primary mt-6 w-full py-4 text-base"><?= icon('lock', 'size-4', '2') ?>Siparişi onayla</button>
<a href="<?= url('sepet') ?>" class="mt-4 block text-center text-sm text-zinc-500 hover:text-ink">Sepete dön</a>
</div>
</aside>
</form>
</section>
<?php view_footer() ?>
Lisans bilgisi projenin LICENSE dosyasında.