259 lines
10 KiB
PHP
259 lines
10 KiB
PHP
<?php include '../template/template1.php'; ?>
|
|
<?php
|
|
require '../vendor/autoload.php';
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
if (!isset($_SESSION['status_login']) || !$_SESSION['status_login']) {
|
|
echo "<script>
|
|
Swal.fire({
|
|
icon: 'warning',
|
|
title: 'Akses Ditolak!',
|
|
text: 'Harap login terlebih dahulu.',
|
|
confirmButtonColor: '#3085d6',
|
|
confirmButtonText: 'OK'
|
|
}).then((result) => {
|
|
if (result.isConfirmed) {
|
|
window.location='/login';
|
|
}
|
|
});
|
|
</script>";
|
|
exit;
|
|
}
|
|
|
|
$id_pembeli = $_SESSION['id_pembeli'];
|
|
|
|
$stmt = $conn->prepare("SELECT nama_pembeli, alamat, saldo, email, no_hp FROM pembeli WHERE id_pembeli = ?");
|
|
$stmt->bind_param("i", $id_pembeli);
|
|
$stmt->execute();
|
|
$pembeli_data = $stmt->get_result()->fetch_assoc(); // Ganti nama variabel agar tidak konflik
|
|
$alamat_pengiriman_db = $pembeli_data['alamat']; // Alamat dari DB
|
|
$saldo = $pembeli_data['saldo'];
|
|
$email = $pembeli_data['email'];
|
|
$no_hp = preg_replace('/^0/', '62', $pembeli_data['no_hp']);
|
|
$nama_pembeli = $pembeli_data['nama_pembeli'];
|
|
|
|
$stmt = $conn->prepare("SELECT c.id_cart, c.id_produk, c.jumlah, c.potong, p.nama_produk, p.harga_jual, p.stok
|
|
FROM cart c
|
|
JOIN produk p ON c.id_produk = p.id_produk
|
|
WHERE c.id_pembeli = ?");
|
|
$stmt->bind_param("i", $id_pembeli);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
|
|
$keranjang = [];
|
|
$total_harga_produk_checkout = 0; // Ganti nama variabel
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
|
if ($row['jumlah'] > $row['stok']) {
|
|
echo "<script>
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'Stok Habis!',
|
|
text: 'Stok tidak mencukupi untuk {$row['nama_produk']}.',
|
|
confirmButtonColor: '#d33',
|
|
confirmButtonText: 'OK'
|
|
}).then((result) => {
|
|
if (result.isConfirmed) {
|
|
window.history.back();
|
|
}
|
|
});
|
|
</script>";
|
|
exit;
|
|
}
|
|
|
|
$keranjang[] = $row;
|
|
$total_harga_produk_checkout += $row['harga_jual'] * $row['jumlah']; // Gunakan variabel baru
|
|
}
|
|
|
|
if (empty($keranjang)) {
|
|
echo "<script>
|
|
Swal.fire({
|
|
icon: 'info',
|
|
title: 'Keranjang Kosong!',
|
|
text: 'Silakan tambahkan produk ke keranjang sebelum checkout.',
|
|
confirmButtonColor: '#3085d6',
|
|
confirmButtonText: 'OK'
|
|
}).then((result) => {
|
|
if (result.isConfirmed) {
|
|
window.location='/produk';
|
|
}
|
|
});
|
|
</script>";
|
|
exit;
|
|
}
|
|
|
|
// Proses checkout
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$alamat_input_user = trim($_POST['alamat']); // Alamat yang diinput/diubah user di form
|
|
$metode_pembayaran = $_POST['metode_pembayaran'];
|
|
|
|
// Perhitungan ongkir HARUS berdasarkan alamat yang diinput user saat checkout, BUKAN dari DB
|
|
// Ini penting karena user bisa mengubah alamat di form checkout
|
|
$alamat_normal_checkout = strtolower(str_replace(['.', ',', '-', '_', '/', '\\'], ' ', $alamat_input_user));
|
|
$is_nganjuk_checkout = strpos($alamat_normal_checkout, 'nganjuk') !== false;
|
|
|
|
$ongkos_kirim_checkout = 0;
|
|
if ($total_harga_produk_checkout > 0) { // Gunakan total harga produk yang benar
|
|
if ($total_harga_produk_checkout < 50000) {
|
|
$ongkos_kirim_checkout = $is_nganjuk_checkout ? 0 : 5000;
|
|
} elseif ($total_harga_produk_checkout >= 50000 && $total_harga_produk_checkout < 200000) { // Koreksi kondisi
|
|
$ongkos_kirim_checkout = $is_nganjuk_checkout ? 3000 : 5000;
|
|
} else { // total_harga_produk_checkout >= 200000
|
|
$ongkos_kirim_checkout = 10000;
|
|
}
|
|
} else {
|
|
$ongkos_kirim_checkout = 0;
|
|
}
|
|
|
|
$total_pembayaran_checkout = $total_harga_produk_checkout + $ongkos_kirim_checkout;
|
|
|
|
if ($metode_pembayaran === 'SALDO' && $saldo < $total_pembayaran_checkout) {
|
|
echo "<script>
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'Saldo Tidak Cukup!',
|
|
text: 'Silakan isi saldo terlebih dahulu sebelum melakukan pembelian.',
|
|
confirmButtonColor: '#d33',
|
|
confirmButtonText: 'OK'
|
|
}).then((result) => {
|
|
if (result.isConfirmed) {
|
|
window.history.back();
|
|
}
|
|
});
|
|
</script>";
|
|
exit;
|
|
}
|
|
|
|
// Atur status order sesuai metode pembayaran
|
|
$status_order = match($metode_pembayaran) {
|
|
'SALDO' => 'Pembayaran Sukses',
|
|
'QRIS' => 'Menunggu Pembayaran',
|
|
'COD' => 'Belum Dibayar',
|
|
default => 'Menunggu Pembayaran'
|
|
};
|
|
|
|
$conn->begin_transaction();
|
|
|
|
try {
|
|
// Gunakan alamat yang diinput user untuk order
|
|
$stmt = $conn->prepare("INSERT INTO orders (id_pembeli, alamat, metode_pembayaran, total_harga, ongkos_kirim, total_pembayaran, status_order) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
|
$stmt->bind_param("issddds", $id_pembeli, $alamat_input_user, $metode_pembayaran, $total_harga_produk_checkout, $ongkos_kirim_checkout, $total_pembayaran_checkout, $status_order);
|
|
$stmt->execute();
|
|
$id_order = $stmt->insert_id;
|
|
|
|
foreach ($keranjang as $item) {
|
|
$stmt = $conn->prepare("INSERT INTO order_details (id_order, id_produk, jumlah, potong, harga) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->bind_param("iiiii", $id_order, $item['id_produk'], $item['jumlah'], $item['potong'], $item['harga_jual']);
|
|
$stmt->execute();
|
|
|
|
$stmt = $conn->prepare("UPDATE produk SET stok = stok - ? WHERE id_produk = ?");
|
|
$stmt->bind_param("ii", $item['jumlah'], $item['id_produk']);
|
|
$stmt->execute();
|
|
}
|
|
|
|
if ($metode_pembayaran === 'SALDO') {
|
|
$saldo_baru = $saldo - $total_pembayaran_checkout;
|
|
$stmt = $conn->prepare("UPDATE pembeli SET saldo = ? WHERE id_pembeli = ?");
|
|
$stmt->bind_param("di", $saldo_baru, $id_pembeli);
|
|
$stmt->execute();
|
|
}
|
|
|
|
$stmt = $conn->prepare("DELETE FROM cart WHERE id_pembeli = ?");
|
|
$stmt->bind_param("i", $id_pembeli);
|
|
$stmt->execute();
|
|
|
|
$conn->commit();
|
|
|
|
// Persiapan Email
|
|
$mail = new PHPMailer(true);
|
|
try {
|
|
$mail->isSMTP();
|
|
$mail->Host = 'smtp.gmail.com';
|
|
$mail->SMTPAuth = true;
|
|
$mail->Username = 'chickycuts2025@gmail.com'; // Ganti dengan email Anda
|
|
$mail->Password = 'etccszhnwgvvgxfx'; // Ganti dengan App Password Gmail Anda
|
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
|
$mail->Port = 587;
|
|
|
|
$mail->setFrom('chickycuts2025@gmail.com', 'ChickyCuts');
|
|
$mail->addAddress($email, $nama_pembeli);
|
|
|
|
$mail->isHTML(true);
|
|
$mail->Subject = 'Pesanan Anda (#'.$id_order.') Telah Diproses - ChickyCuts';
|
|
$mail->Body = "
|
|
<p>Halo {$nama_pembeli},</p>
|
|
<p>Terima kasih telah melakukan pemesanan di ChickyCuts! Pesanan Anda dengan nomor <strong>#{$id_order}</strong> telah kami terima.</p>
|
|
<p><strong>Detail Pesanan:</strong></p>
|
|
<ul>
|
|
<li>Nomor Pesanan: #{$id_order}</li>
|
|
<li>Status Pesanan: {$status_order}</li>
|
|
<li>Alamat Pengiriman: {$alamat_input_user}</li>
|
|
<li>Total Harga Produk: Rp. ".number_format($total_harga_produk_checkout, 0, ',', '.')."</li>
|
|
<li>Ongkos Kirim: Rp. ".number_format($ongkos_kirim_checkout, 0, ',', '.')."</li>
|
|
<li><strong>Total Pembayaran: Rp. ".number_format($total_pembayaran_checkout, 0, ',', '.')."</strong></li>
|
|
<li>Metode Pembayaran: {$metode_pembayaran}</li>
|
|
</ul>";
|
|
|
|
if ($metode_pembayaran === 'QRIS' && $status_order === 'Menunggu Pembayaran') {
|
|
// Anda bisa menyertakan gambar QRIS atau link ke halaman pembayaran QRIS di sini jika perlu
|
|
// Misalnya: $mail->Body .= "<p>Silakan lakukan pembayaran melalui QRIS yang tersedia di halaman checkout atau <a href='URL_QRIS_ANDA'>klik di sini</a>.</p>";
|
|
$mail->Body .= "<p>Silakan lakukan pembayaran melalui QRIS yang telah ditampilkan pada halaman checkout.</p>";
|
|
}
|
|
|
|
$mail->Body .= "
|
|
<p>Kami akan segera memproses pesanan Anda setelah pembayaran dikonfirmasi (jika bukan COD atau Saldo).</p>
|
|
<p>Terima kasih telah berbelanja dengan kami.</p>
|
|
<p>Salam Hangat,<br>Tim ChickyCuts</p>";
|
|
|
|
$mail->send();
|
|
// Tidak perlu menampilkan alert sukses email di sini, cukup alert sukses pesanan
|
|
|
|
} catch (Exception $e) {
|
|
// Email gagal dikirim, tapi pesanan tetap diproses. Mungkin log errornya.
|
|
// error_log("Mailer Error: " . $mail->ErrorInfo);
|
|
// Anda bisa menambahkan alert di sini jika ingin memberitahu user email gagal,
|
|
// tapi biasanya cukup jika pesanan utamanya sukses.
|
|
}
|
|
|
|
// Notifikasi Sukses Pesanan Utama
|
|
echo "<script>
|
|
Swal.fire({
|
|
icon: 'success',
|
|
title: 'Pesanan Diproses!',
|
|
text: 'Pesanan Anda telah berhasil dilakukan. Nomor pesanan: #{$id_order}. Detail juga dikirim ke email Anda.',
|
|
confirmButtonText: 'OK'
|
|
}).then((result) => {
|
|
if (result.isConfirmed) {
|
|
window.location='/pesanan?id_order={$id_order}';
|
|
}
|
|
});
|
|
</script>";
|
|
exit; // Penting untuk menghentikan eksekusi setelah mengirim respons
|
|
|
|
} catch (Exception $e) {
|
|
$conn->rollback();
|
|
// error_log("Checkout Error: " . $e->getMessage()); // Log error
|
|
echo "<script>
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'Gagal!',
|
|
text: 'Terjadi kesalahan saat memproses pesanan: " . addslashes($e->getMessage()) . "',
|
|
confirmButtonColor: '#d33',
|
|
confirmButtonText: 'OK'
|
|
}).then((result) => {
|
|
if (result.isConfirmed) {
|
|
window.location='/checkout'; // Kembali ke halaman checkout
|
|
}
|
|
});
|
|
</script>";
|
|
exit;
|
|
}
|
|
} else {
|
|
// Jika bukan POST request, redirect atau tampilkan error
|
|
echo "<script>window.location='/checkout';</script>";
|
|
exit;
|
|
}
|
|
?>
|
|
<?php include '../template/template2.php'; ?>
|