Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Settle a refund and release its consignment together

Damian Legawiec1b1b673a4731
diff --git a/spree/core/app/services/spree/shipping_labels/confirm_refund.rb b/spree/core/app/services/spree/shipping_labels/confirm_refund.rb
index 2b213ace60..e9178cd3dd 100644
--- a/spree/core/app/services/spree/shipping_labels/confirm_refund.rb
+++ b/spree/core/app/services/spree/shipping_labels/confirm_refund.rb
@@ -22,14 +22,23 @@ module Spree
# 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)
+ settled = ApplicationRecord.transaction do
+ claimed = Spree::ShippingLabel.
+ where(id: shipping_label.id, status: 'refund_requested').
+ update_all(status: 'refunded', refunded_at: refunded_at || Time.current, updated_at: Time.current)
+
+ # The delivery goes with the settle or neither happens: a refunded
+ # label still holding the consignment it minted is the state a
+ # retry cannot detect, since the status already reads refunded.
+ shipping_label.reload.release_unmoved_delivery if claimed.positive?
+ claimed
+ end
return success(shipping_label.reload) if settled.zero?
- shipping_label.reload
- shipping_label.release_unmoved_delivery
+ # Outside the transaction deliberately — subscribers do their own
+ # work, and a slow or failing one must not roll back a refund the
+ # carrier has already made.
shipping_label.publish_event('shipping_label.refunded')
success(shipping_label)
end