@@ -20,28 +20,50 @@ export const paymentMethodService = {
2020 body : { } // POST vide pour déclencher handleGetPaymentMethods
2121 } ) ;
2222
23- if ( error ) {
24- console . error ( '❌ SUPABASE: Erreur récupération méthodes de paiement:' , error ) ;
25- return [ ] ;
23+ if ( ! error && data ?. paymentMethods ) {
24+ console . log ( '✅ SUPABASE: Cartes bancaires chargées:' , data ?. paymentMethods ?. length || 0 ) ;
25+
26+ // Mapper les données pour correspondre à l'interface PaymentMethod
27+ const paymentMethods = data ?. paymentMethods ?. map ( ( pm : any ) => ( {
28+ id : pm . id ,
29+ type : pm . type ,
30+ last4 : pm . card ?. last4 || '' ,
31+ brand : pm . card ?. brand || '' ,
32+ exp_month : pm . card ?. exp_month || 0 ,
33+ exp_year : pm . card ?. exp_year || 0 ,
34+ isDefault : pm . isDefault || false
35+ } ) ) || [ ] ;
36+
37+ return paymentMethods ;
38+ } else {
39+ console . warn ( '⚠️ SUPABASE Edge Function failed, trying fallback API' ) ;
2640 }
27-
28- console . log ( '✅ SUPABASE: Cartes bancaires chargées:' , data ?. paymentMethods ?. length || 0 ) ;
29-
30- // Mapper les données pour correspondre à l'interface PaymentMethod
31- const paymentMethods = data ?. paymentMethods ?. map ( ( pm : any ) => ( {
32- id : pm . id ,
33- type : pm . type ,
34- last4 : pm . card ?. last4 || '' ,
35- brand : pm . card ?. brand || '' ,
36- exp_month : pm . card ?. exp_month || 0 ,
37- exp_year : pm . card ?. exp_year || 0 ,
38- isDefault : pm . isDefault || false
39- } ) ) || [ ] ;
40-
41- return paymentMethods ;
42- } catch ( error ) {
43- console . error ( '❌ SUPABASE: Erreur chargement cartes:' , error ) ;
44- return [ ] ;
41+ } catch ( supabaseError ) {
42+ console . warn ( '⚠️ SUPABASE Edge Function error, trying fallback API:' , supabaseError ) ;
43+ }
44+
45+ // Fallback to old API endpoint if Supabase fails
46+ try {
47+ console . log ( '🔄 FALLBACK: Utilisation API de secours pour les cartes' ) ;
48+ const response = await fetch ( '/api/stripe-payment-methods' , {
49+ method : 'POST' ,
50+ headers : {
51+ 'Content-Type' : 'application/json' ,
52+ } ,
53+ body : JSON . stringify ( { userEmail } ) ,
54+ } ) ;
55+
56+ if ( ! response . ok ) {
57+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
58+ }
59+
60+ const data = await response . json ( ) ;
61+ console . log ( '✅ FALLBACK: Cartes bancaires chargées:' , data ?. paymentMethods ?. length || 0 ) ;
62+
63+ return data ?. paymentMethods || [ ] ;
64+ } catch ( fallbackError ) {
65+ console . error ( '❌ FALLBACK: Erreur API de secours:' , fallbackError ) ;
66+ return [ ] ; // Return empty array instead of throwing
4567 }
4668 } ,
4769
0 commit comments