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.

Fix what a review of the ledger turned up

Damian Legawieca09934faf786
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 2e218fe992..9bee370846 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,11 +14,14 @@ module Spree
queue_as Spree.queues.payouts
def perform
- Spree::Seller.where(status: 'approved').find_each do |seller|
+ # 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'
- next unless due?(seller)
currencies_owed(seller).each do |currency|
+ next unless due?(seller, currency)
+
Spree.seller_payout_sweep_workflow.call(seller: seller, currency: currency)
end
end
@@ -26,14 +29,21 @@ module Spree
private
- # Whether enough time has passed since this seller was last settled.
- # A seller who has never been paid is due as soon as they have earned
- # anything — their first settlement should not wait a whole period.
- def due?(seller)
- last = seller.seller_payouts.order(:created_at).last
- return true if last.nil?
+ # 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
+ # period.
+ #
+ # Asked per currency, because settlements are made per currency. Reading
+ # the seller's most recent payout across all of them would let an
+ # out-of-band settlement in one currency reset the clock for every other
+ # — a monthly seller paid early in dollars would have their euro balance
+ # skipped for another month.
+ def due?(seller, currency)
+ settled_at = seller.seller_payouts.where(currency: currency).maximum(:created_at)
+ return true if settled_at.nil?
- last.created_at <= interval_ago(seller.resolved_payouts_schedule_interval)
+ settled_at <= interval_ago(seller.resolved_payouts_schedule_interval)
end
def interval_ago(interval)
diff --git a/spree/core/app/models/spree/payout_provider/base.rb b/spree/core/app/models/spree/payout_provider/base.rb
index 010673b80e..571e8b8412 100644
--- a/spree/core/app/models/spree/payout_provider/base.rb
+++ b/spree/core/app/models/spree/payout_provider/base.rb
@@ -29,6 +29,18 @@ module Spree
name.demodulize.titleize
end
+ # What a ledger row records as having moved its money.
+ #
+ # The full class name rather than its last segment: every provider gem
+ # calls its class `PayoutProvider`, so the leaf alone identifies nothing
+ # and would make two gems' rows indistinguishable — including to the
+ # unique index on (provider, reference) that guards retries.
+ #
+ # @return [String]
+ def self.provider_key
+ name
+ end
+
# Whether this provider can be selected for a store — an external one
# overrides it to require connected credentials.
#
diff --git a/spree/core/app/workflows/spree/seller_transfers/reverse.rb b/spree/core/app/workflows/spree/seller_transfers/reverse.rb
index 2f374a887f..f3bf67955f 100644
--- a/spree/core/app/workflows/spree/seller_transfers/reverse.rb
+++ b/spree/core/app/workflows/spree/seller_transfers/reverse.rb
@@ -65,11 +65,28 @@ module Spree
halt!(Spree::SellerTransfer.reversals_only.find_by!(refund_id: refund.id))
end
+ # The seller's share of a refunded amount.
+ #
+ # A refund is the customer's gross figure — it carries the tax and the
+ # marketplace's commission, while the seller only ever received their net
+ # cut. Taking the gross back would charge them the commission on goods
+ # that came back, so it is scaled by what the order earned them against
+ # what the customer paid. A full refund still nets to the whole earning.
+ def seller_share_of(refunded)
+ paid = order.total.to_d
+ return refunded.to_d.abs if paid.zero?
+
+ Spree::Money::Rounding.quantize(
+ refunded.to_d.abs * (@earning.amount / paid),
+ Spree::Money::Rounding.precision(@earning.currency)
+ )
+ end
+
# Nil when there is nothing left to take back, which the caller turns
# into a halt outside the transaction — `halt!` refuses to run inside one.
def write_reversal
@earning.with_lock do
- bounded = [amount.to_d.abs, @earning.reversible_amount].min
+ bounded = [seller_share_of(amount), @earning.reversible_amount].min
next nil if bounded <= 0
Spree::SellerTransfer.create!(
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 7d8d45a7c4..4c954872e5 100644
--- a/spree/core/db/migrate/20260822000001_create_spree_seller_transfers.rb
+++ b/spree/core/db/migrate/20260822000001_create_spree_seller_transfers.rb
@@ -54,13 +54,19 @@ class CreateSpreeSellerTransfers < ActiveRecord::Migration[8.1]
where: "kind = 'earning'",
name: 'index_seller_transfers_on_order_earning'
else
- # MySQL has no partial index. Indexing the id together with the row's own
- # primary key for every other kind gives the same guarantee: earnings
- # collide on the order, reversals never collide at all.
+ # MySQL and MariaDB have no partial index, and functional indexes are
+ # MySQL-only — MariaDB rejects the expression outright. A stored
+ # generated column is the one spelling both accept: it holds the order
+ # for an earning and the row's own id otherwise, so earnings collide on
+ # the order while reversals never collide at all.
execute <<~SQL.squish
- CREATE UNIQUE INDEX index_seller_transfers_on_order_earning
- ON spree_seller_transfers(order_id, (CASE WHEN kind = 'earning' THEN 0 ELSE id END))
+ ALTER TABLE spree_seller_transfers
+ ADD COLUMN earning_key BIGINT
+ AS (CASE WHEN kind = 'earning' THEN order_id ELSE -id END) STORED
SQL
+
+ add_index :spree_seller_transfers, :earning_key, unique: true,
+ name: 'index_seller_transfers_on_order_earning'
end
# A provider's own id for the movement, unique where one exists: a retry
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 e38afb4c89..9d043ad1c0 100644
--- a/spree/core/spec/workflows/spree/seller_transfers/reverse_spec.rb
+++ b/spree/core/spec/workflows/spree/seller_transfers/reverse_spec.rb
@@ -16,15 +16,24 @@ RSpec.describe Spree::SellerTransfers::Reverse do
result = described_class.call(order: order, amount: 30)
expect(result).to be_success
- expect(result.value.amount).to eq(-30)
expect(earning.reload.amount).to eq(80)
end
+ # A refund is the customer's gross figure; the seller only ever received
+ # their net cut, so taking the gross back would charge them the
+ # marketplace's commission on goods that came back.
+ it 'takes back only the seller’s share of what was refunded' do
+ earn
+
+ # 30 refunded on a 100 order that earned the seller 80.
+ expect(described_class.call(order: order, amount: 30).value.amount).to eq(-24)
+ end
+
it 'leaves the seller with what is left' do
earn
described_class.call(order: order, amount: 30)
- expect(seller.balance('USD')).to eq(50)
+ expect(seller.balance('USD')).to eq(56)
end
it 'links the reversal to what it reverses' do
@@ -43,7 +52,8 @@ RSpec.describe Spree::SellerTransfers::Reverse do
it 'takes nothing once the earning is fully reversed' do
earn(80)
- described_class.call(order: order, amount: 80)
+ # The whole order refunded takes the whole earning back.
+ described_class.call(order: order, amount: 100)
expect { described_class.call(order: order, amount: 20) }.
not_to change { Spree::SellerTransfer.count }
@@ -69,7 +79,7 @@ RSpec.describe Spree::SellerTransfers::Reverse do
expect { described_class.call(order: order, amount: 30, refund: refund) }.
not_to change { Spree::SellerTransfer.count }
- expect(seller.balance('USD')).to eq(50)
+ expect(seller.balance('USD')).to eq(56)
end
it 'answers with the reversal that was already written' do
@@ -104,6 +114,8 @@ RSpec.describe Spree::SellerTransfers::Reverse do
described_class.call(order: order, amount: 60, refund: create(:refund, amount: 30))
described_class.call(order: order, amount: 60, refund: create(:refund, amount: 30))
+ # Two 60 refunds on a 100 order: 48 each against an 80 earning, floored
+ # at what was credited.
expect(seller.balance('USD')).to eq(0)
expect(Spree::SellerTransfer.reversals_only.sum(:amount)).to eq(-80)
end