(paymentInput) Handle Stripe redirection

Closes #631
This commit is contained in:
Baptiste Arnaud
2023-07-27 17:25:02 +02:00
parent e499478dee
commit c99298e49b
15 changed files with 109 additions and 21 deletions

View File

@@ -54,7 +54,7 @@ export const FileUploadForm = (props: Props) => {
setIsUploading(true)
const urls = await uploadFiles({
basePath: `${props.context.apiHost ?? guessApiHost()}/api/typebots/${
props.context.typebotId
props.context.typebot.id
}/blocks/${props.block.id}`,
files: [
{
@@ -79,7 +79,7 @@ export const FileUploadForm = (props: Props) => {
setIsUploading(true)
const urls = await uploadFiles({
basePath: `${props.context.apiHost ?? guessApiHost()}/api/typebots/${
props.context.typebotId
props.context.typebot.id
}/blocks/${props.block.id}`,
files: files.map((file) => ({
file: file,

View File

@@ -4,6 +4,10 @@ import type { Stripe, StripeElements } from '@stripe/stripe-js'
import { BotContext } from '@/types'
import type { PaymentInputOptions, RuntimeOptions } from '@typebot.io/schemas'
import { loadStripe } from '@/lib/stripe'
import {
removePaymentInProgressFromStorage,
setPaymentInProgressInStorage,
} from '../helpers/paymentInProgressStorage'
type Props = {
context: BotContext
@@ -51,11 +55,14 @@ export const StripePaymentForm = (props: Props) => {
setIsLoading(true)
setPaymentInProgressInStorage({
sessionId: props.context.sessionId,
typebot: props.context.typebot,
})
const { error, paymentIntent } = await stripe.confirmPayment({
elements,
confirmParams: {
// TO-DO: Handle redirection correctly.
return_url: props.context.apiHost,
return_url: window.location.href,
payment_method_data: {
billing_details: {
name: props.options.additionalInformation?.name,
@@ -71,6 +78,7 @@ export const StripePaymentForm = (props: Props) => {
},
redirect: 'if_required',
})
removePaymentInProgressFromStorage()
setIsLoading(false)
if (error?.type === 'validation_error') return

View File

@@ -0,0 +1,15 @@
import { BotContext } from '@/types'
export const setPaymentInProgressInStorage = (state: {
sessionId: string
typebot: BotContext['typebot']
}) => {
sessionStorage.setItem('typebotPaymentInProgress', JSON.stringify(state))
}
export const getPaymentInProgressInStorage = () =>
sessionStorage.getItem('typebotPaymentInProgress')
export const removePaymentInProgressFromStorage = () => {
sessionStorage.removeItem('typebotPaymentInProgress')
}