Skip to content

Commit 40268a0

Browse files
committed
fix: handle empty/failed response for UPI and card payment
1 parent 2a16cd5 commit 40268a0

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

frontend/src/components/Checkout.jsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ const Checkout = ({ cartItems, onClose, onOrderPlaced }) => {
4242
headers: { 'Content-Type': 'application/json' },
4343
body: JSON.stringify({ amount: totalAmount }),
4444
});
45-
const data = await res.json();
45+
46+
if (!res.ok) {
47+
const errText = await res.text();
48+
throw new Error(`Server error ${res.status}: ${errText || 'No response from server'}`);
49+
}
50+
51+
const text = await res.text();
52+
if (!text) throw new Error('Empty response from payment server. Make sure the backend is running and VITE_API_URL is set correctly.');
53+
const data = JSON.parse(text);
4654

4755
const options = {
4856
key: import.meta.env.VITE_RAZORPAY_KEY_ID,
@@ -57,7 +65,9 @@ const Checkout = ({ cartItems, onClose, onOrderPlaced }) => {
5765
headers: { 'Content-Type': 'application/json' },
5866
body: JSON.stringify(response),
5967
});
60-
const verifyData = await verifyRes.json();
68+
const verifyText = await verifyRes.text();
69+
if (!verifyText) { alert('Payment verification failed: empty response from server.'); return; }
70+
const verifyData = JSON.parse(verifyText);
6171
if (verifyData.success) {
6272
if (onOrderPlaced) onOrderPlaced(cartItems);
6373
setOrderPlaced(true);

0 commit comments

Comments
 (0)