@@ -36,13 +36,23 @@ const Checkout = ({ cartItems, onClose, onOrderPlaced }) => {
3636 const loaded = await loadRazorpayScript ( ) ;
3737 if ( ! loaded ) { alert ( 'Razorpay failed to load. Check your internet.' ) ; return ; }
3838
39+ const apiBase = import . meta. env . VITE_API_URL || '' ;
40+
3941 try {
40- const res = await fetch ( `${ import . meta . env . VITE_API_URL } /api/payment/create-order` , {
42+ const res = await fetch ( `${ apiBase } /api/payment/create-order` , {
4143 method : 'POST' ,
4244 headers : { 'Content-Type' : 'application/json' } ,
4345 body : JSON . stringify ( { amount : totalAmount } ) ,
4446 } ) ;
45- const data = await res . json ( ) ;
47+
48+ if ( ! res . ok ) {
49+ const errText = await res . text ( ) ;
50+ throw new Error ( `Server error ${ res . status } : ${ errText || 'No response from server' } ` ) ;
51+ }
52+
53+ const text = await res . text ( ) ;
54+ if ( ! text ) throw new Error ( 'Empty response from payment server. Make sure the backend is running and VITE_API_URL is set correctly.' ) ;
55+ const data = JSON . parse ( text ) ;
4656
4757 const options = {
4858 key : import . meta. env . VITE_RAZORPAY_KEY_ID ,
@@ -52,12 +62,14 @@ const Checkout = ({ cartItems, onClose, onOrderPlaced }) => {
5262 description : 'Shoe Purchase' ,
5363 order_id : data . orderId ,
5464 handler : async ( response ) => {
55- const verifyRes = await fetch ( `${ import . meta . env . VITE_API_URL } /api/payment/verify` , {
65+ const verifyRes = await fetch ( `${ apiBase } /api/payment/verify` , {
5666 method : 'POST' ,
5767 headers : { 'Content-Type' : 'application/json' } ,
5868 body : JSON . stringify ( response ) ,
5969 } ) ;
60- const verifyData = await verifyRes . json ( ) ;
70+ const verifyText = await verifyRes . text ( ) ;
71+ if ( ! verifyText ) { alert ( 'Payment verification failed: empty response from server.' ) ; return ; }
72+ const verifyData = JSON . parse ( verifyText ) ;
6173 if ( verifyData . success ) {
6274 if ( onOrderPlaced ) onOrderPlaced ( cartItems ) ;
6375 setOrderPlaced ( true ) ;
0 commit comments