import { StripeError } from '@stripe/stripe-js' type stripeErrorReason = 'PAYMENT_ALREADY_CONFIRMED' interface IAdaptedStripeError extends StripeError { isStripeError: boolean; errorReason?: stripeErrorReason; } export const adaptStripeError = (error: StripeError): IAdaptedStripeError => { const adaptedError = { ...error, isStripeError: true } switch (error.code) { case 'payment_intent_unexpected_state': if (error?.payment_intent?.status === 'succeeded') { return { ...adaptedError, errorReason: 'PAYMENT_ALREADY_CONFIRMED', message: 'You cannot confirm this PaymentIntent because it has already succeeded after being previously confirmed.', } } return { ...adaptedError } default: return adaptedError } }