handle-stripe-payment-action.ts 896 B

123456789101112131415161718192021222324252627
  1. import { FetchError, postJSON } from '@/infrastructure/fetch-json'
  2. import { loadStripe } from '@stripe/stripe-js/pure'
  3. export default async function handleStripePaymentAction(
  4. error: FetchError
  5. ): Promise<{ handled: boolean }> {
  6. const clientSecret = error?.data?.clientSecret
  7. const publicKey = error?.data?.publicKey
  8. if (clientSecret && publicKey) {
  9. const stripe = await loadStripe(publicKey)
  10. if (stripe) {
  11. const manualConfirmationFlow =
  12. await stripe.confirmCardPayment(clientSecret)
  13. if (!manualConfirmationFlow.error) {
  14. try {
  15. await postJSON(`/user/subscription/sync`)
  16. } catch (error) {
  17. // if the sync fails, there may be stale data until the webhook is
  18. // processed but we can't do any special handling for that in here
  19. }
  20. return { handled: true }
  21. }
  22. }
  23. }
  24. return { handled: false }
  25. }