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.

Sweep sellers store by store, and ask Stripe for every endpoint

Damian Legawiec1f58e3c94c3c
diff --git a/spree/core/app/jobs/spree/seller_payouts/sweep_due_job.rb b/spree/core/app/jobs/spree/seller_payouts/sweep_due_job.rb
index 9bee370846..f2d8996e60 100644
--- a/spree/core/app/jobs/spree/seller_payouts/sweep_due_job.rb
+++ b/spree/core/app/jobs/spree/seller_payouts/sweep_due_job.rb
@@ -14,21 +14,29 @@ module Spree
queue_as Spree.queues.payouts
def perform
- # Preloaded because the schedule falls back to the store's default, so
- # every seller would otherwise reload its own.
- Spree::Seller.where(status: 'approved').includes(:store).find_each do |seller|
- next if seller.resolved_payouts_schedule_interval == 'manual'
-
- currencies_owed(seller).each do |currency|
- next unless due?(seller, currency)
-
- Spree.seller_payout_sweep_workflow.call(seller: seller, currency: currency)
- end
+ # Store by store, so each seller is reached through the store that owns
+ # them — which is what makes the (store, status) index usable, and what
+ # the schedule falls back to anyway.
+ Spree::Store.find_each do |store|
+ store.sellers.approved.find_each { |seller| sweep_seller(seller) }
end
end
private
+ # One seller's failure must not end the run for everyone behind them.
+ def sweep_seller(seller)
+ return if seller.resolved_payouts_schedule_interval == 'manual'
+
+ currencies_owed(seller).each do |currency|
+ next unless due?(seller, currency)
+
+ Spree.seller_payout_sweep_workflow.call(seller: seller, currency: currency)
+ end
+ rescue StandardError => e
+ Rails.error.report(e, handled: true, context: { seller_id: seller.id }, source: 'spree.core')
+ end
+
# Whether enough time has passed since this seller was last settled in
# this currency. A seller who has never been paid is due as soon as they
# have earned anything — their first settlement should not wait a whole
diff --git a/spree/core/spec/jobs/spree/seller_payouts/sweep_due_job_spec.rb b/spree/core/spec/jobs/spree/seller_payouts/sweep_due_job_spec.rb
new file mode 100644
index 0000000000..3521b5cf55
--- /dev/null
+++ b/spree/core/spec/jobs/spree/seller_payouts/sweep_due_job_spec.rb
@@ -0,0 +1,85 @@
+require 'spec_helper'
+
+RSpec.describe Spree::SellerPayouts::SweepDueJob do
+ let(:store) { @default_store }
+ let(:seller) { create(:seller, :approved, store: store) }
+
+ def earn(amount, seller: nil, currency: 'USD')
+ owner = seller || self.seller
+ create(:seller_transfer, :completed, seller: owner, currency: currency, amount: amount,
+ order: create(:order, store: owner.store, seller: owner, currency: currency))
+ end
+
+ it 'settles a seller who is owed money' do
+ earn(40)
+
+ expect { described_class.perform_now }.to change { Spree::SellerPayout.count }.by(1)
+ end
+
+ it 'leaves a seller who has earned nothing' do
+ seller
+
+ expect { described_class.perform_now }.not_to change { Spree::SellerPayout.count }
+ end
+
+ # The operator settles these by hand, so nothing schedules them.
+ it 'never settles a seller on a manual schedule' do
+ seller.update!(payouts_schedule_interval: 'manual')
+ earn(40)
+
+ expect { described_class.perform_now }.not_to change { Spree::SellerPayout.count }
+ end
+
+ it 'ignores a seller who is not approved' do
+ pending_seller = create(:seller, store: store, status: 'pending')
+ earn(40, seller: pending_seller)
+
+ expect { described_class.perform_now }.not_to change { Spree::SellerPayout.count }
+ end
+
+ it 'settles each currency a seller is owed in separately' do
+ earn(40)
+ earn(30, currency: 'EUR')
+
+ described_class.perform_now
+
+ expect(seller.seller_payouts.pluck(:currency, :amount)).to contain_exactly(['USD', 40], ['EUR', 30])
+ end
+
+ # Every marketplace on the installation is swept, each seller reached
+ # through the store that owns them.
+ it 'settles sellers across every store' do
+ other_store = create(:store)
+ other_seller = create(:seller, :approved, store: other_store)
+ earn(40)
+ earn(30, seller: other_seller)
+
+ described_class.perform_now
+
+ expect(seller.seller_payouts.count).to eq(1)
+ expect(other_seller.seller_payouts.count).to eq(1)
+ end
+
+ # One stuck seller must not end the run for everyone behind them.
+ it 'carries on past a seller whose settlement fails' do
+ other_seller = create(:seller, :approved, store: store)
+ earn(40)
+ earn(30, seller: other_seller)
+ allow(Spree).to receive(:seller_payout_sweep_workflow).and_wrap_original do |original|
+ workflow = original.call
+ failing = seller
+ Class.new do
+ define_singleton_method(:call) do |seller:, currency:|
+ raise StandardError, 'provider down' if seller.id == failing.id
+
+ workflow.call(seller: seller, currency: currency)
+ end
+ end
+ end
+
+ described_class.perform_now
+
+ expect(seller.seller_payouts.count).to eq(0)
+ expect(other_seller.seller_payouts.count).to eq(1)
+ end
+end
diff --git a/spree/providers/stripe/app/services/spree_stripe/create_gateway_webhooks.rb b/spree/providers/stripe/app/services/spree_stripe/create_gateway_webhooks.rb
index 16d9ab6e23..1a0ba75fe0 100644
--- a/spree/providers/stripe/app/services/spree_stripe/create_gateway_webhooks.rb
+++ b/spree/providers/stripe/app/services/spree_stripe/create_gateway_webhooks.rb
@@ -17,7 +17,17 @@ module SpreeStripe
return if webhook_url.blank?
api_options = { api_key: payment_method.preferred_secret_key }
- existing = find_webhook(Stripe::WebhookEndpoint.list({}, api_options)[:data], webhook_url, events)
+ # Asked for in full rather than taking Stripe's default page of ten: an
+ # account holds at most sixteen endpoints, so one call sees them all, and
+ # a marketplace registers two per gateway. Missing our own is not a
+ # harmless miss — Stripe does not treat the URL as unique, so the caller
+ # would create a second endpoint beside it and every event would arrive
+ # twice, half of them signed with a secret we do not hold.
+ existing = find_webhook(
+ Stripe::WebhookEndpoint.list({ limit: 100 }, api_options)[:data],
+ webhook_url,
+ events
+ )
# Stripe only returns the signing secret when the endpoint is created, so a
# pre-existing endpoint whose secret we do not hold has to be replaced.
diff --git a/spree/providers/stripe/spec/services/spree_stripe/create_gateway_webhooks_spec.rb b/spree/providers/stripe/spec/services/spree_stripe/create_gateway_webhooks_spec.rb
index 5f6d17985f..60def2a70b 100644
--- a/spree/providers/stripe/spec/services/spree_stripe/create_gateway_webhooks_spec.rb
+++ b/spree/providers/stripe/spec/services/spree_stripe/create_gateway_webhooks_spec.rb
@@ -7,7 +7,7 @@ RSpec.describe SpreeStripe::CreateGatewayWebhooks do
let(:payment_method) { create(:stripe_gateway, store: store) }
let(:webhook_url) { 'https://spreecommerce.org/stripe/' }
- let(:stripe_webhooks) { Stripe::WebhookEndpoint.list({}, payment_method.api_options)[:data] }
+ let(:stripe_webhooks) { Stripe::WebhookEndpoint.list({ limit: 100 }, payment_method.api_options)[:data] }
let(:spree_webhook_endpoints) { stripe_webhooks.select { |webhook| webhook[:url] == webhook_url } }
let(:stripe_webhook) { spree_webhook_endpoints.first }
diff --git a/spree/providers/stripe/spec/vcr/create_another_gateway_webhooks.yml b/spree/providers/stripe/spec/vcr/create_another_gateway_webhooks.yml
index 9b883baf08..dd158951d2 100644
--- a/spree/providers/stripe/spec/vcr/create_another_gateway_webhooks.yml
+++ b/spree/providers/stripe/spec/vcr/create_another_gateway_webhooks.yml
@@ -2,7 +2,7 @@
http_interactions:
- request:
method: get
- uri: https://api.stripe.com/v1/webhook_endpoints
+ uri: https://api.stripe.com/v1/webhook_endpoints?limit=100
body:
encoding: US-ASCII
string: ''
@@ -288,7 +288,7 @@ http_interactions:
recorded_at: Fri, 08 May 2026 15:16:48 GMT
- request:
method: get
- uri: https://api.stripe.com/v1/webhook_endpoints
+ uri: https://api.stripe.com/v1/webhook_endpoints?limit=100
body:
encoding: US-ASCII
string: ''
diff --git a/spree/providers/stripe/spec/vcr/create_gateway_webhooks.yml b/spree/providers/stripe/spec/vcr/create_gateway_webhooks.yml
index 6bb63a5182..a2b4e3f723 100644
--- a/spree/providers/stripe/spec/vcr/create_gateway_webhooks.yml
+++ b/spree/providers/stripe/spec/vcr/create_gateway_webhooks.yml
@@ -2,7 +2,7 @@
http_interactions:
- request:
method: get
- uri: https://api.stripe.com/v1/webhook_endpoints
+ uri: https://api.stripe.com/v1/webhook_endpoints?limit=100
body:
encoding: US-ASCII
string: ''
@@ -269,7 +269,7 @@ http_interactions:
recorded_at: Fri, 08 May 2026 15:16:46 GMT
- request:
method: get
- uri: https://api.stripe.com/v1/webhook_endpoints
+ uri: https://api.stripe.com/v1/webhook_endpoints?limit=100
body:
encoding: US-ASCII
string: ''
diff --git a/spree/providers/stripe/spec/vcr/existing_gateway_webhooks.yml b/spree/providers/stripe/spec/vcr/existing_gateway_webhooks.yml
index 3779e4e676..962be896b9 100644
--- a/spree/providers/stripe/spec/vcr/existing_gateway_webhooks.yml
+++ b/spree/providers/stripe/spec/vcr/existing_gateway_webhooks.yml
@@ -2,7 +2,7 @@
http_interactions:
- request:
method: get
- uri: https://api.stripe.com/v1/webhook_endpoints
+ uri: https://api.stripe.com/v1/webhook_endpoints?limit=100
body:
encoding: US-ASCII
string: ''
@@ -183,7 +183,7 @@ http_interactions:
recorded_at: Fri, 08 May 2026 15:16:47 GMT
- request:
method: get
- uri: https://api.stripe.com/v1/webhook_endpoints
+ uri: https://api.stripe.com/v1/webhook_endpoints?limit=100
body:
encoding: US-ASCII
string: ''