@@ -13,6 +13,11 @@ import { AuthenticationError, ValidationError } from "@packages/error-handler";
1313import bcrypt from "bcryptjs" ;
1414import jwt from "jsonwebtoken" ;
1515import { setCookie } from "../utils/cookies/setCookie" ;
16+ import { Stripe } from "stripe" ;
17+
18+ const stripe = new Stripe ( process . env . STRIPE_SECRET_KEY ! , {
19+ apiVersion : "2025-12-15.clover" ,
20+ } ) ;
1621
1722//new user registration
1823export const userRegistration = async (
@@ -364,3 +369,126 @@ export const createShop = async (
364369 return next ( error ) ;
365370 }
366371} ;
372+
373+ //create a stripe connect link
374+
375+ export const createStripeConnectLink = async (
376+ req : Request ,
377+ res : Response ,
378+ next : NextFunction
379+ ) => {
380+ try {
381+ const { sellerId } = req . body ;
382+ console . log ( req . body , "req.body" ) ;
383+ if ( ! sellerId ) {
384+ return next ( new ValidationError ( "Seller ID is required" ) ) ;
385+ }
386+ const seller = await prisma . sellers . findUnique ( {
387+ where : { id : sellerId } ,
388+ } ) ;
389+ if ( ! seller ) {
390+ return next ( new ValidationError ( "Seller not found" ) ) ;
391+ }
392+
393+ //create stripe connect account
394+ const account = await stripe . accounts . create ( {
395+ type : "express" ,
396+ country : "IN" ,
397+ email : seller . email ,
398+ business_type : "individual" ,
399+ capabilities : {
400+ card_payments : { requested : true } ,
401+ transfers : { requested : true } ,
402+ } ,
403+ } ) ;
404+ await prisma . sellers . update ( {
405+ where : { id : sellerId } ,
406+ data : { stripeId : account . id } ,
407+ } ) ;
408+
409+ const accountLink = await stripe . accountLinks . create ( {
410+ account : account . id ,
411+ refresh_url : `http://localhost:3000/success` ,
412+ return_url : `http://localhost:3000/success` ,
413+ type : "account_onboarding" ,
414+ } ) ;
415+
416+ res . status ( 200 ) . json ( {
417+ success : true ,
418+ url : accountLink . url ,
419+ } ) ;
420+ } catch ( error ) {
421+ return next ( error ) ;
422+ }
423+ } ;
424+
425+ //Seller Login
426+
427+ export const loginSeller = async (
428+ req : Request ,
429+ res : Response ,
430+ next : NextFunction
431+ ) => {
432+ try {
433+ const { email, password } = req . body ;
434+ if ( ! email || ! password ) {
435+ return next ( new ValidationError ( "Email and password are required" ) ) ;
436+ }
437+ const seller = await prisma . sellers . findUnique ( {
438+ where : { email } ,
439+ } ) ;
440+ if ( ! seller ) {
441+ return next ( new AuthenticationError ( "Seller not found" ) ) ;
442+ }
443+ //verify password
444+ const isMatch = await bcrypt . compare ( password , seller . password ! ) ;
445+ if ( ! isMatch ) {
446+ return next ( new AuthenticationError ( "Invalid credentials" ) ) ;
447+ }
448+ //Generate Access Token and Refresh Token
449+ const accessToken = jwt . sign (
450+ { id : seller . id , role : "seller" } ,
451+ process . env . JWT_ACCESS_SECRET ! as string ,
452+ { expiresIn : "15m" }
453+ ) ;
454+ const refreshToken = jwt . sign (
455+ { id : seller . id , role : "seller" } ,
456+ process . env . JWT_REFRESH_SECRET ! as string ,
457+ { expiresIn : "7d" }
458+ ) ;
459+
460+ //store refresh token and access token in httpOnly cookies
461+ setCookie ( res , "sellerAccessToken" , accessToken ) ;
462+ setCookie ( res , "sellerRefreshToken" , refreshToken ) ;
463+
464+ res . status ( 200 ) . json ( {
465+ success : true ,
466+ message : "Login successful" ,
467+
468+ user : {
469+ id : seller . id ,
470+ name : seller . name ,
471+ email : seller . email ,
472+ } ,
473+ } ) ;
474+ } catch ( error ) {
475+ return next ( error ) ;
476+ }
477+ } ;
478+
479+ //get loggedin seller info
480+ export const getSeller = async (
481+ req : any ,
482+ res : Response ,
483+ next : NextFunction
484+ ) => {
485+ try {
486+ const seller = req . seller ;
487+ res . status ( 201 ) . json ( {
488+ success : true ,
489+ seller,
490+ } ) ;
491+ } catch ( error ) {
492+ next ( error ) ;
493+ }
494+ } ;
0 commit comments