From 90c4cde3981450cf20172fb938cd18c0943a57c6 Mon Sep 17 00:00:00 2001
From: Miqdad <ahmadmiqdad27@gmail.com>
Date: Sat, 30 Aug 2025 08:05:33 +0700
Subject: <Miqdad> temp

---
 src/lib/transaction/components/Transaction.jsx | 51 ++++++++++++--------------
 1 file changed, 24 insertions(+), 27 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 0a399f12..803b60bd 100644
--- a/src/lib/transaction/components/Transaction.jsx
+++ b/src/lib/transaction/components/Transaction.jsx
@@ -209,40 +209,37 @@ const Transaction = ({ id }) => {
     transaction.refetch();
   };
 
-  // ===== Bayar Sekarang (pakai link dari backend; fallback generate via Next API) =====
   const handlePayNow = async () => {
     try {
       setContLoading(true);
 
-      // (0) minta backend cek status & generate link baru jika expired/cancel
-      let ensuredUrl = '';
-      try {
-        const { data } = await axios.post(
-          `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/so-payment-link?soId=${transaction.data.id}`
-        );
-        ensuredUrl = data?.redirectUrl || '';
-      } catch (_) {
-        // biarkan silent; lanjut ke existing/fallback
-      }
-
-      // (1) pakai URL yang paling terjamin dulu
-      const existingUrl =
-        ensuredUrl ||
-        transaction?.data?.paymentSummary?.redirectUrl ||
-        transaction?.data?.paymentLinkMidtrans ||
-        '';
+      // Selalu minta link baru ke server
+      const res = await axios.post(
+        '/api/shop/midtrans-payment', // pakai relative path biar nggak salah host
+        null,
+        {
+          params: {
+            transactionId: transaction.data.id,
+            force: 1,
+            ts: Date.now(),
+          },
+        }
+      );
 
-      if (existingUrl) {
-        window.open(existingUrl, '_blank', 'noopener,noreferrer');
-        toast.success('Membuka halaman pembayaran…');
-        return;
+      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');
       }
 
-      // (2) fallback terakhir: pakai Next API (mis. bikin Snap baru)
-      const res = await axios.post(
-        `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/midtrans-payment?transactionId=${transaction.data.id}`
-      );
-      window.location.href = res.data.redirectUrl;
+      window.location.href = ensuredUrl; // pakai link BARU
     } catch (e) {
       toast.error(
         e?.response?.data?.description ||
-- 
cgit v1.2.3

