1+ import * as functions from 'firebase-functions' ;
2+ import { stripe } from './stripeConfig' ;
3+
4+ export const stripeCreateConnectAccount = functions . https . onRequest ( async ( req , res ) => {
5+ res . set ( 'Access-Control-Allow-Origin' , '*' ) ;
6+ res . set ( 'Access-Control-Allow-Methods' , 'POST, OPTIONS' ) ;
7+ res . set ( 'Access-Control-Allow-Headers' , 'Content-Type' ) ;
8+
9+ if ( req . method === 'OPTIONS' ) {
10+ return res . status ( 200 ) . end ( ) ;
11+ }
12+
13+ if ( req . method !== 'POST' ) {
14+ return res . status ( 405 ) . json ( { error : 'Method not allowed' } ) ;
15+ }
16+
17+ try {
18+ const { affiliateEmail, affiliateName, country = 'FR' } = req . body ;
19+
20+ if ( ! affiliateEmail || ! affiliateName ) {
21+ return res . status ( 400 ) . json ( { error : 'affiliateEmail and affiliateName are required' } ) ;
22+ }
23+
24+ console . log ( '🔗 Creating Stripe Connect account for:' , affiliateEmail ) ;
25+
26+ const account = await stripe . accounts . create ( {
27+ type : 'express' ,
28+ country : country ,
29+ email : affiliateEmail ,
30+ capabilities : {
31+ card_payments : { requested : true } ,
32+ transfers : { requested : true } ,
33+ } ,
34+ metadata : {
35+ affiliateEmail,
36+ affiliateName,
37+ } ,
38+ } ) ;
39+
40+ console . log ( '✅ Stripe Connect account created:' , account . id ) ;
41+
42+ return res . json ( {
43+ accountId : account . id ,
44+ email : affiliateEmail ,
45+ name : affiliateName ,
46+ country : account . country ,
47+ created : account . created ,
48+ } ) ;
49+
50+ } catch ( error ) {
51+ console . error ( '❌ Error creating Stripe Connect account:' , error ) ;
52+ return res . status ( 500 ) . json ( {
53+ error : 'Failed to create Stripe Connect account' ,
54+ details : error instanceof Error ? error . message : 'Unknown error'
55+ } ) ;
56+ }
57+ } ) ;
58+
59+ export const stripeCreateAccountLink = functions . https . onRequest ( async ( req , res ) => {
60+ res . set ( 'Access-Control-Allow-Origin' , '*' ) ;
61+ res . set ( 'Access-Control-Allow-Methods' , 'POST, OPTIONS' ) ;
62+ res . set ( 'Access-Control-Allow-Headers' , 'Content-Type' ) ;
63+
64+ if ( req . method === 'OPTIONS' ) {
65+ return res . status ( 200 ) . end ( ) ;
66+ }
67+
68+ if ( req . method !== 'POST' ) {
69+ return res . status ( 405 ) . json ( { error : 'Method not allowed' } ) ;
70+ }
71+
72+ try {
73+ const { accountId, affiliateId, refreshUrl, returnUrl } = req . body ;
74+
75+ if ( ! accountId ) {
76+ return res . status ( 400 ) . json ( { error : 'accountId is required' } ) ;
77+ }
78+
79+ console . log ( '🔗 Creating account link for:' , accountId ) ;
80+
81+ const origin = req . headers . origin || 'http://localhost:5173' ;
82+
83+ const accountLink = await stripe . accountLinks . create ( {
84+ account : accountId ,
85+ refresh_url : refreshUrl || `${ origin } /affiliate-onboarding?refresh=true&account=${ accountId } ` ,
86+ return_url : returnUrl || `${ origin } /affiliate-onboarding?success=true&account=${ accountId } ` ,
87+ type : 'account_onboarding' ,
88+ } ) ;
89+
90+ console . log ( '✅ Account link created:' , accountLink . url ) ;
91+
92+ return res . json ( {
93+ url : accountLink . url ,
94+ expires_at : accountLink . expires_at ,
95+ } ) ;
96+
97+ } catch ( error ) {
98+ console . error ( '❌ Error creating account link:' , error ) ;
99+ return res . status ( 500 ) . json ( {
100+ error : 'Failed to create account link' ,
101+ details : error instanceof Error ? error . message : 'Unknown error'
102+ } ) ;
103+ }
104+ } ) ;
105+
106+ export const stripeCreateTransfer = functions . https . onRequest ( async ( req , res ) => {
107+ res . set ( 'Access-Control-Allow-Origin' , '*' ) ;
108+ res . set ( 'Access-Control-Allow-Methods' , 'POST, OPTIONS' ) ;
109+ res . set ( 'Access-Control-Allow-Headers' , 'Content-Type' ) ;
110+
111+ if ( req . method === 'OPTIONS' ) {
112+ return res . status ( 200 ) . end ( ) ;
113+ }
114+
115+ if ( req . method !== 'POST' ) {
116+ return res . status ( 405 ) . json ( { error : 'Method not allowed' } ) ;
117+ }
118+
119+ try {
120+ const { accountId, amount, description, metadata } = req . body ;
121+
122+ if ( ! accountId || ! amount ) {
123+ return res . status ( 400 ) . json ( { error : 'accountId and amount are required' } ) ;
124+ }
125+
126+ console . log ( '💸 Creating transfer:' , { accountId, amount } ) ;
127+
128+ const transfer = await stripe . transfers . create ( {
129+ amount : Math . round ( amount ) , // Amount in cents
130+ currency : 'eur' ,
131+ destination : accountId ,
132+ description : description || 'Commission payment' ,
133+ metadata : metadata || { } ,
134+ } ) ;
135+
136+ console . log ( '✅ Transfer created:' , transfer . id ) ;
137+
138+ return res . json ( {
139+ transferId : transfer . id ,
140+ amount : transfer . amount ,
141+ currency : transfer . currency ,
142+ destination : transfer . destination ,
143+ created : transfer . created ,
144+ } ) ;
145+
146+ } catch ( error ) {
147+ console . error ( '❌ Error creating transfer:' , error ) ;
148+ return res . status ( 500 ) . json ( {
149+ error : 'Failed to create transfer' ,
150+ details : error instanceof Error ? error . message : 'Unknown error'
151+ } ) ;
152+ }
153+ } ) ;
0 commit comments