PaymentConfirmation.tsx 981 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import { useStripe } from '@stripe/react-stripe-js'
  2. import { PaymentIntentResult } from '@stripe/stripe-js'
  3. import { useEffect } from 'react'
  4. import { LoadingLine } from 'ui'
  5. export const PaymentConfirmation = ({
  6. paymentIntentSecret,
  7. onPaymentIntentConfirm,
  8. onLoadingChange,
  9. onError,
  10. }: {
  11. paymentIntentSecret: string
  12. onPaymentIntentConfirm: (response: PaymentIntentResult) => void
  13. onLoadingChange: (loading: boolean) => void
  14. onError?: (error: Error) => void
  15. }) => {
  16. const stripe = useStripe()
  17. useEffect(() => {
  18. if (stripe && paymentIntentSecret) {
  19. onLoadingChange(true)
  20. stripe!
  21. .confirmCardPayment(paymentIntentSecret)
  22. .then((res) => {
  23. onPaymentIntentConfirm(res)
  24. onLoadingChange(false)
  25. })
  26. .catch((err) => {
  27. console.error(err)
  28. onError?.(err)
  29. onLoadingChange(false)
  30. })
  31. }
  32. }, [paymentIntentSecret, stripe])
  33. return <LoadingLine loading={true} />
  34. }