Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Settle a refund once when the carrier confirms twice, and follow the card's copy
Damian Legawiec49db854479c9
diff --git a/packages/dashboard/e2e/order-fulfillments.spec.ts b/packages/dashboard/e2e/order-fulfillments.spec.tsindex 42548bb867..f2dc7ec370 100644--- a/packages/dashboard/e2e/order-fulfillments.spec.ts+++ b/packages/dashboard/e2e/order-fulfillments.spec.ts@@ -44,7 +44,7 @@ test.describe('order fulfillments', () => {await createDraftOrder(page, creds.store_id)const card = fulfillmentsCard(page)- await expect(card.getByText(/unfulfilled \(\d+\)/i)).toBeVisible({ timeout: 15_000 })+ await expect(card.getByText(/\d+ items? still to fulfill/i)).toBeVisible({ timeout: 15_000 })await expect(card.getByText(new RegExp(FIXTURE_PROMO_PRODUCT, 'i')).first()).toBeVisible()})@@ -56,7 +56,7 @@ test.describe('order fulfillments', () => {await createDraftOrder(page, creds.store_id)const card = fulfillmentsCard(page)- await expect(card.getByText(/unfulfilled \(\d+\)/i)).toBeVisible({ timeout: 15_000 })+ await expect(card.getByText(/\d+ items? still to fulfill/i)).toBeVisible({ timeout: 15_000 })await expect(card.getByRole('button', { name: /^add fulfillment$/i })).toHaveCount(0)})@@ -68,7 +68,7 @@ test.describe('order fulfillments', () => {await createDraftOrder(page, creds.store_id)const card = fulfillmentsCard(page)- await expect(card.getByText(/unfulfilled \(\d+\)/i)).toBeVisible({ timeout: 15_000 })+ await expect(card.getByText(/\d+ items? still to fulfill/i)).toBeVisible({ timeout: 15_000 })await expect(card.getByText(new RegExp(FIXTURE_PROMO_PRODUCT, 'i')).first()).toBeVisible()await expect(card.getByRole('button', { name: /actions for/i })).toHaveCount(0)})@@ -81,7 +81,7 @@ test.describe('order fulfillments', () => {await createDraftOrder(page, creds.store_id)const card = fulfillmentsCard(page)- await expect(card.getByText(/unfulfilled \(\d+\)/i)).toBeVisible({ timeout: 15_000 })+ await expect(card.getByText(/\d+ items? still to fulfill/i)).toBeVisible({ timeout: 15_000 })await expect(card.getByRole('button', { name: /add tracking/i })).toHaveCount(0)await expect(card.getByRole('button', { name: /buy label/i })).toHaveCount(0)await expect(card.getByRole('button', { name: /print label/i })).toHaveCount(0)diff --git a/spree/core/app/services/spree/shipping_labels/confirm_refund.rb b/spree/core/app/services/spree/shipping_labels/confirm_refund.rbindex a4daecd005..2b213ace60 100644--- a/spree/core/app/services/spree/shipping_labels/confirm_refund.rb+++ b/spree/core/app/services/spree/shipping_labels/confirm_refund.rb@@ -18,11 +18,18 @@ module Spreereturn failure(shipping_label)end- ApplicationRecord.transaction do- shipping_label.update!(status: 'refunded', refunded_at: refunded_at || Time.current)- shipping_label.release_unmoved_delivery- end+ # Carriers retry webhooks, so two confirmations can arrive at once and+ # both read `refund_requested`. The settle is a conditional UPDATE:+ # exactly one call moves the row, and only that one releases the+ # delivery and announces the refund.+ settled = Spree::ShippingLabel.+ where(id: shipping_label.id, status: 'refund_requested').+ update_all(status: 'refunded', refunded_at: refunded_at || Time.current, updated_at: Time.current)++ return success(shipping_label.reload) if settled.zero?+ shipping_label.reload+ shipping_label.release_unmoved_deliveryshipping_label.publish_event('shipping_label.refunded')success(shipping_label)enddiff --git a/spree/core/spec/services/spree/shipping_labels/confirm_refund_spec.rb b/spree/core/spec/services/spree/shipping_labels/confirm_refund_spec.rbindex 0525ccb9be..8cba07ba4b 100644--- a/spree/core/spec/services/spree/shipping_labels/confirm_refund_spec.rb+++ b/spree/core/spec/services/spree/shipping_labels/confirm_refund_spec.rb@@ -42,4 +42,18 @@ RSpec.describe Spree::ShippingLabels::ConfirmRefund doexpect(label.reload.delivery).to be_presentend++ # Carriers retry webhooks. Two confirmations for the same label must settle+ # it once and announce it once, or downstream handlers run twice.+ it 'settles once when the carrier confirms twice' do+ label = create(:shipping_label, owner: fulfillment, status: 'refund_requested')+ second_view = Spree::ShippingLabel.find(label.id)++ expect(label).to receive(:publish_event).with('shipping_label.refunded').once.and_call_original+ expect(second_view).not_to receive(:publish_event)++ expect(service.call(shipping_label: label)).to be_success+ expect(service.call(shipping_label: second_view)).to be_success+ expect(label.reload).to be_refunded+ endend