2
0

(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

@@ -1,7 +1,11 @@
import { InitialChatReply } from '@/types'
import { BotContext, InitialChatReply } from '@/types'
import { guessApiHost } from '@/utils/guessApiHost'
import type { SendMessageInput, StartParams } from '@typebot.io/schemas'
import { isNotDefined, isNotEmpty, sendRequest } from '@typebot.io/lib'
import {
getPaymentInProgressInStorage,
removePaymentInProgressFromStorage,
} from '@/features/blocks/inputs/payment/helpers/paymentInProgressStorage'
export async function getInitialChatReplyQuery({
typebot,
@@ -10,24 +14,54 @@ export async function getInitialChatReplyQuery({
prefilledVariables,
startGroupId,
resultId,
stripeRedirectStatus,
}: StartParams & {
stripeRedirectStatus?: string
apiHost?: string
}) {
if (isNotDefined(typebot))
throw new Error('Typebot ID is required to get initial messages')
return sendRequest<InitialChatReply>({
const paymentInProgressStateStr = getPaymentInProgressInStorage() ?? undefined
const paymentInProgressState = paymentInProgressStateStr
? (JSON.parse(paymentInProgressStateStr) as {
sessionId: string
typebot: BotContext['typebot']
})
: undefined
if (paymentInProgressState) removePaymentInProgressFromStorage()
const { data, error } = await sendRequest<InitialChatReply>({
method: 'POST',
url: `${isNotEmpty(apiHost) ? apiHost : guessApiHost()}/api/v1/sendMessage`,
body: {
startParams: {
isPreview,
typebot,
prefilledVariables,
startGroupId,
resultId,
isStreamEnabled: true,
},
startParams: paymentInProgressState
? undefined
: {
isPreview,
typebot,
prefilledVariables,
startGroupId,
resultId,
isStreamEnabled: true,
},
sessionId: paymentInProgressState?.sessionId,
message: paymentInProgressState
? stripeRedirectStatus === 'failed'
? 'fail'
: 'Success'
: undefined,
} satisfies SendMessageInput,
})
return {
data: data
? {
...data,
...(paymentInProgressState
? { typebot: paymentInProgressState.typebot }
: {}),
}
: undefined,
error,
}
}