@@ -45,6 +45,19 @@ exports.handler = async (event, context) => {
4545
4646 const { email, walletAddress, tier = 'free' , turnstileToken } = body ;
4747
48+ // Validate inputs
49+ const validationError = validateParams ( { email, walletAddress } , [ 'email' , 'walletAddress' ] ) ;
50+ if ( validationError ) {
51+ return formatResponse (
52+ validationError . statusCode ,
53+ {
54+ error : validationError . error ,
55+ message : validationError . error
56+ } ,
57+ process . env . CORS_ORIGIN
58+ ) ;
59+ }
60+
4861 // Validate Turnstile token for free tier
4962 if ( tier . toLowerCase ( ) === 'free' ) {
5063 if ( ! turnstileToken ) {
@@ -83,19 +96,6 @@ exports.handler = async (event, context) => {
8396 } ) ;
8497 }
8598
86- // Validate inputs
87- const validationError = validateParams ( { email, walletAddress } , [ 'email' , 'walletAddress' ] ) ;
88- if ( validationError ) {
89- return formatResponse (
90- validationError . statusCode ,
91- {
92- error : validationError . error ,
93- message : validationError . error
94- } ,
95- process . env . CORS_ORIGIN
96- ) ;
97- }
98-
9999 if ( ! validateEmail ( email ) ) {
100100 return formatResponse (
101101 400 ,
@@ -128,30 +128,53 @@ exports.handler = async (event, context) => {
128128 ) ;
129129 }
130130
131- // For Free tier, create and send API key immediately
132- const clientIP = getClientIP ( event ) ;
133- const user = await createUserWithApiKey ( normalizedEmail , normalizedWallet , 'free' , clientIP ) ;
131+ if ( tier . toLowerCase ( ) === 'premium' ) {
132+ // For Premium tier, redirect to payment flow
133+ const clientIP = getClientIP ( event ) ;
134+
135+ // Log premium tier signup initiation
136+ console . log ( 'Premium tier signup initiated:' , {
137+ email : normalizedEmail ,
138+ walletAddress : normalizedWallet ,
139+ tier : 'premium' ,
140+ clientIP : clientIP
141+ } ) ;
134142
135- // Log successful user account creation
136- console . log ( 'User account created successfully:' , {
137- email : normalizedEmail ,
138- walletAddress : normalizedWallet ,
139- tier : 'free' ,
140- clientIP : clientIP
141- } ) ;
143+ return formatResponse (
144+ 200 ,
145+ {
146+ message : 'Redirect to payment flow' ,
147+ redirectUrl : `${ process . env . PAYMENT_URL } ?email=${ encodeURIComponent ( normalizedEmail ) } &wallet=${ encodeURIComponent ( normalizedWallet ) } `
148+ } ,
149+ process . env . CORS_ORIGIN
150+ ) ;
151+ }
152+ else {
153+ // For Free tier, create and send API key immediately
154+ const clientIP = getClientIP ( event ) ;
155+ const user = await createUserWithApiKey ( normalizedEmail , normalizedWallet , 'free' , clientIP ) ;
156+
157+ // Log successful user account creation
158+ console . log ( 'User account created successfully:' , {
159+ email : normalizedEmail ,
160+ walletAddress : normalizedWallet ,
161+ tier : 'free' ,
162+ clientIP : clientIP
163+ } ) ;
142164
143- // Send welcome email with API key
144- await sendWelcomeEmail ( normalizedEmail , user . apiKey ) ;
165+ // Send welcome email with API key
166+ await sendWelcomeEmail ( normalizedEmail , user . apiKey ) ;
145167
146- // Return success response
147- return formatResponse (
148- 200 ,
149- {
150- message : 'API key created successfully' ,
151- apiKey : user . apiKey
152- } ,
153- process . env . CORS_ORIGIN
154- ) ;
168+ // Return success response
169+ return formatResponse (
170+ 200 ,
171+ {
172+ message : 'API key created successfully' ,
173+ apiKey : user . apiKey
174+ } ,
175+ process . env . CORS_ORIGIN
176+ ) ;
177+ }
155178 }
156179 catch ( error ) {
157180 console . error ( 'Error processing signup:' , error ) ;
0 commit comments