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.

Reverse through the provider that moved the money

Damian Legawiec711912f139c1
diff --git a/spree/core/app/workflows/spree/seller_transfers/reverse.rb b/spree/core/app/workflows/spree/seller_transfers/reverse.rb
index edf5e86275..c3fd27ff99 100644
--- a/spree/core/app/workflows/spree/seller_transfers/reverse.rb
+++ b/spree/core/app/workflows/spree/seller_transfers/reverse.rb
@@ -118,8 +118,16 @@ module Spree
failure(reversal, e.message)
end
+ # The provider that made the earning, not whichever one the store uses
+ # now. A marketplace that changes provider still has money sitting with
+ # the old one, and asking the new one to reverse a transfer it never
+ # made leaves the original standing — the seller keeps a refunded sale.
def provider
- @provider ||= order.store.payout_provider_instance
+ @provider ||= begin
+ configured = Spree.payout_providers.find { |candidate| candidate.to_s == @earning.provider }
+
+ (configured || order.store.payout_provider_class).new
+ end
end
end
end
diff --git a/spree/core/db/migrate/20260822000001_create_spree_seller_transfers.rb b/spree/core/db/migrate/20260822000001_create_spree_seller_transfers.rb
index 3f9fe70fb1..d5e3cc2e03 100644
--- a/spree/core/db/migrate/20260822000001_create_spree_seller_transfers.rb
+++ b/spree/core/db/migrate/20260822000001_create_spree_seller_transfers.rb
@@ -82,7 +82,12 @@ class CreateSpreeSellerTransfers < ActiveRecord::Migration[8.1]
where: 'reference IS NOT NULL',
name: 'index_seller_transfers_on_provider_reference'
else
- add_index :spree_seller_transfers, [:provider, :reference],
+ # Unique here too. MySQL and MariaDB have no partial index, but they do
+ # treat every NULL as distinct from every other — so the rows that carry
+ # no reference (the built-in provider writes none) sit alongside each
+ # other freely while a provider's own id stays unrepeatable, which is
+ # what stops a retry recording the same movement twice.
+ add_index :spree_seller_transfers, [:provider, :reference], unique: true,
name: 'index_seller_transfers_on_provider_reference'
end
end
diff --git a/spree/core/db/migrate/20260822000002_create_spree_seller_payouts.rb b/spree/core/db/migrate/20260822000002_create_spree_seller_payouts.rb
index 66274c21b0..4b03a8a9b1 100644
--- a/spree/core/db/migrate/20260822000002_create_spree_seller_payouts.rb
+++ b/spree/core/db/migrate/20260822000002_create_spree_seller_payouts.rb
@@ -36,7 +36,12 @@ class CreateSpreeSellerPayouts < ActiveRecord::Migration[8.1]
where: 'reference IS NOT NULL',
name: 'index_seller_payouts_on_provider_reference'
else
- add_index :spree_seller_payouts, [:provider, :reference],
+ # Unique here too. MySQL and MariaDB have no partial index, but they do
+ # treat every NULL as distinct from every other — so the rows that carry
+ # no reference (the built-in provider writes none) sit alongside each
+ # other freely while a provider's own id stays unrepeatable, which is
+ # what stops a retry recording the same movement twice.
+ add_index :spree_seller_payouts, [:provider, :reference], unique: true,
name: 'index_seller_payouts_on_provider_reference'
end
end
diff --git a/spree/core/spec/workflows/spree/seller_transfers/reverse_spec.rb b/spree/core/spec/workflows/spree/seller_transfers/reverse_spec.rb
index 9d043ad1c0..6f742bc708 100644
--- a/spree/core/spec/workflows/spree/seller_transfers/reverse_spec.rb
+++ b/spree/core/spec/workflows/spree/seller_transfers/reverse_spec.rb
@@ -121,6 +121,37 @@ RSpec.describe Spree::SellerTransfers::Reverse do
end
end
+ # Money sits with whoever moved it. A marketplace that changes provider
+ # still has to reverse the old one's transfer through the old one — asking
+ # the new provider to undo something it never did leaves the original
+ # standing, and the seller keeps a refunded sale.
+ describe 'when the store changed provider after the earning' do
+ let(:previous) do
+ Class.new(Spree::PayoutProvider::Base) do
+ def self.name = 'TestPreviousPayoutProvider'
+
+ def reverse!(seller_transfer)
+ seller_transfer.update!(status: 'completed', reference: 'reversed-by-previous')
+ seller_transfer
+ end
+ end
+ end
+
+ before do
+ stub_const('TestPreviousPayoutProvider', previous)
+ allow(Spree).to receive(:payout_providers).and_return([Spree::PayoutProvider::System, previous])
+ end
+
+ it 'reverses through the provider that made the earning' do
+ create(:seller_transfer, :completed, seller: seller, order: order, amount: 80,
+ provider: 'TestPreviousPayoutProvider')
+
+ reversal = described_class.call(order: order, amount: 30).value
+
+ expect(reversal.reference).to eq('reversed-by-previous')
+ end
+ end
+
# A settlement that has happened is never rewritten: the reversal is simply
# unsettled, so the next sweep nets it off what comes after.
describe 'when the earning was already settled' do
diff --git a/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb b/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb
index 36cbbfeef2..a018f37baa 100644
--- a/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb
+++ b/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb
@@ -88,7 +88,15 @@ module SpreeStripe
# after Stripe withdrew it. Recording the answer keeps the workflows
# that gate money on the column honest without their asking.
stamp = account.payouts_enabled ? (seller.payouts_enabled_at || Time.current) : nil
- seller.update!(payouts_enabled_at: stamp) if seller.payouts_enabled_at != stamp
+
+ if seller.payouts_enabled_at != stamp
+ became_payable = stamp.present?
+ seller.update!(payouts_enabled_at: stamp)
+ # Whatever they earned while we believed they could not be paid. The
+ # webhook normally does this; noticing here covers the case where it
+ # never arrived, which is otherwise money sitting pending forever.
+ Spree::SellerTransfers::ExecutePendingJob.perform_later(seller.id) if became_payable
+ end
account.payouts_enabled
end