diff --git a/.changeset/slimy-seas-join.md b/.changeset/slimy-seas-join.md new file mode 100644 index 0000000000..0cc841f280 --- /dev/null +++ b/.changeset/slimy-seas-join.md @@ -0,0 +1,5 @@ +--- +"@medusajs/core-flows": patch +--- + +fix(core-flows): refresh payment collections upon shipping changes diff --git a/integration-tests/http/__tests__/cart/store/cart.spec.ts b/integration-tests/http/__tests__/cart/store/cart.spec.ts index 6f3a73444f..8cf153e2fc 100644 --- a/integration-tests/http/__tests__/cart/store/cart.spec.ts +++ b/integration-tests/http/__tests__/cart/store/cart.spec.ts @@ -302,7 +302,7 @@ medusaIntegrationTestRunner({ }) describe("POST /store/carts/:id/line-items", () => { - let shippingOption + let shippingOption, shippingOptionExpensive beforeEach(async () => { const stockLocation = ( @@ -358,25 +358,63 @@ medusaIntegrationTestRunner({ adminHeaders ) + const shippingOptionPayload = { + name: `Shipping`, + service_zone_id: fulfillmentSet.service_zones[0].id, + shipping_profile_id: shippingProfile.id, + provider_id: "manual_test-provider", + price_type: "flat", + type: { + label: "Test type", + description: "Test description", + code: "test-code", + }, + prices: [ + { currency_code: "usd", amount: 1000 }, + { + currency_code: "usd", + amount: 0, + rules: [ + { + attribute: "item_total", + operator: "gt", + value: 5000, + }, + ], + }, + ], + rules: [ + { + attribute: "enabled_in_store", + value: '"true"', + operator: "eq", + }, + { + attribute: "is_return", + value: "false", + operator: "eq", + }, + ], + } + shippingOption = ( + await api.post( + `/admin/shipping-options`, + shippingOptionPayload, + adminHeaders + ) + ).data.shipping_option + + shippingOptionExpensive = ( await api.post( `/admin/shipping-options`, { - name: `Shipping`, - service_zone_id: fulfillmentSet.service_zones[0].id, - shipping_profile_id: shippingProfile.id, - provider_id: "manual_test-provider", - price_type: "flat", - type: { - label: "Test type", - description: "Test description", - code: "test-code", - }, + ...shippingOptionPayload, prices: [ - { currency_code: "usd", amount: 1000 }, + { currency_code: "usd", amount: 10000 }, { currency_code: "usd", - amount: 0, + amount: 5000, rules: [ { attribute: "item_total", @@ -386,18 +424,6 @@ medusaIntegrationTestRunner({ ], }, ], - rules: [ - { - attribute: "enabled_in_store", - value: '"true"', - operator: "eq", - }, - { - attribute: "is_return", - value: "false", - operator: "eq", - }, - ], }, adminHeaders ) @@ -555,6 +581,61 @@ medusaIntegrationTestRunner({ }) ) }) + + it("should update payment collection upon changing shipping option", async () => { + await api.post( + `/store/carts/${cart.id}/shipping-methods`, + { option_id: shippingOption.id }, + storeHeaders + ) + + await api.post( + `/store/payment-collections`, + { cart_id: cart.id }, + storeHeaders + ) + + const cartAfterCollection = ( + await api.get(`/store/carts/${cart.id}`, storeHeaders) + ).data.cart + + expect(cartAfterCollection).toEqual( + expect.objectContaining({ + id: cart.id, + shipping_methods: expect.arrayContaining([ + expect.objectContaining({ + shipping_option_id: shippingOption.id, + }), + ]), + payment_collection: expect.objectContaining({ + amount: 2398, + }), + }) + ) + + let cartAfterExpensiveShipping = ( + await api.post( + `/store/carts/${cart.id}/shipping-methods`, + { option_id: shippingOptionExpensive.id }, + storeHeaders + ) + ).data.cart + + expect(cartAfterExpensiveShipping).toEqual( + expect.objectContaining({ + id: cartAfterExpensiveShipping.id, + shipping_methods: expect.arrayContaining([ + expect.objectContaining({ + shipping_option_id: shippingOptionExpensive.id, + amount: 5000, + }), + ]), + payment_collection: expect.objectContaining({ + amount: 6398, + }), + }) + ) + }) }) it("should add item to cart with tax lines multiple times", async () => { diff --git a/packages/core/core-flows/src/cart/workflows/add-shipping-method-to-cart.ts b/packages/core/core-flows/src/cart/workflows/add-shipping-method-to-cart.ts index 7c0f15282f..b5a5e842e6 100644 --- a/packages/core/core-flows/src/cart/workflows/add-shipping-method-to-cart.ts +++ b/packages/core/core-flows/src/cart/workflows/add-shipping-method-to-cart.ts @@ -17,8 +17,7 @@ import { validateAndReturnShippingMethodsDataStep } from "../steps/validate-ship import { validateCartShippingOptionsPriceStep } from "../steps/validate-shipping-options-price" import { cartFieldsForRefreshSteps } from "../utils/fields" import { listShippingOptionsForCartWithPricingWorkflow } from "./list-shipping-options-for-cart-with-pricing" -import { updateCartPromotionsWorkflow } from "./update-cart-promotions" -import { updateTaxLinesWorkflow } from "./update-tax-lines" +import { refreshCartItemsWorkflow } from "./refresh-cart-items" export interface AddShippingMethodToCartWorkflowInput { cart_id: string @@ -132,9 +131,9 @@ export const addShippingMethodToCartWorkflow = createWorkflow( } ) - const currentShippingMethods = transform({ cart }, ({ cart }) => { - return cart.shipping_methods.map((sm) => sm.id) - }) + const currentShippingMethods = transform({ cart }, ({ cart }) => + cart.shipping_methods.map((sm) => sm.id) + ) parallelize( removeShippingMethodFromCartStep({ @@ -149,16 +148,8 @@ export const addShippingMethodToCartWorkflow = createWorkflow( }) ) - updateTaxLinesWorkflow.runAsStep({ - input: { - cart_id: input.cart_id, - }, - }) - - updateCartPromotionsWorkflow.runAsStep({ - input: { - cart_id: input.cart_id, - }, + refreshCartItemsWorkflow.runAsStep({ + input: { cart_id: cart.id }, }) } ) diff --git a/packages/medusa/src/api/store/carts/query-config.ts b/packages/medusa/src/api/store/carts/query-config.ts index d236c198ec..d66d20e424 100644 --- a/packages/medusa/src/api/store/carts/query-config.ts +++ b/packages/medusa/src/api/store/carts/query-config.ts @@ -28,16 +28,15 @@ export const defaultStoreCartFields = [ "original_shipping_subtotal", "original_shipping_total", "metadata", + "sales_channel_id", "promotions.id", "promotions.code", "promotions.is_automatic", "promotions.application_method.value", "promotions.application_method.type", "promotions.application_method.currency_code", - "items", - "items.thumbnail", - "region", "items.id", + "items.thumbnail", "items.product", "items.product.id", "items.variant", @@ -118,10 +117,7 @@ export const defaultStoreCartFields = [ "region.currency_code", "region.automatic_taxes", "*region.countries", - "sales_channel_id", - - "payment_collection.id", - "payment_collection.amount", + "*payment_collection", "*payment_collection.payment_sessions", ]