medusa icon indicating copy to clipboard operation
medusa copied to clipboard

Each cart manipulation creates a new payment intent in Stripe

Open chemicalkosek opened this issue 3 years ago • 3 comments

I have noticed very high TTFB on each cart manipulation. After a lot of digging I have narrowed it down to that it's because each time a new payment intent is made in Stripe.

Steps to reproduce:

  1. Be sure to have Stripe set up
  2. Add items to cart.
  3. At this step all cart manipulation should be fast
  4. Visit checkout page (payment sessions need to be created)
  5. From now on each cart manipulation, like item quantity change or shipping method change causes a new payment intent to be made in Stripe. That creates high TTFB 800-1000ms (roundtrip to Stripe) making everything sluggish and a lot of payments in Stripe (I have thousands now)

chemicalkosek avatar May 14 '22 16:05 chemicalkosek

Further research on this issue gave the following insight:

If you go to the checkout page, Stripe creates a new customer with new payment intent. Now because email is not filled out yet, each cart manipulation creates another Stripe customer with new payment intent:

image

This is the same cart but each cart manipulation creates a new Stripe customer. This will stop if you fill in the email in checkout page. If email is filled than the payment intent is just updated as expected. Also each email change will create a new customer and new payment intent (which is expected I guess?)

I have "fixed" it on the frontend by modifying Kasper's init payment sessions function from next starter. I just delay creating payment sessions until email exists in cart:

  useEffect(() => {
    const initPayment = async () => {
      if (
        cart?.id &&
        !cart.payment_sessions?.length &&
        cart?.items?.length &&
        cart?.email
      ) {
        const paymentSession = await createPaymentSession(cart.id);
        if (!paymentSession) {
          setTimeout(initPayment, 500);
        } else {
          setCart(paymentSession);
        }
      }
    };

    initPayment();
  }, [cart, setCart]);

chemicalkosek avatar May 18 '22 15:05 chemicalkosek

did you try to see what happen after the email has been entered and that the customer goes back to the store to add some items?

adrien2p avatar May 19 '22 14:05 adrien2p

did you try to see what happen after the email has been entered and that the customer goes back to the store to add some items?

Yes, as I wrote, filled email makes most of the issues go away. At least the issues with many stripe customers and payment intents. There's still delay when manipulating cart, because it has to connect to Stripe to update the payment intents with new totals. Although it's approx 500ms, not 1000ms like when creating payment intents. I don't know whether this should be somehow protected in the medusa stripe plugin, medusa itself or the docs should state that we should not create payment sessions until email in cart exists. Btw I use Stripe plugin plus my own stripe service to handle P24 payments (https://stripe.com/docs/payments/p24), so there's always two payment intents. Anyway when payment sessions are already created then all cart modifications have delay, but this might be by design.

chemicalkosek avatar May 19 '22 15:05 chemicalkosek

Closing as most of problems with multiple payment intents are fixed in latest versions If anybody still encounters this: remember to also create payment sessions after email in cart is filled out.

chemicalkosek avatar Jan 19 '23 12:01 chemicalkosek