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.

Refuse a settlement reference that already belongs to another one

Damian Legawiecf533bb4eb1c5
diff --git a/spree/core/app/workflows/spree/seller_payouts/complete.rb b/spree/core/app/workflows/spree/seller_payouts/complete.rb
index 48c08128fa..8d223c8fe4 100644
--- a/spree/core/app/workflows/spree/seller_payouts/complete.rb
+++ b/spree/core/app/workflows/spree/seller_payouts/complete.rb
@@ -54,6 +54,17 @@ module Spree
halt!(seller_payout.reload) if claimed.zero?
seller_payout.reload
+ rescue ActiveRecord::RecordNotUnique
+ # The reference is already on another settlement, so this movement is
+ # recorded and completing this row would record it twice. Reachable
+ # through a settlement whose outcome was never established: it holds no
+ # reference of its own, so it is matched by the id we sent rather than
+ # the provider's, and a redelivered event can name one already filed.
+ #
+ # The row is left where it is for a person to reconcile — marking it
+ # completed without the reference would claim a movement that belongs
+ # to a different settlement.
+ halt!(seller_payout.reload)
end
end
end
diff --git a/spree/core/spec/workflows/spree/seller_payouts/complete_spec.rb b/spree/core/spec/workflows/spree/seller_payouts/complete_spec.rb
index 37c2ffedc3..91a2233bfe 100644
--- a/spree/core/spec/workflows/spree/seller_payouts/complete_spec.rb
+++ b/spree/core/spec/workflows/spree/seller_payouts/complete_spec.rb
@@ -54,4 +54,35 @@ RSpec.describe Spree::SellerPayouts::Complete do
expect(payout.reload.reference).to eq('BACS-9912')
end
end
+
+ # A settlement whose outcome was never established holds no reference of its
+ # own, so a webhook matches it by the id Spree sent rather than the
+ # provider's. A redelivered event can therefore name a reference already
+ # filed against a different settlement.
+ describe 'when the reference belongs to another settlement' do
+ let!(:already_filed) do
+ create(:seller_payout, seller: seller, currency: 'USD', reference: 'po_X', status: 'completed')
+ end
+ let(:stuck) do
+ create(:seller_payout, seller: seller, currency: 'USD', reference: nil, status: 'unresolved')
+ end
+
+ it 'does not blow up on the unique index' do
+ expect { described_class.call(seller_payout: stuck, reference: 'po_X') }.not_to raise_error
+ end
+
+ # Completing it would record one movement as two settlements.
+ it 'leaves it for a person to reconcile' do
+ described_class.call(seller_payout: stuck, reference: 'po_X')
+
+ expect(stuck.reload).to be_unresolved
+ expect(stuck.reference).to be_nil
+ end
+
+ it 'leaves the settlement that already holds the reference alone' do
+ described_class.call(seller_payout: stuck, reference: 'po_X')
+
+ expect(already_filed.reload.reference).to eq('po_X')
+ end
+ end
end