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/Transactions.jsx | 30 ++++++++++++++-----------
 1 file changed, 17 insertions(+), 13 deletions(-)

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

diff --git a/src/lib/transaction/components/Transactions.jsx b/src/lib/transaction/components/Transactions.jsx
index 5466466c..064471d5 100644
--- a/src/lib/transaction/components/Transactions.jsx
+++ b/src/lib/transaction/components/Transactions.jsx
@@ -40,7 +40,7 @@ import { Calendar } from 'lucide-react';
 import { DateRangePicker } from 'react-date-range';
 import 'react-date-range/dist/styles.css';
 import 'react-date-range/dist/theme/default.css';
-
+import axios from 'axios';
 
 const Transactions = ({ context = '' }) => {
   const auth = useAuth();
@@ -58,7 +58,7 @@ const Transactions = ({ context = '' }) => {
   const { setRefreshCart } = useProductCartContext();
 
   const [cachedAllData, setCachedAllData] = useState(null); // Simpan data "All"
-  const [currentData, setCurrentData] = useState([]); 
+  const [currentData, setCurrentData] = useState([]);
   const [inputQuery, setInputQuery] = useState(q);
   const [toOthers, setToOthers] = useState(null);
   const [toCancel, setToCancel] = useState(null);
@@ -397,39 +397,43 @@ const Transactions = ({ context = '' }) => {
     try {
       setContLoadingId(saleOrder.id);
 
-      // (0) Minta backend pastikan/generate link terbaru
-      let ensuredUrl = '';
+      // 1) Selalu coba generate link baru via Next API dulu
+      const base = (process.env.NEXT_PUBLIC_SELF_HOST || '').replace(/\/$/, '');
       try {
         const { data } = await axios.post(
-          `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/so-payment-link?soId=${saleOrder.id}`
+          `${base}/api/shop/midtrans-payment`,
+          null,
+          { params: { transactionId: saleOrder.id } }
         );
-        ensuredUrl = data?.redirectUrl || '';
-      } catch (e) {
+        if (data?.redirectUrl) {
+          window.open(data.redirectUrl, '_blank', 'noopener,noreferrer');
+          toast.success('Membuka halaman pembayaran…');
+          return;
+        }
+      } catch (err) {
+        // biarkan fail, lanjut fallback
       }
 
+      // 2) Fallback: pakai link yang sudah ada di list (mungkin masih valid)
       const existingUrl =
-        ensuredUrl ||
         saleOrder?.paymentSummary?.redirectUrl ||
         saleOrder?.paymentLinkMidtrans ||
         '';
-
       if (existingUrl) {
         window.open(existingUrl, '_blank', 'noopener,noreferrer');
-        toast.success('Membuka halaman pembayaran…');
+        toast('Membuka link pembayaran yang sudah ada');
         return;
       }
 
-      // (2) Fallback: refetch list (kalau backend auto-generate saat list)
+      // 3) Fallback terakhir: refetch list
       await transactions.refetch();
       const updated =
         transactions?.data?.saleOrders?.find((s) => s.id === saleOrder.id) ||
         null;
       const updatedUrl =
-        ensuredUrl || // kalau step (0) akhirnya sukses
         updated?.paymentSummary?.redirectUrl ||
         updated?.paymentLinkMidtrans ||
         '';
-
       if (updatedUrl) {
         window.open(updatedUrl, '_blank', 'noopener,noreferrer');
         toast.success('Membuka halaman pembayaran…');
-- 
cgit v1.2.3

