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 five ways the ledger paid the wrong amount, or nothing at all

Damian Legawiec61f17660994a
diff --git a/spree/api/app/controllers/spree/api/v3/webhooks/payouts_controller.rb b/spree/api/app/controllers/spree/api/v3/webhooks/payouts_controller.rb
index 3ed6180879..48e46a6e12 100644
--- a/spree/api/app/controllers/spree/api/v3/webhooks/payouts_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/webhooks/payouts_controller.rb
@@ -46,8 +46,13 @@ module Spree
rescue ActiveRecord::RecordNotFound
head :not_found
rescue StandardError => e
+ # 500, unlike the payments endpoint beside this one — that hands
+ # its work to a job, so accepting the delivery is honest. This acts
+ # inline, so answering 200 for work that failed tells the provider
+ # the settlement was recorded when it was not, and nothing will
+ # redeliver it. A retry is the only thing that recovers it.
Rails.error.report(e, source: 'spree.webhooks.payouts')
- head :ok
+ head :internal_server_error
end
end
end
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 f2d8996e60..11f94393c5 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
@@ -48,7 +48,12 @@ module Spree
# — 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)
+ # Failed payouts are excluded: one releases its earnings back to the
+ # next sweep, so counting it as a settlement would block the very
+ # retry it exists to allow — for a whole interval, with the money
+ # sitting owed and unsent.
+ settled_at = seller.seller_payouts.where(currency: currency).
+ where.not(status: 'failed').maximum(:created_at)
return true if settled_at.nil?
settled_at <= interval_ago(seller.resolved_payouts_schedule_interval)
diff --git a/spree/core/app/workflows/spree/seller_payouts/sweep.rb b/spree/core/app/workflows/spree/seller_payouts/sweep.rb
index a325dc09ed..35df6fa48f 100644
--- a/spree/core/app/workflows/spree/seller_payouts/sweep.rb
+++ b/spree/core/app/workflows/spree/seller_payouts/sweep.rb
@@ -12,8 +12,10 @@ module Spree
# transfers with no payout — so a batch can never be swept twice, and a
# re-run after a crash finds nothing left to take.
#
- # A failed send leaves the transfers stamped: the retry re-sends *this*
- # payout rather than sweeping the same earnings into a second one.
+ # A failed send releases them again, because a payout is created here and
+ # nowhere else: transfers left stamped to a failed one would be invisible
+ # to every later sweep while the balance still reported them owed. The
+ # next sweep is the retry.
class Sweep < Spree::Workflow
hooks :validate, :after_sweep
@@ -93,7 +95,11 @@ module Spree
# restated payout can even come to nothing — which must not be sent: a
# provider asked to move zero either errors or moves nothing, and
# either way it is not a settlement.
- discard_empty_payout if @payout.amount <= 0
+ # Re-checked, not just tested against zero: a concurrent sweep taking
+ # some of the rows changes the figure the threshold was weighed
+ # against, and a restated payout can fall below a minimum the original
+ # amount cleared.
+ discard_empty_payout if @payout.amount <= 0 || @payout.amount < seller.resolved_minimum_payout_amount
end
def restate_amount
diff --git a/spree/core/app/workflows/spree/seller_transfers/create.rb b/spree/core/app/workflows/spree/seller_transfers/create.rb
index c7aecb76db..65d130a24c 100644
--- a/spree/core/app/workflows/spree/seller_transfers/create.rb
+++ b/spree/core/app/workflows/spree/seller_transfers/create.rb
@@ -94,7 +94,12 @@ module Spree
# since the marketplace files it (Decision 9).
def earned_amount
base = order.total.to_d
- base -= order.additional_tax_total.to_d if order.seller.tax_remittance == 'platform'
+ # Both halves of the consumer tax, because a market decides which one
+ # it uses: added on top where prices are quoted net, folded into the
+ # price where they are quoted gross. A marketplace that remits the tax
+ # and only subtracts the added half pays a tax-inclusive seller the VAT
+ # as well as paying it to the tax authority.
+ base -= order.tax_total.to_d if order.seller.tax_remittance == 'platform'
[base - order.commission_lines.sum(:total).to_d, 0].max
end
diff --git a/spree/core/app/workflows/spree/seller_transfers/reverse.rb b/spree/core/app/workflows/spree/seller_transfers/reverse.rb
index f3bf67955f..edf5e86275 100644
--- a/spree/core/app/workflows/spree/seller_transfers/reverse.rb
+++ b/spree/core/app/workflows/spree/seller_transfers/reverse.rb
@@ -62,6 +62,11 @@ module Spree
@reversal = write_reversal
halt!(order) if @reversal.nil?
rescue ActiveRecord::RecordNotUnique
+ # Only a refund-keyed reversal can collide, since that index is what
+ # makes it unique. Re-raising anything else keeps the real error
+ # visible rather than replacing it with a lookup that cannot succeed.
+ raise if refund.nil?
+
halt!(Spree::SellerTransfer.reversals_only.find_by!(refund_id: refund.id))
end
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
index 3521b5cf55..b8f9527287 100644
--- 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
@@ -60,6 +60,22 @@ RSpec.describe Spree::SellerPayouts::SweepDueJob do
expect(other_seller.seller_payouts.count).to eq(1)
end
+ # A failed payout released its earnings back to the next sweep, so counting
+ # it as a settlement would block the retry it exists to allow.
+ it 'retries a seller whose last settlement failed' do
+ earn(40)
+ create(:seller_payout, seller: seller, currency: 'USD', status: 'failed', amount: 0)
+
+ expect { described_class.perform_now }.to change { Spree::SellerPayout.where(status: 'pending').count }.by(1)
+ end
+
+ it 'still waits when the last settlement went out' do
+ earn(40)
+ create(:seller_payout, seller: seller, currency: 'USD', status: 'completed', amount: 40)
+
+ expect { described_class.perform_now }.not_to change { Spree::SellerPayout.count }
+ 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)
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 db64a0a9f9..75cd6e21da 100644
--- a/spree/core/spec/workflows/spree/seller_transfers/create_spec.rb
+++ b/spree/core/spec/workflows/spree/seller_transfers/create_spec.rb
@@ -6,7 +6,7 @@ RSpec.describe Spree::SellerTransfers::Create do
# A shipped seller order worth 100, on which the marketplace charged 12
# including the VAT it charges the seller on its fee.
- def shipped_order(total: 100, commission: 12, tax_remittance: 'seller', tax: 0)
+ def shipped_order(total: 100, commission: 12, tax_remittance: 'seller', tax: 0, included_tax: 0)
order = create(:order, store: store, seller: seller)
line_item = create(:line_item, order: order)
create(:fulfillment, order: order, cart: nil, status: 'fulfilled')
@@ -20,8 +20,8 @@ RSpec.describe Spree::SellerTransfers::Create do
# Written last and directly: the workflow reads the order's own totals, and
# letting recalculation derive them from the factory's line items would be
# testing the totals workflow rather than the ledger.
- order.update_columns(total: total, additional_tax_total: tax, status: 'placed',
- completed_at: Time.current)
+ order.update_columns(total: total, additional_tax_total: tax, included_tax_total: included_tax,
+ status: 'placed', completed_at: Time.current)
order.reload
end
@@ -48,6 +48,17 @@ RSpec.describe Spree::SellerTransfers::Create do
expect(result.value.amount).to eq(68)
end
+ # A market quoting gross prices keeps the tax inside the order total
+ # rather than adding it on top, so subtracting only the added half would
+ # pay the seller VAT the marketplace is about to remit.
+ it 'withholds tax that was included in the price, not added to it' do
+ result = described_class.call(
+ order: shipped_order(total: 100, commission: 12, tax_remittance: 'platform', included_tax: 20)
+ )
+
+ expect(result.value.amount).to eq(68)
+ end
+
it 'leaves consumer tax with a seller who remits it themselves' do
result = described_class.call(order: shipped_order(total: 100, commission: 12, tax: 20))