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.

Separate a transfer nobody can account for from one that will be retried

Damian Legawiec63b3d7527878
diff --git a/spree/api/app/controllers/spree/api/v3/admin/seller_payouts_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/seller_payouts_controller.rb
index 521480ad34..8e23f907bb 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/seller_payouts_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/seller_payouts_controller.rb
@@ -61,7 +61,15 @@ module Spree
# each row for its own would be a query per settlement, and a
# counter column cannot work here because the sweep claims transfers
# with `update_all`, which no callback sees.
+ #
+ # Only for the listing. Every action shares this method, and building
+ # the count needs `collection` — so a member action would run a whole
+ # paginated index query to serialize its one record, then read that
+ # record's count out of a page it may not even be on. The serializer
+ # asks a single payout for its own count, which is one query.
def serializer_params
+ return super unless action_name == 'index'
+
super.merge(transfer_counts: transfer_counts)
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/seller_payouts_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/seller_payouts_controller_spec.rb
index 9b3f954e46..40118ba55c 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/seller_payouts_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/seller_payouts_controller_spec.rb
@@ -57,6 +57,26 @@ RSpec.describe Spree::Api::V3::Admin::SellerPayoutsController, type: :controller
expect(response).to have_http_status(:not_found)
end
+
+ it 'still reports how many earnings the settlement covers' do
+ create(:seller_transfer, :completed, seller: seller, payout: payout)
+ create(:seller_transfer, :completed, seller: seller, payout: payout)
+
+ patch :complete, params: { id: payout.prefixed_id }, as: :json
+
+ expect(json_response['transfers_count']).to eq(2)
+ end
+
+ # The page-wide count belongs to the listing, and building it runs the
+ # whole paginated index query. Doing that to serialize one record is waste
+ # on every call, and answers from a page this settlement need not be on.
+ it 'does not run the listing query to serialize one settlement' do
+ expect(controller).not_to receive(:collection)
+
+ patch :complete, params: { id: payout.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ end
end
describe 'writes' do
diff --git a/spree/core/app/models/spree/seller_transfer.rb b/spree/core/app/models/spree/seller_transfer.rb
index d04de73886..c3d21d72e1 100644
--- a/spree/core/app/models/spree/seller_transfer.rb
+++ b/spree/core/app/models/spree/seller_transfer.rb
@@ -59,7 +59,10 @@ module Spree
# graph (docs/plans/6.0-service-workflows.md).
#
include Spree::HasStatus
- has_status :pending, :processing, :completed, :failed, default: :pending
+ # `processing` is an attempt that failed and will be tried again;
+ # `unresolved` is one whose outcome the provider could not report, which
+ # nothing may retry until a person establishes what happened.
+ has_status :pending, :processing, :completed, :failed, :unresolved, default: :pending
#
# Scopes
diff --git a/spree/core/app/workflows/spree/seller_transfers/create.rb b/spree/core/app/workflows/spree/seller_transfers/create.rb
index 65d130a24c..42f7401916 100644
--- a/spree/core/app/workflows/spree/seller_transfers/create.rb
+++ b/spree/core/app/workflows/spree/seller_transfers/create.rb
@@ -77,9 +77,19 @@ module Spree
return unless order.seller.payouts_enabled?
provider.transfer!(seller_transfer)
+ rescue Spree::Core::AmbiguousGatewayError => e
+ # Nobody knows whether the money moved, so nothing may send it again:
+ # the retry job takes `pending` and `processing` rows, and an
+ # idempotency key only covers a retry while the provider keeps its
+ # record. Parked where no automatic attempt reaches it, for an
+ # operator to resolve against the provider's own books.
+ seller_transfer.update!(status: 'unresolved')
+ Rails.error.report(e, handled: true, context: { seller_transfer_id: seller_transfer.id },
+ source: 'spree.core')
+ failure(seller_transfer, e.message)
rescue StandardError => e
- # The money may or may not have moved — that is precisely what an
- # operator has to resolve, so the row parks rather than pretending.
+ # A definite refusal — the money did not move, so the earning is still
+ # owed and the retry job picks it up.
seller_transfer.update!(status: 'processing')
Rails.error.report(e, handled: true, context: { seller_transfer_id: seller_transfer.id },
source: 'spree.core')
diff --git a/spree/core/app/workflows/spree/seller_transfers/reverse.rb b/spree/core/app/workflows/spree/seller_transfers/reverse.rb
index 8cbd0e8318..e1814dcb99 100644
--- a/spree/core/app/workflows/spree/seller_transfers/reverse.rb
+++ b/spree/core/app/workflows/spree/seller_transfers/reverse.rb
@@ -112,6 +112,13 @@ module Spree
def execute_reversal
provider.reverse!(reversal)
+ rescue Spree::Core::AmbiguousGatewayError => e
+ # Whether the clawback happened is the provider's to say. Recorded as
+ # such rather than as a refusal, so an operator reconciling knows which
+ # rows are questions and which are simply owed.
+ reversal.update!(status: 'unresolved')
+ Rails.error.report(e, handled: true, context: { seller_transfer_id: reversal.id }, source: 'spree.core')
+ failure(reversal, e.message)
rescue StandardError => e
reversal.update!(status: 'processing')
Rails.error.report(e, handled: true, context: { seller_transfer_id: reversal.id }, source: 'spree.core')
diff --git a/spree/core/spec/workflows/spree/seller_transfers/create_spec.rb b/spree/core/spec/workflows/spree/seller_transfers/create_spec.rb
index 75cd6e21da..faa82baabe 100644
--- a/spree/core/spec/workflows/spree/seller_transfers/create_spec.rb
+++ b/spree/core/spec/workflows/spree/seller_transfers/create_spec.rb
@@ -140,4 +140,53 @@ RSpec.describe Spree::SellerTransfers::Create do
expect(seller.balance('USD')).to eq(88)
end
end
+
+ # The two ways a provider call can go wrong want opposite reactions, so they
+ # must not share a status: one is owed and should be tried again, the other
+ # may already have been sent and must not be.
+ describe 'when the provider call goes wrong' do
+ before { seller.update!(payouts_enabled_at: Time.current) }
+
+ context 'and the provider refused outright' do
+ before do
+ allow_any_instance_of(Spree::PayoutProvider::System).to receive(:transfer!).
+ and_raise(StandardError, 'gateway down')
+ end
+
+ it 'leaves the earning for the retry job to send' do
+ result = described_class.call(order: shipped_order)
+
+ expect(result.value.reload).to be_processing
+ end
+ end
+
+ context 'and nobody knows whether the money moved' do
+ before do
+ allow_any_instance_of(Spree::PayoutProvider::System).to receive(:transfer!).
+ and_raise(Spree::Core::AmbiguousGatewayError, 'connection timed out')
+ end
+
+ it 'parks it where no automatic attempt will send it again' do
+ result = described_class.call(order: shipped_order)
+
+ expect(result.value.reload).to be_unresolved
+ end
+
+ # The retry job takes pending and processing rows, and an idempotency
+ # key stops a duplicate only while the provider still holds the record.
+ it 'is not picked up by the job that retries earnings' do
+ described_class.call(order: shipped_order)
+
+ retryable = seller.seller_transfers.earnings.where(status: %w[pending processing], payout_id: nil)
+
+ expect(retryable).to be_empty
+ end
+
+ it 'stays out of the balance until somebody establishes what happened' do
+ described_class.call(order: shipped_order)
+
+ expect(seller.balance('USD')).to eq(0)
+ end
+ end
+ end
end