Skip to content

Commit

Permalink
fix: modified handleStripeEvent to account for webhook calls with mis…
Browse files Browse the repository at this point in the history
…sing metadata payload
  • Loading branch information
sebastianwzq committed Dec 7, 2023
1 parent 0f81655 commit bc5974d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/app/modules/payments/stripe.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import * as PaymentService from './payments.service'
import {
StripeFetchError,
StripeMetadataIncorrectEnvError,
StripeMetadataInvalidError,
} from './stripe.errors'
import * as StripeService from './stripe.service'
import { mapRouteError } from './stripe.utils'
Expand Down Expand Up @@ -314,9 +315,13 @@ export const reconcileAccount: ControllerHandler<
return okAsync(undefined)
})
.orElse((error) => {
if (error instanceof StripeMetadataIncorrectEnvError) {
// Intercept this as it is not really an error. Ignore it as it was
// never meant for this environment anyway.
if (
error instanceof StripeMetadataIncorrectEnvError ||
error instanceof StripeMetadataInvalidError
) {
// Intercept this as it is not really an error.
// StripeMetadataIncorrectEnvError: the request will be processed by another environment server.
// StripeMetadataInvalidError: Agencies are using the Stripe account to process payments outside of FormSG.
return okAsync(undefined)
}
logger.error({
Expand Down
15 changes: 11 additions & 4 deletions src/app/modules/payments/stripe.events.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { createLoggerWithLabel } from '../../config/logger'
import { stripe } from '../../loaders/stripe'
import { ControllerHandler } from '../core/core.types'

import { StripeMetadataIncorrectEnvError } from './stripe.errors'
import {
StripeMetadataIncorrectEnvError,
StripeMetadataInvalidError,
} from './stripe.errors'
import * as StripeService from './stripe.service'
import { mapRouteError } from './stripe.utils'

Expand Down Expand Up @@ -91,9 +94,13 @@ const _handleStripeEventUpdates: ControllerHandler<
.match(
() => res.sendStatus(StatusCodes.OK),
(error) => {
if (error instanceof StripeMetadataIncorrectEnvError) {
// Intercept this error and return 202 Accepted instead, indicating
// the request will be processed by another environment server.
if (
error instanceof StripeMetadataIncorrectEnvError ||
error instanceof StripeMetadataInvalidError
) {
// Intercept these errors and return 202 Accepted instead.
// StripeMetadataIncorrectEnvError: the request will be processed by another environment server.
// StripeMetadataInvalidError: Agencies are using the Stripe account to process payments outside of FormSG.
return res.sendStatus(StatusCodes.ACCEPTED)
}
// Additional logging with error details
Expand Down

0 comments on commit bc5974d

Please sign in to comment.