2022-06-07 08:49:12 +02:00
|
|
|
import { z } from 'zod'
|
2023-03-09 08:46:36 +01:00
|
|
|
import {
|
|
|
|
optionBaseSchema,
|
|
|
|
blockBaseSchema,
|
|
|
|
credentialsBaseSchema,
|
|
|
|
} from '../../baseSchemas'
|
2023-01-25 11:27:47 +01:00
|
|
|
import { InputBlockType } from '../enums'
|
|
|
|
import { PaymentProvider } from './enums'
|
2022-06-07 08:49:12 +02:00
|
|
|
|
|
|
|
export type CreditCardDetails = {
|
|
|
|
number: string
|
|
|
|
exp_month: string
|
|
|
|
exp_year: string
|
|
|
|
cvc: string
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:54:37 -04:00
|
|
|
const addressSchema = z.object({
|
|
|
|
country: z.string().optional(),
|
|
|
|
line1: z.string().optional(),
|
|
|
|
line2: z.string().optional(),
|
|
|
|
state: z.string().optional(),
|
|
|
|
city: z.string().optional(),
|
|
|
|
postalCode: z.string().optional(),
|
|
|
|
})
|
|
|
|
|
2023-03-14 16:42:12 +01:00
|
|
|
export const paymentInputOptionsSchema = optionBaseSchema.merge(
|
2022-06-07 08:49:12 +02:00
|
|
|
z.object({
|
|
|
|
provider: z.nativeEnum(PaymentProvider),
|
|
|
|
labels: z.object({
|
|
|
|
button: z.string(),
|
|
|
|
success: z.string().optional(),
|
|
|
|
}),
|
|
|
|
additionalInformation: z
|
|
|
|
.object({
|
2023-03-29 15:08:03 +02:00
|
|
|
description: z.string().optional(),
|
2022-06-07 08:49:12 +02:00
|
|
|
name: z.string().optional(),
|
|
|
|
email: z.string().optional(),
|
|
|
|
phoneNumber: z.string().optional(),
|
2023-05-05 14:54:37 -04:00
|
|
|
address: addressSchema.optional(),
|
2022-06-07 08:49:12 +02:00
|
|
|
})
|
|
|
|
.optional(),
|
|
|
|
credentialsId: z.string().optional(),
|
|
|
|
currency: z.string(),
|
|
|
|
amount: z.string().optional(),
|
2023-07-27 17:25:02 +02:00
|
|
|
retryMessageContent: z.string().optional(),
|
2022-06-07 08:49:12 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2022-12-22 17:02:34 +01:00
|
|
|
export const paymentInputRuntimeOptionsSchema = z.object({
|
|
|
|
paymentIntentSecret: z.string(),
|
|
|
|
amountLabel: z.string(),
|
|
|
|
publicKey: z.string(),
|
|
|
|
})
|
|
|
|
|
2023-03-14 16:42:12 +01:00
|
|
|
export const paymentInputSchema = blockBaseSchema.merge(
|
2022-06-07 08:49:12 +02:00
|
|
|
z.object({
|
2022-06-11 07:27:38 +02:00
|
|
|
type: z.enum([InputBlockType.PAYMENT]),
|
2022-06-07 08:49:12 +02:00
|
|
|
options: paymentInputOptionsSchema,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2023-03-09 08:46:36 +01:00
|
|
|
export const stripeCredentialsSchema = z
|
|
|
|
.object({
|
|
|
|
type: z.literal('stripe'),
|
|
|
|
data: z.object({
|
|
|
|
live: z.object({
|
|
|
|
secretKey: z.string(),
|
|
|
|
publicKey: z.string(),
|
|
|
|
}),
|
|
|
|
test: z.object({
|
|
|
|
secretKey: z.string().optional(),
|
|
|
|
publicKey: z.string().optional(),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
.merge(credentialsBaseSchema)
|
|
|
|
|
2022-06-07 08:49:12 +02:00
|
|
|
export const defaultPaymentInputOptions: PaymentInputOptions = {
|
|
|
|
provider: PaymentProvider.STRIPE,
|
|
|
|
labels: { button: 'Pay', success: 'Success' },
|
2023-07-27 17:25:02 +02:00
|
|
|
retryMessageContent: 'Payment failed. Please, try again.',
|
2022-06-07 08:49:12 +02:00
|
|
|
currency: 'USD',
|
|
|
|
}
|
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export type PaymentInputBlock = z.infer<typeof paymentInputSchema>
|
2022-06-07 08:49:12 +02:00
|
|
|
export type PaymentInputOptions = z.infer<typeof paymentInputOptionsSchema>
|
2022-12-22 17:02:34 +01:00
|
|
|
export type PaymentInputRuntimeOptions = z.infer<
|
|
|
|
typeof paymentInputRuntimeOptionsSchema
|
|
|
|
>
|
2023-03-09 08:46:36 +01:00
|
|
|
export type StripeCredentials = z.infer<typeof stripeCredentialsSchema>
|
2023-05-05 14:54:37 -04:00
|
|
|
export type PaymentAddress = NonNullable<
|
|
|
|
PaymentInputOptions['additionalInformation']
|
|
|
|
>['address']
|