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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix three things review found in the payout ledger
Damian Legawiec2df5c82ff8e1
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.rbindex 4d9727ed02..521480ad34 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@@ -56,6 +56,18 @@ module Spreedef collection_includes[:seller]end++ # One grouped count for the page, handed to the serializer — asking+ # 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.+ def serializer_params+ super.merge(transfer_counts: transfer_counts)+ end++ def transfer_counts+ @transfer_counts ||= Spree::SellerTransfer.where(payout_id: collection.map(&:id)).group(:payout_id).count+ endendendenddiff --git a/spree/api/app/serializers/spree/api/v3/admin/seller_payout_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/seller_payout_serializer.rbindex 71cac40ec5..80767ae3fd 100644--- a/spree/api/app/serializers/spree/api/v3/admin/seller_payout_serializer.rb+++ b/spree/api/app/serializers/spree/api/v3/admin/seller_payout_serializer.rb@@ -31,7 +31,14 @@ module Spree# How many earnings this settlement covers, so a list answers "what# is in this deposit" without loading every transfer.- attribute(:transfers_count) { |payout| payout.transfers.count }+ #+ # Read from a count the controller made for the whole page when there+ # is one; a single payout read falls back to asking for its own.+ attribute(:transfers_count) do |payout, params|+ counts = params && params[:transfer_counts]++ counts ? counts.fetch(payout.id, 0) : payout.transfers.count+ endendendenddiff --git a/spree/core/app/models/spree/store.rb b/spree/core/app/models/spree/store.rbindex 429b2f56fe..c0972bae0d 100644--- a/spree/core/app/models/spree/store.rb+++ b/spree/core/app/models/spree/store.rb@@ -409,7 +409,15 @@ module Spree## @return [Spree::PayoutProvider::Base]def payout_provider_instance- @payout_provider_instance ||= payout_provider_class.new+ resolved = payout_provider_class+ # Keyed by the class it was built from, so changing the setting on a+ # store already in hand cannot keep answering with the old provider —+ # which would route real money through whoever was configured before.+ if @payout_provider_instance.nil? || @payout_provider_instance.class != resolved+ @payout_provider_instance = resolved.new+ end++ @payout_provider_instanceenddef reload(*)diff --git a/spree/core/spec/models/spree/store_spec.rb b/spree/core/spec/models/spree/store_spec.rbindex ccade2feb3..b537160cc0 100644--- a/spree/core/spec/models/spree/store_spec.rb+++ b/spree/core/spec/models/spree/store_spec.rb@@ -1209,4 +1209,26 @@ describe Spree::Store, type: :model, without_global_store: true doit { is_expected.to be false }endend+ describe '#payout_provider_instance' do+ let(:payouts_store) { create(:store) }++ it 'answers with the store’s own provider' do+ expect(payouts_store.payout_provider_instance).to be_a(Spree::PayoutProvider::System)+ end++ # A store object held across a settings change must not keep paying+ # through whoever was configured before.+ it 'follows the setting when it changes' do+ other = Class.new(Spree::PayoutProvider::Base) do+ def self.name = 'TestOtherPayoutProvider'+ end+ stub_const('TestOtherPayoutProvider', other)+ allow(Spree).to receive(:payout_providers).and_return([Spree::PayoutProvider::System, other])++ payouts_store.payout_provider_instance+ payouts_store.preferred_payout_provider = 'TestOtherPayoutProvider'++ expect(payouts_store.payout_provider_instance).to be_a(other)+ end+ endenddiff --git a/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb b/spree/providers/stripe/app/models/spree_stripe/payout_provider.rbindex 318d5627cc..36cbbfeef2 100644--- a/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb+++ b/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb@@ -124,6 +124,11 @@ module SpreeStripe# Moves one seller's earning to their connected account.def transfer!(seller_transfer)+ # Nothing to move. A seller whose commission met their sale earns zero,+ # which is a true ledger row and an amount Stripe refuses — asking it to+ # would park the row and have the retry job ask again forever.+ return complete_without_sending(seller_transfer) if minor_units(seller_transfer).zero?+seller = seller_transfer.sellergateway = gateway_for(seller.store)@@ -194,6 +199,11 @@ module SpreeStripeprivate+ def complete_without_sending(seller_transfer)+ seller_transfer.update!(status: 'completed')+ seller_transfer+ end+# Keyed to the earnings being settled, not to the settlement row.## A payout that times out is marked failed and releases its earnings, sodiff --git a/spree/providers/stripe/spec/models/spree_stripe/payout_provider_spec.rb b/spree/providers/stripe/spec/models/spree_stripe/payout_provider_spec.rbindex eeaa7da0c3..3dc80fbc77 100644--- a/spree/providers/stripe/spec/models/spree_stripe/payout_provider_spec.rb+++ b/spree/providers/stripe/spec/models/spree_stripe/payout_provider_spec.rb@@ -41,6 +41,20 @@ RSpec.describe SpreeStripe::PayoutProvider dodescribed_class.new.transfer!(seller_transfer)end+ # A seller whose commission met their sale earns nothing. The row is+ # true, and Stripe refuses the amount — asking it would park the row and+ # have the retry job ask again forever.+ it 'sends nothing when there is nothing to send' do+ nothing = create(:seller_transfer, seller: seller, amount: 0, currency: 'USD',+ order: create(:order, store: store, seller: seller))++ expect(Stripe::Transfer).not_to receive(:create)++ described_class.new.transfer!(nothing)++ expect(nothing.reload).to be_completed+ end+it 'refuses when the store has no Stripe payment method to pay from' dogateway.update!(active: false)