vendure-ecommerce / vendure

The commerce platform with customization in its DNA.
https://www.vendure.io
MIT License
5.39k stars 943 forks source link

Add Payment To Order in a different channel than the default #2881

Open margamorais opened 3 weeks ago

margamorais commented 3 weeks ago

Describe the bug I have multiple channels created in my backoffice. When I try to complete an order in a channel different from the default channel, the order fails when calling the addPaymentToOrder mutation. The following is a screenshot of the error:

image

The error states that an order with this ID is not found, however, this is not the ID of my current active order:

image

The current active order has the id 28, and the error mentions id 44, and every time I call the mutation, I get the same error but with an incremented ID in the error (i.e. 44, then 47, then 48).

If I try to add the payment in the default channel, its successful and I can finish the payment flow.

To Reproduce Steps to reproduce the behavior:

  1. Create a new channel.
  2. Add a product to the new channel.
  3. Assign the payment, shipping methods, etc to the new channel.
  4. Create an order with that product in the new channel.
  5. Transition the order to "Arranging Payment"
  6. Attempt to addPaymentToOrder (to me, its happening with any payment method).
  7. See the error.

Expected behavior I should be able to complete an order in a channel different than the default channel.

Environment (please complete the following information):

michaelbromley commented 3 weeks ago

Hi,

I'm not able to recreate this. Can you check a couple of things:

  1. Is there actually an order in the system corresponding to the ids 44, 47 etc?
  2. Do you have a custom ActiveOrderStrategy defined?

When you execute the addItemToOrder mutation, the active order is determined here. This uses the ActiveOrderStrategy to determine which order to operate on. In the default implementation this is looking at the active session to determine the order id.

So to debug this I'd look in the session table in the database and see what the activeOrderId value is listed as for the active session.

margamorais commented 2 weeks ago

Hi Michael,

  1. There are no orders in the DB with those IDs, or the ids ever mentioned in the error.
  2. We are using the default ActiveOrderStrategy.

I checked the session table in the db, and the activeOrderId corresponding to the session I'm currently on has the same id that is returned when I query activeOrder, and it's definitely different from the ID that shows up in the error when I use the mutation addPaymentToOrder.

margamorais commented 2 weeks ago

We detected it's a problem coming from one of our configurations, we are debugging to find out the issue. Thank you for your help, I'm going to close the issue now.

margamorais commented 2 weeks ago

Hi @michaelbromley,

After debugging all of our plugins, we found out the issue is happening in the multivendor plugin. We are using the multivendor plugin that vendure provides, however we had to make some changes in the plugin to adapt it to our use case. One of the those things is, we need products to be assigned to multiple channels, not just the default channel and the seller channel, as it is assumed in the multivendor original plugin.

The first thing we did was altering the mv-order-seller-strategy.ts, setOrderLineSellerChannel method, where we need to find the seller channel amongst the multiple channels that the product might have. We replaced this:

if (orderLine.productVariant.channels.length === 2) {
      const sellerChannel = orderLine.productVariant.channels.find(
        (c) => !idsAreEqual(c.id, defaultChannel.id)
      );
      if (sellerChannel) {
        return sellerChannel;
      }
    }

with this:

if (orderLine.productVariant.channels.length > 1) {
      const sellerChannel = orderLine.productVariant.channels.find(
        (c) =>
          !c?.customFields?.isCompanyChannel &&
          !idsAreEqual(c.id, defaultChannel.id)
      );
      if (sellerChannel) {
        return sellerChannel;
      }
    }

And then the issue with the payment started happening.

Do you know if there is any other part of the multivendor plugin where we would have to make changes to make this use case possible?

Thank you for your help!