fix(checkout): prevent payment completion on Razorpay modal cancel (#6)
- Add modal.confirm_close: true to prompt confirmation and abort in-flight payments when user clicks Cancel (X) - Implement modal.ondismiss to track cancellation and reset checkout loading state - Add isPaymentCancelled guard in Razorpay handler callback to prevent unintended payment verification after dismissal - Handle loading state cleanup on payment.failed event
This commit is contained in:
parent
776e09a918
commit
9847d773f2
22
src/App.tsx
22
src/App.tsx
|
|
@ -621,6 +621,9 @@ export default function App() {
|
|||
}
|
||||
}
|
||||
|
||||
let isPaymentCancelled = false;
|
||||
let isPaymentCompleted = false;
|
||||
|
||||
const rzpOptions = {
|
||||
key: razorpayKeyId,
|
||||
amount: Math.round(grand_total * 100),
|
||||
|
|
@ -634,7 +637,25 @@ export default function App() {
|
|||
email: loggedInUser?.email || '',
|
||||
contact: loggedInUser?.phone_number || loggedInUser?.phone || ''
|
||||
},
|
||||
modal: {
|
||||
confirm_close: true,
|
||||
escape: true,
|
||||
backdropclose: false,
|
||||
ondismiss: function () {
|
||||
if (!isPaymentCompleted) {
|
||||
isPaymentCancelled = true;
|
||||
setCheckoutLoading(false);
|
||||
console.log('Razorpay payment modal dismissed / cancelled by user.');
|
||||
}
|
||||
}
|
||||
},
|
||||
handler: async function (response: any) {
|
||||
if (isPaymentCancelled) {
|
||||
console.warn('Payment was cancelled by the user. Verification ignored.');
|
||||
setCheckoutLoading(false);
|
||||
return;
|
||||
}
|
||||
isPaymentCompleted = true;
|
||||
setCheckoutLoading(true);
|
||||
try {
|
||||
const verifyRes = await axios.post(`${API_BASE}/api/checkout/verify`, {
|
||||
|
|
@ -683,6 +704,7 @@ export default function App() {
|
|||
|
||||
const razorpayInstance = new (window as any).Razorpay(rzpOptions);
|
||||
razorpayInstance.on('payment.failed', function (resp: any) {
|
||||
setCheckoutLoading(false);
|
||||
alert(`Payment failed! Reason: ${resp.error.description}`);
|
||||
});
|
||||
razorpayInstance.open();
|
||||
|
|
|
|||
Loading…
Reference in New Issue