1+ // Fonction API unifiée pour toutes les opérations Stripe
2+ export default async function handler ( req , res ) {
3+ const { method } = req ;
4+ const { action, setupIntentId } = req . query ;
5+
6+ if ( ! [ 'POST' , 'GET' ] . includes ( method ) ) {
7+ return res . status ( 405 ) . json ( { error : 'Method not allowed' } ) ;
8+ }
9+
10+ try {
11+ const firebaseBaseUrl = 'https://us-central1-refspring-project.cloudfunctions.net' ;
12+
13+ // Router vers la bonne fonction Firebase selon l'action
14+ const endpointMap = {
15+ 'check-setup' : `${ firebaseBaseUrl } /stripeCheckSetup` ,
16+ 'connect-webhook' : `${ firebaseBaseUrl } /stripeConnectWebhook` ,
17+ 'create-account-link' : `${ firebaseBaseUrl } /stripeCreateAccountLink` ,
18+ 'create-connect-account' : `${ firebaseBaseUrl } /stripeCreateConnectAccount` ,
19+ 'create-invoice' : `${ firebaseBaseUrl } /stripeCreateInvoice` ,
20+ 'create-payment-link' : `${ firebaseBaseUrl } /stripeCreatePaymentLink` ,
21+ 'create-setup' : `${ firebaseBaseUrl } /stripeCreateSetup` ,
22+ 'create-transfer' : `${ firebaseBaseUrl } /stripeCreateTransfer` ,
23+ 'delete-payment-method' : `${ firebaseBaseUrl } /stripeDeletePaymentMethod` ,
24+ 'get-payment-methods' : `${ firebaseBaseUrl } /stripeGetPaymentMethods` ,
25+ 'set-default-payment-method' : `${ firebaseBaseUrl } /stripeSetDefaultPaymentMethod`
26+ } ;
27+
28+ const endpoint = endpointMap [ action ] ;
29+ if ( ! endpoint ) {
30+ return res . status ( 400 ) . json ( { error : 'Invalid action' } ) ;
31+ }
32+
33+ let url = endpoint ;
34+ let fetchOptions = {
35+ method : method ,
36+ headers : {
37+ 'Content-Type' : 'application/json' ,
38+ }
39+ } ;
40+
41+ // Pour GET avec paramètres (comme check-setup)
42+ if ( method === 'GET' && setupIntentId ) {
43+ url += `?setupIntentId=${ encodeURIComponent ( setupIntentId ) } ` ;
44+ } else if ( method === 'POST' ) {
45+ fetchOptions . body = JSON . stringify ( req . body ) ;
46+ }
47+
48+ const response = await fetch ( url , fetchOptions ) ;
49+ const data = await response . json ( ) ;
50+
51+ if ( ! response . ok ) {
52+ return res . status ( response . status ) . json ( data ) ;
53+ }
54+
55+ res . json ( data ) ;
56+ } catch ( error ) {
57+ console . error ( 'Stripe API error:' , error ) ;
58+ res . status ( 500 ) . json ( { error : 'Internal server error' } ) ;
59+ }
60+ }
0 commit comments