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
File a payout account under whoever issued it
Damian Legawiece584fc5f40dc
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 00a08bf89e..4d9727ed02 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@@ -12,16 +12,22 @@ module Spreeclass SellerPayoutsController < ResourceControllerscoped_resource :payouts- before_action :set_resource, only: [:complete]-# PATCH /api/v3/admin/seller_payouts/:id/complete## What the built-in provider waits for: the operator saying the bank# transfer went out, with the reference it went out under.+ #+ # Authorized as an update, like every other custom member action —+ # naming the action itself would only pass while the ability grants+ # `manage`, and a host narrowing that would get a 403 the scope gate+ # had already allowed.def complete+ @resource = find_resource+ authorize! :update, @resource+result = Spree.seller_payout_complete_workflow.call(seller_payout: @resource,- reference: params[:reference]+ reference: params[:reference].to_s.presence)if result.success?diff --git a/spree/core/app/jobs/spree/seller_transfers/execute_pending_job.rb b/spree/core/app/jobs/spree/seller_transfers/execute_pending_job.rbindex 499cd12333..a6bc2ce714 100644--- a/spree/core/app/jobs/spree/seller_transfers/execute_pending_job.rb+++ b/spree/core/app/jobs/spree/seller_transfers/execute_pending_job.rb@@ -20,7 +20,11 @@ module Spreeprovider = seller.store.payout_provider_instance- seller.seller_transfers.pending.where(payout_id: nil).find_each do |transfer|+ # `processing` as well as `pending`: a transfer whose provider call+ # raised parks there, and without picking it up again a single network+ # blip would strand that earning for good — it is counted in no+ # balance and collected by no sweep.+ seller.seller_transfers.where(status: %w[pending processing], payout_id: nil).find_each do |transfer|provider.transfer!(transfer)rescue StandardError => e# One seller's stuck earning must not stop the rest — the row staysdiff --git a/spree/core/app/models/spree/seller.rb b/spree/core/app/models/spree/seller.rbindex 7d9d3bdfee..0806a30a73 100644--- a/spree/core/app/models/spree/seller.rb+++ b/spree/core/app/models/spree/seller.rb@@ -243,24 +243,33 @@ module Spree# webhook needs — "which seller is acct_123?" — is a uniquely indexed read# rather than a scan.#+ # @param provider [Class] the payout provider whose account is wanted;+ # defaults to the one the store is configured to pay through# @return [String, nil]- def payout_account_reference- external_id_for(payout_reference_system)+ def payout_account_reference(provider = store.payout_provider_class)+ external_id_for(provider.reference_system)end+ # Records the account a provider issued this seller.+ #+ # @param provider [Class] the provider that issued it — stated by the+ # caller rather than read from the store, since a gem writes the account+ # it minted whatever the store is currently configured to pay through# @param account_reference [String, nil] blank forgets the account# @return [void]- def payout_account_reference=(account_reference)- set_external_id(payout_reference_system, account_reference)+ def set_payout_account_reference(provider, account_reference)+ set_external_id(provider.reference_system, account_reference)end- # Sellers holding an account with the store's payout provider, for the- # reverse lookup a provider webhook does.+ # Sellers holding an account with one provider, for the reverse lookup a+ # provider webhook does — it knows the account, not the seller.#+ # @param store [Spree::Store]+ # @param provider [Class]# @param account_reference [String]# @return [ActiveRecord::Relation]- def self.with_payout_account(store, account_reference)- store.sellers.with_external_id(store.payout_provider_class.reference_system, account_reference)+ def self.with_payout_account(store, provider, account_reference)+ store.sellers.with_external_id(provider.reference_system, account_reference)end# Whether a provider says this seller may be sent money yet.@@ -276,15 +285,6 @@ module Spreepayouts_enabled_at.present?end- # Which system a payout account reference belongs to. The provider's own- # key, so a marketplace that changes provider does not read the old- # account as if it were the new one.- #- # @return [String]- def payout_reference_system- store.payout_provider_class.reference_system- end-# What this seller's settlement schedule is, falling back to the store's.## @return [String]diff --git a/spree/core/app/models/spree/seller_payout.rb b/spree/core/app/models/spree/seller_payout.rbindex b41e01635d..4d89d3b159 100644--- a/spree/core/app/models/spree/seller_payout.rb+++ b/spree/core/app/models/spree/seller_payout.rb@@ -84,7 +84,10 @@ module Spreedef fail!transaction dorelease_transfers- update!(status: 'failed', amount: 0)+ # The amount stays: an operator investigating a failed settlement has+ # to see what was attempted, and the `owed` scope already excludes it+ # from what is still to be sent.+ update!(status: 'failed')endenddiff --git a/spree/core/spec/models/spree/seller_spec.rb b/spree/core/spec/models/spree/seller_spec.rbindex 0f0e9e0ca4..141eddead0 100644--- a/spree/core/spec/models/spree/seller_spec.rb+++ b/spree/core/spec/models/spree/seller_spec.rb@@ -421,42 +421,61 @@ describe Spree::Seller dodescribe 'the payout account' dolet(:seller) { create(:seller, :approved, store: @default_store) }- it 'is stored as an external reference against the store’s provider' do- seller.payout_account_reference = 'acct_123'+ # A stand-in for a provider gem, which names itself when it files the+ # account it minted.+ let(:provider) do+ Class.new(Spree::PayoutProvider::Base) do+ def self.reference_system = 'acme_pay'+ end+ end++ it 'is filed under the provider that issued it' do+ seller.set_payout_account_reference(provider, 'acct_123')reference = seller.external_references.reload.first- expect(reference.system).to eq('system')+ expect(reference.system).to eq('acme_pay')expect(reference.external_id).to eq('acct_123')endit 'reads back what was written' do- seller.payout_account_reference = 'acct_123'+ seller.set_payout_account_reference(provider, 'acct_123')- expect(seller.reload.payout_account_reference).to eq('acct_123')+ expect(seller.reload.payout_account_reference(provider)).to eq('acct_123')endit 'is nil for a seller who holds no account' do- expect(seller.payout_account_reference).to be_nil+ expect(seller.payout_account_reference(provider)).to be_nilendit 'forgets the account when blanked' do- seller.payout_account_reference = 'acct_123'- seller.payout_account_reference = nil+ seller.set_payout_account_reference(provider, 'acct_123')+ seller.set_payout_account_reference(provider, nil)++ expect(seller.reload.payout_account_reference(provider)).to be_nil+ end++ # The whole point of keying by provider: a marketplace that changes+ # provider keeps the old account rather than reading it as the new one.+ it 'keeps one provider’s account out of another’s' do+ other_provider = Class.new(Spree::PayoutProvider::Base) do+ def self.reference_system = 'other_pay'+ end+ seller.set_payout_account_reference(provider, 'acct_123')- expect(seller.reload.payout_account_reference).to be_nil+ expect(seller.reload.payout_account_reference(other_provider)).to be_nilend# What a provider webhook does: it knows the account, not the seller.it 'finds the seller an account belongs to' do- seller.payout_account_reference = 'acct_123'+ seller.set_payout_account_reference(provider, 'acct_123')- expect(Spree::Seller.with_payout_account(@default_store, 'acct_123')).to contain_exactly(seller)+ expect(Spree::Seller.with_payout_account(@default_store, provider, 'acct_123')).to contain_exactly(seller)endit 'does not find a seller in another marketplace' do- seller.payout_account_reference = 'acct_123'+ seller.set_payout_account_reference(provider, 'acct_123')- expect(Spree::Seller.with_payout_account(create(:store), 'acct_123')).to be_empty+ expect(Spree::Seller.with_payout_account(create(:store), provider, 'acct_123')).to be_emptyendendenddiff --git a/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rb b/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rbindex 8cfe221cf3..237ebffb8a 100644--- a/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rb+++ b/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rb@@ -63,11 +63,13 @@ module SpreeStripe# @param return_url [String] where Stripe sends them when they finish# @return [String] the onboarding URLdef create_connect_account_link(seller:, refresh_url:, return_url:)- seller.update!(payout_account_reference: create_connect_account(seller)) if seller.payout_account_reference.blank?+ account_id = payout_account_for(seller) || create_connect_account(seller).tap do |created|+ seller.set_payout_account_reference(SpreeStripe::PayoutProvider, created)+ endStripe::AccountLink.create({- account: seller.payout_account_reference,+ account: account_id,refresh_url: refresh_url,return_url: return_url,type: 'account_onboarding'@@ -151,15 +153,34 @@ module SpreeStripe# Which seller Stripe is talking about. A uniquely indexed read of the# account reference, scoped to this store's own sellers.+ #+ # Filed under this gem's own key rather than the store's configured+ # payout provider: Stripe issued the account, so Stripe is what it+ # belongs to, and a store still settling by hand must still be able to+ # onboard sellers to Connect.def seller_for_account(account_id)return if account_id.blank?- Spree::Seller.with_payout_account(store, account_id).first+ Spree::Seller.with_payout_account(store, SpreeStripe::PayoutProvider, account_id).first+ end++ # @return [String, nil] the Connect account this seller already holds+ def payout_account_for(seller)+ seller.payout_account_reference(SpreeStripe::PayoutProvider)end+ # Stripe's own id first. The fallback is for a settlement recorded before+ # Stripe named it, and it must match on amount as well as currency:+ # Stripe batches a connected account's balance on its own schedule, so+ # one payout of 250 does not mean the oldest settlement of 40 landed —+ # completing that would debit the wrong figure and stamp Stripe's id+ # where the settlement it really belongs to can no longer find it.def find_payout(seller, object)seller.seller_payouts.find_by(reference: object.id) ||- seller.seller_payouts.owed.where(currency: object.currency.to_s.upcase).order(:created_at).first+ seller.seller_payouts.owed.+ where(currency: object.currency.to_s.upcase, reference: nil).+ where(amount: Spree::Money::Rounding.from_minor_units(object.amount, object.currency)).+ order(:created_at).firstenddef verify_connect_webhook_signature(raw_body, headers)diff --git a/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb b/spree/providers/stripe/app/models/spree_stripe/payout_provider.rbindex ad6b234190..4f87699080 100644--- a/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb+++ b/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb@@ -56,7 +56,7 @@ module SpreeStripe{amount: minor_units(seller_transfer),currency: seller_transfer.currency.downcase,- destination: seller.payout_account_reference,+ destination: seller.payout_account_reference(self.class),# Funds the transfer from the customer's own charge, so it settles# with that charge rather than out of the platform's balance.source_transaction: source_charge_for(seller_transfer, gateway),diff --git a/spree/providers/stripe/spec/models/spree_stripe/gateway/connect_spec.rb b/spree/providers/stripe/spec/models/spree_stripe/gateway/connect_spec.rbindex 4c524ba4e3..28af1cb670 100644--- a/spree/providers/stripe/spec/models/spree_stripe/gateway/connect_spec.rb+++ b/spree/providers/stripe/spec/models/spree_stripe/gateway/connect_spec.rb@@ -3,7 +3,11 @@ require 'spec_helper'RSpec.describe SpreeStripe::Gateway::Connect dolet(:store) { @default_store }let(:gateway) { create(:stripe_gateway, store: store) }- let(:seller) { create(:seller, :approved, store: store, payout_account_reference: 'acct_seller') }+ let(:seller) do+ create(:seller, :approved, store: store).tap do |record|+ record.set_payout_account_reference(SpreeStripe::PayoutProvider, 'acct_seller')+ end+ endlet(:headers) { { 'HTTP_STRIPE_SIGNATURE' => 'sig_test' } }let(:raw_body) { '{"id": "evt_test"}' }@@ -63,7 +67,7 @@ RSpec.describe SpreeStripe::Gateway::Connect doit 'completes the settlement the seller was owed' doallow(Stripe::Webhook).to receive(:construct_event).and_return(- stripe_event('payout.paid', { id: 'po_1', currency: 'usd' }, account: 'acct_seller')+ stripe_event('payout.paid', { id: 'po_1', currency: 'usd', amount: 2_000 }, account: 'acct_seller'))gateway.handle_payout_webhook(raw_body, headers)@@ -72,9 +76,21 @@ RSpec.describe SpreeStripe::Gateway::Connect doexpect(payout.reference).to eq('po_1')end+ # Stripe batches a connected account's balance on its own schedule, so a+ # payout of one amount says nothing about a settlement of another.+ it 'leaves a settlement of a different amount alone' do+ allow(Stripe::Webhook).to receive(:construct_event).and_return(+ stripe_event('payout.paid', { id: 'po_1', currency: 'usd', amount: 25_000 }, account: 'acct_seller')+ )++ gateway.handle_payout_webhook(raw_body, headers)++ expect(payout.reload).to be_pending+ end+it 'leaves a settlement in another currency alone' doallow(Stripe::Webhook).to receive(:construct_event).and_return(- stripe_event('payout.paid', { id: 'po_1', currency: 'eur' }, account: 'acct_seller')+ stripe_event('payout.paid', { id: 'po_1', currency: 'eur', amount: 2_000 }, account: 'acct_seller'))gateway.handle_payout_webhook(raw_body, headers)@@ -88,7 +104,7 @@ RSpec.describe SpreeStripe::Gateway::Connect dobefore doallow(Stripe::Webhook).to receive(:construct_event).and_return(- stripe_event('payout.failed', { id: 'po_1', currency: 'usd' }, account: 'acct_seller')+ stripe_event('payout.failed', { id: 'po_1', currency: 'usd', amount: 2_000 }, account: 'acct_seller'))end@@ -116,11 +132,11 @@ RSpec.describe SpreeStripe::Gateway::Connect do# completed and land on the next one still owed.context 'when payout.paid is delivered twice' dolet!(:first) { create(:seller_payout, seller: seller, currency: 'USD', amount: 40) }- let!(:second) { create(:seller_payout, seller: seller, currency: 'USD', amount: 60) }+ let!(:second) { create(:seller_payout, seller: seller, currency: 'USD', amount: 40) }before doallow(Stripe::Webhook).to receive(:construct_event).and_return(- stripe_event('payout.paid', { id: 'po_1', currency: 'usd' }, account: 'acct_seller')+ stripe_event('payout.paid', { id: 'po_1', currency: 'usd', amount: 4_000 }, account: 'acct_seller'))end@@ -154,7 +170,7 @@ RSpec.describe SpreeStripe::Gateway::Connect dodescribe '#create_connect_account_link' doit 'creates an Express account for a seller who has none, then links to onboarding' do- seller = create(:seller, :approved, store: store, payout_account_reference: nil)+ seller = create(:seller, :approved, store: store)allow(Stripe::Account).to receive(:create).and_return(Stripe::StripeObject.construct_from(id: 'acct_new'))allow(Stripe::AccountLink).to receive(:create).and_return(Stripe::StripeObject.construct_from(url: 'https://connect.stripe.com/setup/x'))@@ -162,7 +178,7 @@ RSpec.describe SpreeStripe::Gateway::Connect dourl = gateway.create_connect_account_link(seller: seller, refresh_url: 'https://s/r', return_url: 'https://s/d')expect(url).to eq('https://connect.stripe.com/setup/x')- expect(seller.reload.payout_account_reference).to eq('acct_new')+ expect(seller.reload.payout_account_reference(SpreeStripe::PayoutProvider)).to eq('acct_new')endit 'reuses the account a seller already holds' dodiff --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 86669db0b4..6db663e9f8 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@@ -3,7 +3,11 @@ require 'spec_helper'RSpec.describe SpreeStripe::PayoutProvider dolet(:store) { @default_store }let!(:gateway) { create(:stripe_gateway, store: store) }- let(:seller) { create(:seller, :approved, store: store, payout_account_reference: 'acct_seller') }+ let(:seller) do+ create(:seller, :approved, store: store).tap do |record|+ record.set_payout_account_reference(SpreeStripe::PayoutProvider, 'acct_seller')+ end+ endlet(:order) { create(:order, store: store, seller: seller, total: 100, currency: 'USD') }let(:seller_transfer) { create(:seller_transfer, seller: seller, order: order, amount: 42.5, currency: 'USD') }