Skip to content
Merged
Changes from all commits
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
20 changes: 17 additions & 3 deletions src/modules/stripe/components/stripe-checkout-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useElements,
useStripe,
} from "@stripe/react-stripe-js";
import type { StripePaymentElementChangeEvent } from "@stripe/stripe-js";
import { loadStripe } from "@stripe/stripe-js";
import { useState } from "react";

Expand Down Expand Up @@ -52,10 +53,19 @@ export const StripeCheckoutFormWrapped = (props: {
const stripe = useStripe();
const elements = useElements();
const [loading, setLoading] = useState(false);
const [paymentMethodType, setPaymentMethodType] = useState<string | null>(
null,
);

const { mutateAsync: transactionInitialize } =
useTransactionInitializeMutation();

const handlePaymentElementChange = (
event: StripePaymentElementChangeEvent,
) => {
setPaymentMethodType(event.value.type);
};

const handleSubmit = async (event: any) => {
if (!stripe) {
return null;
Expand All @@ -81,7 +91,11 @@ export const StripeCheckoutFormWrapped = (props: {

setLoading(true);

if (!selectedPaymentMethod) {
// Use selectedPaymentMethod from submit, or fall back to tracked type from onChange
// This handles Stripe Link which doesn't return selectedPaymentMethod from submit()
const resolvedPaymentMethod = selectedPaymentMethod || paymentMethodType;

if (!resolvedPaymentMethod) {
setLoading(false);
toast({
variant: "destructive",
Expand All @@ -94,7 +108,7 @@ export const StripeCheckoutFormWrapped = (props: {
const transactionInitializeResult = await transactionInitialize({
data: {
paymentIntent: {
paymentMethod: selectedPaymentMethod,
paymentMethod: resolvedPaymentMethod,
},
},
saleorAmount: props.saleorAmount,
Expand Down Expand Up @@ -163,7 +177,7 @@ export const StripeCheckoutFormWrapped = (props: {

return (
<form onSubmit={handleSubmit} className="flex flex-col gap-4">
<PaymentElement />
<PaymentElement onChange={handlePaymentElementChange} />
<div className="flex justify-stretch">
<FormButton
type="submit"
Expand Down