Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/modules/stripe/actions/initialize-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ const InitializeTransactionError = BaseError.subclass(
);

const saleorDataSchema = z.object({
stripeClientSecret: z.string(),
paymentIntent: z.object({
stripeClientSecret: z.string().optional(),
errors: z
.array(
z.object({
code: z.string(),
message: z.string(),
}),
)
.optional(),
}),
});

export const initializeTransaction = actionClient
Expand All @@ -49,7 +59,11 @@ export const initializeTransaction = actionClient
envUrl: envUrlSchema,
checkoutId: z.string(),
paymentGatewayId: z.string(),
data: z.unknown(),
data: z.object({
paymentIntent: z.object({
paymentMethod: z.string(),
}),
}),
amount: z.number(),
idempotencyKey: z.string(),
}),
Expand Down
42 changes: 38 additions & 4 deletions src/modules/stripe/components/stripe-checkout-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const StripeCheckoutFormWrapped = (props: {
const stripe = useStripe();
const elements = useElements();
const [loading, setLoading] = useState(false);
const [paymentMethod, setPaymentMethod] = useState<string | null>(null);

const handleSubmit = async (event: any) => {
if (!stripe) {
Expand All @@ -79,21 +80,49 @@ export const StripeCheckoutFormWrapped = (props: {

setLoading(true);

const initializeTransactionData = await initializeTransaction({
if (!paymentMethod) {
setLoading(false);
toast({
variant: "destructive",
title: "Error submitting checkout form",
description: "No payment method selected",
});
return;
}

const initializeTransactionResult = await initializeTransaction({
checkoutId: props.checkoutId,
amount: props.saleorAmount,
envUrl: props.envUrl,
paymentGatewayId: props.paymentGatewayId,
idempotencyKey: getIdempotencyKey(),
data: {
paymentIntent: {
paymentMethod,
},
},
});

if (!initializeTransactionData?.data) {
if (!initializeTransactionResult?.data) {
setLoading(false);
throw new BaseError("No data returned from the server");
}

const dataErrors =
initializeTransactionResult?.data.data.paymentIntent?.errors ?? [];

if (dataErrors.length > 0) {
setLoading(false);
toast({
variant: "destructive",
title: dataErrors[0]?.code,
description: dataErrors[0]?.message,
});
return;
}

const stripeClientSecret =
initializeTransactionData?.data?.data.stripeClientSecret;
initializeTransactionResult?.data?.data.paymentIntent.stripeClientSecret;

if (!stripeClientSecret) {
setLoading(false);
Expand Down Expand Up @@ -134,7 +163,11 @@ export const StripeCheckoutFormWrapped = (props: {

return (
<form onSubmit={handleSubmit} className="flex flex-col gap-4">
<PaymentElement />
<PaymentElement
onChange={(event) => {
setPaymentMethod(event.value.type);
}}
/>
<div className="flex justify-stretch">
<Button
type="submit"
Expand Down Expand Up @@ -184,6 +217,7 @@ export const StripeCheckoutForm = ({
amount: stripeMoney.amount,
currency: stripeMoney.currency,
mode: "payment",
paymentMethodTypes: ["card"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe lets also add not supported method so we can test error handling

}}
>
<StripeCheckoutFormWrapped
Expand Down