Skip to content

Commit

Permalink
fix: no more considering that a payment becomes successful when it wa…
Browse files Browse the repository at this point in the history
…s still pending
  • Loading branch information
TeddyRoncin committed Sep 12, 2024
1 parent c2b7256 commit 27ff715
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/controllers/stripe/paymentSucceededWebhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default [
try {
const { cart } = response.locals;

if (cart.transactionState !== TransactionState.processing) {
if (![TransactionState.processing, TransactionState.pending].includes(cart.transactionState)) {
logger.warn(
`A transaction with state ${cart.transactionState} has received the webhook 'payment_intent.succeeded' by Stripe.`,
);
Expand Down
2 changes: 1 addition & 1 deletion tests/fake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { createFakeTeam, createFakeUser } from './utils';
await database.orga.deleteMany();
await database.user.deleteMany();

// For each tournaments, create fake teams
// For each tournament, create fake teams
const tournaments = await fetchTournaments();

await Promise.all(
Expand Down
19 changes: 11 additions & 8 deletions tests/stripe/paymentSucceededWebhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,23 @@ describe('POST /stripe/succeeded', () => {
.send(generateCart(paymentIntent.id))
.expect(401, { error: Error.PleaseDontPlayWithStripeWebhooks })); // Given status is succeeded, real status is requires_action.

it('should change the transactionState of the cart from processing to paid', async () => {
paymentIntent.status = 'succeeded';
await updateCart(cart.id, { transactionState: TransactionState.processing });
await request(app).post('/stripe/succeeded').send(generateCart(paymentIntent.id)).expect(200, { api: 'ok' });
const databaseCart = await database.cart.findUnique({ where: { id: cart.id } });
expect(databaseCart.transactionState).to.equal(TransactionState.paid);
describe('test for initial transactionState = `processing` | `pending`', () => {
for (const transactionState of [TransactionState.processing, TransactionState.pending]) {
it(`should change the transactionState of the cart from ${transactionState} to paid`, async () => {
paymentIntent.status = 'succeeded';
await updateCart(cart.id, { transactionState });
await request(app).post('/stripe/succeeded').send(generateCart(paymentIntent.id)).expect(200, { api: 'ok' });
const databaseCart = await database.cart.findUnique({ where: { id: cart.id } });
expect(databaseCart.transactionState).to.equal(TransactionState.paid);
});
}
});

describe('test for initial transactionState = `paid` | `expired` | `refunded` | `pending` | `canceled`', () => {
describe('test for initial transactionState = `paid` | `expired` | `refunded` | `canceled`', () => {
for (const transactionState of [
TransactionState.paid,
TransactionState.expired,
TransactionState.refunded,
TransactionState.pending,
TransactionState.canceled,
]) {
it(`should not change the transactionState of the cart as it already equals ${transactionState}`, async () => {
Expand Down

0 comments on commit 27ff715

Please sign in to comment.