-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: match payer email with registered email (#1552)
Connect the payer session with the currently logged in user.
- Loading branch information
Showing
6 changed files
with
86 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import express from 'express'; | ||
import { getIndexFileContents } from '../IndexController/getIndexFileContents'; | ||
import { getDatabase } from '../../data_layer'; | ||
import { useDefaultEmailService } from '../../services/EmailService/EmailService'; | ||
import UsersRepository from '../../data_layer/UsersRepository'; | ||
import UsersService from '../../services/UsersService'; | ||
import TokenRepository from '../../data_layer/TokenRepository'; | ||
import AuthenticationService from '../../services/AuthenticationService'; | ||
import { getStripe } from '../../lib/integrations/stripe'; | ||
import { extractTokenFromCookies } from './extractTokenFromCookies'; | ||
|
||
export class StripeController { | ||
async getSuccessfulCheckout(req: express.Request, res: express.Response) { | ||
const cookies = req.get('cookie'); | ||
const token = extractTokenFromCookies(cookies); | ||
|
||
if (!token) { | ||
return res.send(getIndexFileContents()); | ||
} | ||
|
||
const database = getDatabase(); | ||
const emailService = useDefaultEmailService(); | ||
const userRepository = new UsersRepository(database); | ||
const usersService = new UsersService(userRepository, emailService); | ||
const tokenRepository = new TokenRepository(database); | ||
const authService = new AuthenticationService( | ||
tokenRepository, | ||
userRepository | ||
); | ||
|
||
const loggedInUser = await authService.getUserFrom(token); | ||
const sessionId = req.query.session_id as string; | ||
|
||
if (loggedInUser && sessionId) { | ||
const stripe = getStripe(); | ||
const session = await stripe.checkout.sessions.retrieve(sessionId); | ||
const email = session.customer_email; | ||
|
||
if (loggedInUser.email !== email && email) { | ||
await usersService.updateSubScriptionEmailUsingPrimaryEmail( | ||
email.toLowerCase(), | ||
loggedInUser.email.toLowerCase() | ||
); | ||
} | ||
} | ||
|
||
res.send(getIndexFileContents()); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/controllers/StripeController/extractTokenFromCookies.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function extractTokenFromCookies( | ||
cookies: string | undefined | ||
): string | null { | ||
if (!cookies) { | ||
return null; | ||
} | ||
|
||
const cookiesArray = cookies.split('; '); | ||
const tokenCookie = cookiesArray.find((cookie) => | ||
cookie.startsWith('token=') | ||
); | ||
return tokenCookie ? tokenCookie.split('=')[1] : null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters