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.

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.rb
index 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 Spree
class SellerPayoutsController < ResourceController
scoped_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.rb
index 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 Spree
provider = 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 stays
diff --git a/spree/core/app/models/spree/seller.rb b/spree/core/app/models/spree/seller.rb
index 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 Spree
payouts_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.rb
index 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 Spree
def fail!
transaction do
release_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')
end
end
diff --git a/spree/core/spec/models/spree/seller_spec.rb b/spree/core/spec/models/spree/seller_spec.rb
index 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 do
describe 'the payout account' do
let(: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')
end
it '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')
end
it '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_nil
end
it '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_nil
end
# 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)
end
it '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_empty
end
end
end
diff --git a/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rb b/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rb
index 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 URL
def 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)
+ end
Stripe::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).first
end
def 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.rb
index 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.rb
index 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 do
let(: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
+ end
let(:headers) { { 'HTTP_STRIPE_SIGNATURE' => 'sig_test' } }
let(:raw_body) { '{"id": "evt_test"}' }
@@ -63,7 +67,7 @@ RSpec.describe SpreeStripe::Gateway::Connect do
it 'completes the settlement the seller was owed' do
allow(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 do
expect(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' do
allow(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 do
before do
allow(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' do
let!(: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 do
allow(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 do
describe '#create_connect_account_link' do
it '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 do
url = 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')
end
it 'reuses the account a seller already holds' do
diff --git a/spree/providers/stripe/spec/models/spree_stripe/payout_provider_spec.rb b/spree/providers/stripe/spec/models/spree_stripe/payout_provider_spec.rb
index 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 do
let(: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
+ end
let(: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') }