#Payment sessions are required to complete cart

15 messages · Page 1 of 1 (latest)

tranquil cypress
#

Ill continue in here

#

I am trying to confirm a stripe payment, and the when the payment is confirmed in stripe i would complete the cart but i have no clue why it isnt working.
Here is how it works:
1- when the user loads the payment page, we initate a new payment session:

#
    async initiatePaymentSession(): Promise<StorePaymentCollectionResponse> {
        const cart = await this.getCart();

        const paymentCollection =
            await medusaClient.store.payment.initiatePaymentSession(cart, {
                provider_id: "pp_stripe_stripe"
            });

        return paymentCollection;
    }
#

2- when the user submits we try to confirm the payment from stripe and then complete the cart:

#
const handleSubmit = async (event: React.FormEvent) => {
        event.preventDefault();

        if (!stripe || !elements) {
            return;
        }

        setLoading(true);
        setError(null);

        try {
            await MedusaService.updateCart({
                email: data.email,
                shipping_address: {
                    first_name: data.firstName,
                    last_name: data.lastName,
                    address_1: data.address,
                    city: data.city,
                    country_code: "se",
                    postal_code: data.postalCode,
                    phone: data.phone
                }
            });


            const { error: stripeError, paymentIntent } = await stripe.confirmPayment(
                {
                    elements,
                    confirmParams: {
                        return_url: `${window.location.origin}/order-confirmation`
                    },
                    redirect: "if_required"
                }
            );
            console.log("🚀 ~ handleSubmit ~ paymentIntent:", paymentIntent);

            if (stripeError) {
                setError(stripeError.message ?? "An error occurred");
                return;
            }

            if (paymentIntent && paymentIntent.status === "succeeded") {
                await MedusaService.completeCart();
            }
        } catch (err) {
            setError("An error occurred during checkout");
            console.error("Checkout error:", err);
        } finally {
            setLoading(false);
        }
    };
#

3- lastely the idea is to navigate the user to a order confirmation page

But i am losing my mind trying to figure out why i am getting this error.

tranquil cypress
#

And i just something weird, it seems that the payment_collection in the cart is not a object containing a single payment session instead it is a array

hardy mica
#

If you call updateCart and it causes the cart amount to change any existing payment session would be deleted. If you have Automatic Taxes on then the cart amount could be changed due to taxes being different based on the address you're updating. Maybe try removing the updateCart call and see if it completes successfully.

tranquil cypress
#

OMG thanks i think that solved the issue, but how am i supposed to set the user information then?

hardy mica
tranquil cypress
#

Nope nevermind, the update wasnt the problem, idk why tf it worked for one order and then stopped working again

slim patio
#

i have one question for you this payment session only generates when user logged only or without login also will work?

tranquil cypress
#

No i found out what the error was, so i think i missunderstood the documentation, but i had to setup up ngrok and reverse the medusa localhost url so that stripe can call the webhooks to mark a payment session as authorized

woven light
placid carbon
#

+1