medusajs / medusa

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

Wrong amount for cart payed from webhook #8379

Open amir-saadallah opened 1 month ago

amir-saadallah commented 1 month ago

Bug report

Describe the bug

I have a subscriber that attempt to mark a cart paid and create an order from it. everything looks fine except for the order total image in order summary image

System information

Medusa version (including plugins): 1.20.8 Node.js version: 20.10 Database: PostgreSQL 16 Operating system: Browser (if relevant):

srindom commented 1 month ago

Hi @amir-saadallah - are you able to share a minimal reproduction of this

amir-saadallah commented 1 month ago

Hi @amir-saadallah - are you able to share a minimal reproduction of this

This is what I'm trying to do with a subscriber that acts upon webhook receive and this is the success case where I attempt to create an order from the cart

async function handleSuccessPayment(
  payment_ref: string,
  konnectService_: KonnectPaymentService,
  cartService_: CartService,
  paymentRepository_: Repository<Payment>,
  orderService_: OrderService,
  manager_: EntityManager
) {
  // We need to ensure, that an order is created in situations, where the
  // customer might have closed their browser before order creation

  const paymentDetails = await konnectService_.retrievePayment({
    payment_id: payment_ref,
  });
  const cart = await cartService_.retrieveWithTotals((paymentDetails as any).orderId);
  try {
    const orderPayment = await paymentRepository_.findOne({
      where: { cart_id: cart.id },
    });

    if (!orderPayment) {
      throw new Error("Order payment not found");
    }
    // save the ref
    const updatedPayment = {
      ...orderPayment,
      data: {
        ...orderPayment.data,
        resultCode: "authorized",
        payment_id: payment_ref,
      },
    };
    console.log("attempt to save");
    await paymentRepository_.save(updatedPayment);
    // attempt to auto-capture
    console.log("attempt to capture");
    await konnectService_.capturePayment({ payment_id: payment_ref });
  } catch (error) {
    // not created -> let's do it 
    manager_.transaction(async (manager) => {
      const session = {
        payment_id: payment_ref,
        resultCode: "authorized",
        amount: cart.total,
      };

      await cartService_
        .withTransaction(manager)
        .updatePaymentSession(cart.id, session);
        // authorize
      await cartService_.withTransaction(manager).authorizePayment(cart.id);
      // create the order
      await orderService_.withTransaction(manager).createFromCart(cart.id);
    });
  }
}
srindom commented 1 month ago

Unfortunately, not able to reproduce with the provided information. Any chance you can create a repository where this issue occurs.

amir-saadallah commented 3 weeks ago

I figured it out. for some reason, if u don't set the shipping option from the methods available in the cart (in my case I was fetching all available shipping in the region) the dashboard will show the shipping price as total. I didn't dig into the API to see how we do stuff but changing the shipping methods selection from the methods available in the cart fixes that.