From 016b9f1e9b234f546a0354b818fd33712379a803 Mon Sep 17 00:00:00 2001
From: Miqdad <ahmadmiqdad27@gmail.com>
Date: Sun, 31 Aug 2025 12:18:14 +0700
Subject: <Miqdad> oke

---
 src/lib/transaction/components/Transaction.jsx | 64 +++++++++++++++++---------
 1 file changed, 43 insertions(+), 21 deletions(-)

(limited to 'src/lib/transaction/components/Transaction.jsx')

diff --git a/src/lib/transaction/components/Transaction.jsx b/src/lib/transaction/components/Transaction.jsx
index 803b60bd..30ba74d2 100644
--- a/src/lib/transaction/components/Transaction.jsx
+++ b/src/lib/transaction/components/Transaction.jsx
@@ -209,37 +209,59 @@ const Transaction = ({ id }) => {
     transaction.refetch();
   };
 
+  // ===== Bayar Sekarang (pakai link dari backend; fallback generate via Next API) =====
   const handlePayNow = async () => {
     try {
       setContLoading(true);
 
-      // Selalu minta link baru ke server
-      const res = await axios.post(
-        '/api/shop/midtrans-payment', // pakai relative path biar nggak salah host
-        null,
+      const base = (process.env.NEXT_PUBLIC_ODOO_API_HOST || '').replace(
+        /\/$/,
+        ''
+      );
+      const token = auth?.token;
+      const partnerId = auth?.partnerId;
+
+      // 1) Minta Odoo ensure (hanya generate jika kosong / expire / cancel)
+      const { data: resp } = await axios.get(
+        `${base}/api/v1/partner/${partnerId}/sale_order/${transaction.data.id}`,
         {
-          params: {
-            transactionId: transaction.data.id,
-            force: 1,
-            ts: Date.now(),
-          },
+          params: { ensure_payment_link: 1, ts: Date.now() },
+          headers: { Token: token },
         }
       );
 
-      const ensuredUrl = res?.data?.redirectUrl || '';
-      if (!ensuredUrl) {
-        const fallback =
-          transaction?.data?.paymentSummary?.redirectUrl ||
-          transaction?.data?.paymentLinkMidtrans ||
-          '';
-        if (fallback) {
-          window.open(fallback, '_blank', 'noopener,noreferrer');
-          return;
-        }
-        throw new Error('redirectUrl kosong');
+      const payload = resp?.data || resp;
+
+      // 2) Ambil URL dari berbagai kemungkinan field (snake_case dan camelCase)
+      let url =
+        payload?.payment_summary?.redirect_url ||
+        payload?.paymentSummary?.redirectUrl ||
+        payload?.paymentLinkMidtrans ||
+        payload?.payment_link_midtrans ||
+        '';
+
+      if (url) {
+        window.location.href = url;
+        return;
+      }
+
+      // 3) Fallback: refetch detail dari hook lalu cek lagi
+      await transaction.refetch();
+
+      url =
+        transaction?.data?.paymentSummary?.redirectUrl ||
+        transaction?.data?.payment_summary?.redirect_url ||
+        transaction?.data?.paymentLinkMidtrans ||
+        transaction?.data?.payment_link_midtrans ||
+        '';
+
+      if (url) {
+        window.location.href = url;
+        return;
       }
 
-      window.location.href = ensuredUrl; // pakai link BARU
+      // 4) Masih kosong → error
+      throw new Error('Link pembayaran belum tersedia.');
     } catch (e) {
       toast.error(
         e?.response?.data?.description ||
-- 
cgit v1.2.3

