11
2- import { functions } from '@/lib/firebase' ;
3- import { httpsCallable } from 'firebase/functions' ;
4-
52export interface PaymentMethod {
63 id : string ;
74 type : string ;
@@ -14,45 +11,72 @@ export interface PaymentMethod {
1411
1512export const paymentMethodService = {
1613 async getPaymentMethods ( userEmail : string ) : Promise < PaymentMethod [ ] > {
17- console . log ( '🔍 FIREBASE : Chargement des cartes bancaires pour:' , userEmail ) ;
14+ console . log ( '🔍 API : Chargement des cartes bancaires pour:' , userEmail ) ;
1815
1916 try {
20- const getPaymentMethods = httpsCallable ( functions , 'stripeGetPaymentMethods' ) ;
21- const result = await getPaymentMethods ( { userEmail } ) ;
22- const data = result . data as { paymentMethods ?: PaymentMethod [ ] } ;
17+ const response = await fetch ( '/api/stripe-payment-methods' , {
18+ method : 'POST' ,
19+ headers : {
20+ 'Content-Type' : 'application/json' ,
21+ } ,
22+ body : JSON . stringify ( { userEmail } ) ,
23+ } ) ;
24+
25+ if ( ! response . ok ) {
26+ throw new Error ( 'Failed to fetch payment methods' ) ;
27+ }
2328
24- console . log ( '✅ FIREBASE: Cartes bancaires chargées:' , data . paymentMethods ?. length || 0 ) ;
29+ const data = await response . json ( ) ;
30+ console . log ( '✅ API: Cartes bancaires chargées:' , data . paymentMethods ?. length || 0 ) ;
2531 return data . paymentMethods || [ ] ;
2632 } catch ( error ) {
27- console . error ( '❌ FIREBASE : Erreur chargement cartes:' , error ) ;
33+ console . error ( '❌ API : Erreur chargement cartes:' , error ) ;
2834 return [ ] ;
2935 }
3036 } ,
3137
3238 async deletePaymentMethod ( paymentMethodId : string ) : Promise < void > {
33- console . log ( `🗑️ FIREBASE : Suppression de la carte ${ paymentMethodId } ` ) ;
39+ console . log ( `🗑️ API : Suppression de la carte ${ paymentMethodId } ` ) ;
3440
3541 try {
36- const deletePaymentMethod = httpsCallable ( functions , 'stripeDeletePaymentMethod' ) ;
37- await deletePaymentMethod ( { paymentMethodId } ) ;
42+ const response = await fetch ( '/api/stripe-delete-payment-method' , {
43+ method : 'POST' ,
44+ headers : {
45+ 'Content-Type' : 'application/json' ,
46+ } ,
47+ body : JSON . stringify ( { paymentMethodId } ) ,
48+ } ) ;
3849
39- console . log ( '✅ FIREBASE: Carte supprimée de Stripe' ) ;
50+ if ( ! response . ok ) {
51+ throw new Error ( 'Failed to delete payment method' ) ;
52+ }
53+
54+ console . log ( '✅ API: Carte supprimée de Stripe' ) ;
4055 } catch ( error ) {
41- console . error ( '❌ FIREBASE : Erreur suppression carte:' , error ) ;
56+ console . error ( '❌ API : Erreur suppression carte:' , error ) ;
4257 throw error ;
4358 }
4459 } ,
4560
4661 async setDefaultPaymentMethod ( userEmail : string , paymentMethodId : string ) : Promise < void > {
47- console . log ( `⭐ FIREBASE : Définition de la carte par défaut ${ paymentMethodId } pour ${ userEmail } ` ) ;
62+ console . log ( `⭐ API : Définition de la carte par défaut ${ paymentMethodId } pour ${ userEmail } ` ) ;
4863
4964 try {
50- const setDefaultPaymentMethod = httpsCallable ( functions , 'stripeSetDefaultPaymentMethod' ) ;
51- await setDefaultPaymentMethod ( { userEmail, paymentMethodId } ) ;
65+ const response = await fetch ( '/api/stripe-set-default-payment-method' , {
66+ method : 'POST' ,
67+ headers : {
68+ 'Content-Type' : 'application/json' ,
69+ } ,
70+ body : JSON . stringify ( { userEmail, paymentMethodId } ) ,
71+ } ) ;
72+
73+ if ( ! response . ok ) {
74+ throw new Error ( 'Failed to set default payment method' ) ;
75+ }
5276
53- console . log ( '✅ FIREBASE : Carte par défaut mise à jour' ) ;
77+ console . log ( '✅ API : Carte par défaut mise à jour' ) ;
5478 } catch ( error ) {
55- console . error ( '❌ FIREBASE : Erreur définition carte par défaut:' , error ) ;
79+ console . error ( '❌ API : Erreur définition carte par défaut:' , error ) ;
5680 throw error ;
5781 }
5882 }
0 commit comments