From 67731281227f961c9b56f9f310951a8e5c82ab39 Mon Sep 17 00:00:00 2001
From: trisusilo48 <tri.susilo@altama.co.id>
Date: Fri, 23 Aug 2024 14:19:05 +0700
Subject: bug fixing checkout ios env

---
 src/lib/checkout/components/Checkout.jsx | 40 ++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 12 deletions(-)

(limited to 'src/lib/checkout/components/Checkout.jsx')

diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx
index 09a791ee..47f148d1 100644
--- a/src/lib/checkout/components/Checkout.jsx
+++ b/src/lib/checkout/components/Checkout.jsx
@@ -389,12 +389,38 @@ const Checkout = () => {
     if (typeof file !== 'undefined') data.po_file = await getFileBase64(file);
 
     const isCheckouted = await checkoutApi({ data });
+
     if (!isCheckouted?.id) {
       toast.error('Gagal melakukan transaksi, terjadi kesalahan internal');
       return;
-    }
+    } else {
+      gtagPurchase(products, biayaKirim, isCheckouted.name);
+
+      gtag('event', 'conversion', {
+        send_to: 'AW-954540379/nDymCL3BhaQYENvClMcD',
+        value:
+          cartCheckout?.grandTotal +
+          Math.round(parseInt(biayaKirim * 1.1) / 1000) * 1000,
+        currency: 'IDR',
+        transaction_id: isCheckouted.id,
+      });
 
-    gtagPurchase(products, biayaKirim, isCheckouted.name);
+      for (const product of products) deleteItemCart({ productId: product.id });
+      if (grandTotal > 0) {
+        const payment = await axios.post(
+          `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/midtrans-payment?transactionId=${isCheckouted.id}`
+        );
+        setIsLoading(false);
+        window.location.href = payment.data.redirectUrl;
+      } else {
+        window.location.href = `${
+          process.env.NEXT_PUBLIC_SELF_HOST
+        }/shop/checkout/success?order_id=${isCheckouted.name.replace(
+          /\//g,
+          '-'
+        )}`;
+      }
+    }
 
     const midtrans = async () => {
       for (const product of products) deleteItemCart({ productId: product.id });
@@ -413,16 +439,6 @@ const Checkout = () => {
         )}`;
       }
     };
-
-    gtag('event', 'conversion', {
-      send_to: 'AW-954540379/nDymCL3BhaQYENvClMcD',
-      value:
-        cartCheckout?.grandTotal +
-        Math.round(parseInt(biayaKirim * 1.1) / 1000) * 1000,
-      currency: 'IDR',
-      transaction_id: isCheckouted.id,
-      event_callback: midtrans,
-    });
   };
 
   const handlingActivateCode = async () => {
-- 
cgit v1.2.3

