Skip to content

Commit

Permalink
fix: match payer email with registered email (#1552)
Browse files Browse the repository at this point in the history
Connect the payer session with the currently logged in user.
  • Loading branch information
aalemayhu authored Jun 27, 2024
1 parent 85ba57c commit 391fee3
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 7 deletions.
49 changes: 49 additions & 0 deletions src/controllers/StripeController/StripeController.ts
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 src/controllers/StripeController/extractTokenFromCookies.ts
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;
}
12 changes: 8 additions & 4 deletions src/data_layer/UsersRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,19 @@ class UsersRepository {
]);
}

async updateSubscriptionLinkedEmail(owner: string, email: string) {
async linkCurrentUserWithEmail(owner: string, email: string) {
const user = await this.database(this.table).where({ id: owner }).first();
if (!user) {
return null;
}

return this.updateSubScriptionEmailUsingPrimaryEmail(user.email, email);
}

updateSubScriptionEmailUsingPrimaryEmail(email: string, newEmail: string) {
return this.database('subscriptions')
.where({ email: user.email })
.update({ linked_email: email.toLowerCase() });
.where({ email: email.toLowerCase() })
.update({ linked_email: newEmail.toLowerCase() });
}

async getSubscriptionLinkedEmail(owner: string) {
Expand All @@ -86,7 +90,7 @@ class UsersRepository {
}

const subscription: Subscriptions = await this.database('subscriptions')
.where({ email: user.email })
.where({ email: user.email.toLowerCase() })
.select('linked_email')
.first();
return subscription?.linked_email;
Expand Down
6 changes: 6 additions & 0 deletions src/routes/WebhookRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import {
updateStoreSubscription,
} from '../lib/integrations/stripe';
import { getDatabase } from '../data_layer';
import { StripeController } from '../controllers/StripeController/StripeController';

const WebhooksRouter = () => {
const router = express.Router();
const controller = new StripeController();

router.post(
'/webhook',
Expand Down Expand Up @@ -73,6 +75,10 @@ const WebhooksRouter = () => {
response.send();
}
);

router.get('/successful-checkout', (req, res) =>
controller.getSuccessfulCheckout(req, res)
);
return router;
};

Expand Down
4 changes: 2 additions & 2 deletions src/services/AuthenticationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class AuthenticationService {
async getIsSubscriber(owner: string, db: Knex, email: string) {
const linkedEmail = await db('subscriptions')
.select('active')
.where({ linked_email: email })
.where({ linked_email: email.toLowerCase() })
.first();

if (linkedEmail) {
Expand All @@ -129,7 +129,7 @@ class AuthenticationService {

const result = await db('subscriptions')
.select('active')
.where({ email: email })
.where({ email: email.toLowerCase() })
.first();

return result?.active ?? false;
Expand Down
9 changes: 8 additions & 1 deletion src/services/UsersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ class UsersService {
}

updateSubscriptionLinkedEmail(owner: string, email: string) {
return this.repository.updateSubscriptionLinkedEmail(owner, email);
return this.repository.linkCurrentUserWithEmail(owner, email);
}

updateSubScriptionEmailUsingPrimaryEmail(email: string, newEmail: string) {
return this.repository.updateSubScriptionEmailUsingPrimaryEmail(
email,
newEmail
);
}

getSubscriptionLinkedEmail(owner: string) {
Expand Down

0 comments on commit 391fee3

Please sign in to comment.