medusajs / medusa

Building blocks for digital commerce
https://medusajs.com
MIT License
24.32k stars 2.38k forks source link

Customer credit card charged when inventory is zero during order complete process #4946

Open Arsenalist opened 11 months ago

Arsenalist commented 11 months ago

Medusa: 1.14. Stripe Plugin: 6.0

Symptom: Customer credit card is charged even when inventory is zero during order complete process.

To reproduce, do the following:

The following error is received on cart complete

{"code":"payment_intent_unexpected_state","type":"invalid_data","message":"An error occurred in cancelPayment:\nundefined\n"}

The Stripe error log has:

You cannot cancel this PaymentIntent because it has a status of succeeded.

This happens because cancelPayment() is being called on a "succeeded" payment. @chemicalkosek describes it best on Discord thread. Here is the exact line of code where it happens: https://github.com/medusajs/medusa/blob/c348263fdba73ad0cdb8b8941d91a415d5b26a2a/packages/medusa/src/strategies/cart-completion.ts#L373

The payment had a requires_capture status then it would succeed with the cancelation on Stripe. Because it's succeeded , it fails. I think this is a remnant from back when this was developed for uncaptured card payments.

Nowadays all those gateway payments are captured automatically in Stripe and you can't change that. It's a requirement to autocapture. Those payments are actually a majority in Poland. A little percentage of customers pay by card.

Some possible solutions:

  1. Check that payment status is not succeeded before trying to cancel payment
  2. Return a different entity indicating the issue so customer can be presented with something better
Arsenalist commented 11 months ago

Hi - any update on this? This happened again and it's causing me chaos. Basically:

alessioacella commented 2 months ago

+1