From 1f1393be30c7fabb31f1fe80be3f4731009b8cf5 Mon Sep 17 00:00:00 2001 From: CIFO Dev Date: Thu, 4 Dec 2025 10:39:54 +0700 Subject: [PATCH] Fix auto-payment trigger: wait for valid orderId before triggering Snap - Add guard in AutoSnapPayment to only trigger when orderId and amount are valid - Show loading state while waiting for payment data to resolve - Prevent premature Snap token creation with empty/invalid data - Fix PayPage and CheckoutPage auto-payment flow --- src/pages/CheckoutPage.tsx | 18 ++++++++++++++---- src/pages/PayPage.tsx | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/pages/CheckoutPage.tsx b/src/pages/CheckoutPage.tsx index 8788a23..0f72ed5 100644 --- a/src/pages/CheckoutPage.tsx +++ b/src/pages/CheckoutPage.tsx @@ -6,9 +6,6 @@ import { PaymentMethodList } from '../features/payments/components/PaymentMethod import type { PaymentMethod } from '../features/payments/components/PaymentMethodList' import { SnapPaymentTrigger } from '../features/payments/snap/SnapPaymentTrigger' import { usePaymentConfig } from '../features/payments/lib/usePaymentConfig' -import { Logger } from '../lib/logger' -import React from 'react' - interface AutoSnapPaymentProps { orderId: string amount: number @@ -24,7 +21,8 @@ function AutoSnapPayment({ orderId, amount, customer, onChargeInitiated, onSucce const hasTriggered = React.useRef(false) React.useEffect(() => { - if (hasTriggered.current) return + // Only trigger when we have valid orderId and amount + if (!orderId || !amount || hasTriggered.current) return hasTriggered.current = true const triggerPayment = async () => { @@ -97,6 +95,18 @@ function AutoSnapPayment({ orderId, amount, customer, onChargeInitiated, onSucce return () => clearTimeout(timer) }, [orderId, amount, customer, onChargeInitiated, onSuccess, onError]) + // Don't render anything until we have valid data + if (!orderId || !amount) { + return ( +
+
+
+

Memuat data pembayaran...

+
+
+ ) + } + return (
{error && ( diff --git a/src/pages/PayPage.tsx b/src/pages/PayPage.tsx index 09c6f00..1f0ffba 100644 --- a/src/pages/PayPage.tsx +++ b/src/pages/PayPage.tsx @@ -27,7 +27,8 @@ function AutoSnapPayment({ orderId, amount, customer, onChargeInitiated, onSucce const hasTriggered = React.useRef(false) React.useEffect(() => { - if (hasTriggered.current) return + // Only trigger when we have valid orderId and amount + if (!orderId || !amount || hasTriggered.current) return hasTriggered.current = true const triggerPayment = async () => { @@ -100,6 +101,18 @@ function AutoSnapPayment({ orderId, amount, customer, onChargeInitiated, onSucce return () => clearTimeout(timer) }, [orderId, amount, customer, onChargeInitiated, onSuccess, onError]) + // Don't render anything until we have valid data + if (!orderId || !amount) { + return ( +
+
+
+

Memuat data pembayaran...

+
+
+ ) + } + return (
{error && (