From 308563b3f45255450206decc03329dcc64c4bc50 Mon Sep 17 00:00:00 2001
From: Miqdad <ahmadmiqdad27@gmail.com>
Date: Fri, 29 Aug 2025 10:42:05 +0700
Subject: <MIqdad> Fix payment

---
 src/lib/transaction/components/Transaction.jsx | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 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 cc5f862c..0a399f12 100644
--- a/src/lib/transaction/components/Transaction.jsx
+++ b/src/lib/transaction/components/Transaction.jsx
@@ -213,17 +213,32 @@ const Transaction = ({ id }) => {
   const handlePayNow = async () => {
     try {
       setContLoading(true);
-      // 1) gunakan URL yang sudah dikirim backend (jika ada)
+
+      // (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 ||
         '';
+
       if (existingUrl) {
         window.open(existingUrl, '_blank', 'noopener,noreferrer');
         toast.success('Membuka halaman pembayaran…');
         return;
       }
-      // 2) fallback: panggil Next API untuk generate
+
+      // (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}`
       );
-- 
cgit v1.2.3

