Buatlah logika checkout keranjang belanja untuk toko online. Endpoint menerima payload barang belanjaan, memvalidasi stok produk, menerapkan voucher diskon khusus kategori, menghitung PPN 11%, dan mengembalikan rincian tagihan.
POST /cart/checkout
{
"items": [
{ "productId": "P1", "quantity": 2 },
{ "productId": "P2", "quantity": 1 }
],
"voucherCode": "TECH20"
}
{
"subtotal": 2050,
"discount": 150,
"netSubtotal": 1900,
"tax": 209,
"total": 2109
}
const products = [
{ id: "P1", name: "Laptop", price: 1000, category: "ELECTRONICS", stock: 3 },
{ id: "P2", name: "Mouse", price: 50, category: "ELECTRONICS", stock: 10 },
{ id: "P3", name: "Shirt", price: 30, category: "FASHION", stock: 5 }
];
const vouchers = [
{ code: "TECH20", category: "ELECTRONICS", discountPercent: 20, maxDiscount: 150 }
];
items tidak ada atau array kosong → kembalikan HTTP 400 "Items array is required".quantity > product.stock → kembalikan HTTP 400 "Product X out of stock".ELECTRONICS, dengan batas diskon maksimal (maxDiscount) = 150.tax = (subtotal - discount) × 0.11product.stock -= quantity) setelah transaksi berhasil.Idempotency pada Payment Gateway Di komentar kode solusi Anda, jelaskan bagaimana cara menjamin Idempotency menggunakan header
X-Idempotency-Keyuntuk mencegah penagihan ganda (double charge) saat koneksi jaringan terputus dan user melakukan retry.