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.

Close two ways a seller could be paid money they were not owed

Damian Legawiec210d9bd738d8
diff --git a/spree/api/app/controllers/spree/api/v3/seller/onboarding_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/onboarding_controller.rb
index a1b9cd097e..9bf6e1f874 100644
--- a/spree/api/app/controllers/spree/api/v3/seller/onboarding_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/seller/onboarding_controller.rb
@@ -47,8 +47,8 @@ module Spree
provider = current_store.payout_provider_instance
url = provider.onboarding_url(
current_seller,
- refresh_url: params.require(:refresh_url),
- return_url: params.require(:return_url)
+ refresh_url: panel_url(params.require(:refresh_url)),
+ return_url: panel_url(params.require(:return_url))
)
# A null link rather than an empty response: the panel has to tell
@@ -59,6 +59,26 @@ module Spree
render_service_error(e.message)
end
+ # Where a provider may send the seller back to.
+ #
+ # Checked against the panel's own origin rather than echoed: a seller
+ # could otherwise name any address and turn the marketplace's genuine
+ # provider flow into a redirect to a page of their choosing, which is
+ # a convincing place to ask somebody for credentials. Anything else
+ # falls back to the panel's own root.
+ def panel_url(candidate)
+ panel = Spree::Sellers::PanelUrl.call(store: current_store)
+ return panel if candidate.blank?
+
+ uri = URI.parse(candidate.to_s)
+ allowed = URI.parse(panel)
+
+ same_origin = uri.scheme == allowed.scheme && uri.host == allowed.host && uri.port == allowed.port
+ same_origin ? candidate.to_s : panel
+ rescue URI::InvalidURIError
+ panel
+ end
+
protected
def read_actions
diff --git a/spree/api/spec/controllers/spree/api/v3/seller/onboarding_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/onboarding_controller_spec.rb
index 75d53fd9a5..a65e7622c2 100644
--- a/spree/api/spec/controllers/spree/api/v3/seller/onboarding_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/seller/onboarding_controller_spec.rb
@@ -119,13 +119,32 @@ RSpec.describe Spree::Api::V3::Seller::OnboardingController, type: :controller d
expect(json_response['url']).to eq('https://provider.example/setup/abc')
end
- # The panel says where to come back to — core does not know its routes.
- it 'sends the panel’s own return addresses to the provider' do
+ # The panel says where to come back to, within its own origin.
+ it 'sends back an address the panel actually serves' do
+ panel = Spree::Sellers::PanelUrl.call(store: store)
+
expect_any_instance_of(Spree::PayoutProvider::System).to receive(:onboarding_url).
- with(anything, hash_including(refresh_url: urls[:refresh_url], return_url: urls[:return_url])).
+ with(anything, hash_including(return_url: "#{panel}/onboarding")).
and_return('https://provider.example/setup/abc')
- post :payout_account, params: urls, as: :json
+ post :payout_account,
+ params: { refresh_url: "#{panel}/onboarding", return_url: "#{panel}/onboarding" },
+ as: :json
+ end
+
+ # A seller could otherwise turn the marketplace's genuine provider flow
+ # into a redirect to a page of their choosing, which is a convincing
+ # place to ask somebody for their details.
+ it 'refuses to send the seller somewhere else entirely' do
+ panel = Spree::Sellers::PanelUrl.call(store: store)
+
+ expect_any_instance_of(Spree::PayoutProvider::System).to receive(:onboarding_url).
+ with(anything, hash_including(return_url: panel)).
+ and_return('https://provider.example/setup/abc')
+
+ post :payout_account,
+ params: { refresh_url: 'https://evil.test/steal', return_url: 'https://evil.test/steal' },
+ as: :json
end
it 'reports a provider that refuses rather than raising' do
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 a6bc2ce714..43a0874337 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
@@ -24,7 +24,11 @@ module Spree
# 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|
+ # Earnings only. A reversal parks in `processing` the same way when its
+ # own provider call raises, but sending one through `transfer!` would
+ # pay the seller the amount it exists to take back — the row is
+ # negative and the amount sent is its absolute value.
+ seller.seller_transfers.earnings.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/store.rb b/spree/core/app/models/spree/store.rb
index 74985fa2e1..429b2f56fe 100644
--- a/spree/core/app/models/spree/store.rb
+++ b/spree/core/app/models/spree/store.rb
@@ -401,15 +401,22 @@ module Spree
#
# Who moves money to this store's sellers.
#
- # Memoized per record, not per process: a store reloaded after an operator
- # changes the setting resolves afresh, so a marketplace that connects a
- # provider mid-process still starts using it without a restart.
+ # Memoized per record, and cleared on reload — `reload` leaves plain
+ # instance variables alone, so without that a provider holding its own
+ # cache (Stripe's account lookups do) would answer from a snapshot taken
+ # arbitrarily long ago, and an operator changing the setting would not be
+ # picked up by a store object already in hand.
#
# @return [Spree::PayoutProvider::Base]
def payout_provider_instance
@payout_provider_instance ||= payout_provider_class.new
end
+ def reload(*)
+ @payout_provider_instance = nil
+ super
+ end
+
# The class alone, for the questions that are about the provider rather
# than about a movement — there is no need to build one to ask what it
# requires.
diff --git a/spree/core/app/workflows/spree/seller_payouts/sweep.rb b/spree/core/app/workflows/spree/seller_payouts/sweep.rb
index 35df6fa48f..fcc346b883 100644
--- a/spree/core/app/workflows/spree/seller_payouts/sweep.rb
+++ b/spree/core/app/workflows/spree/seller_payouts/sweep.rb
@@ -102,8 +102,15 @@ module Spree
discard_empty_payout if @payout.amount <= 0 || @payout.amount < seller.resolved_minimum_payout_amount
end
+ # Both the figure and the period: a settlement whose window covers
+ # earnings that ended up in a different payout answers the
+ # reconciliation question wrongly, which is the only question the
+ # period fields exist for.
def restate_amount
- @payout.update!(amount: @payout.transfers_total)
+ @payout.update!(
+ amount: @payout.transfers_total,
+ period_start: @payout.transfers.minimum(:created_at) || @payout.period_start
+ )
end
# Nothing was claimed, so there is nothing to release and nothing to
diff --git a/spree/core/spec/jobs/spree/seller_transfers/execute_pending_job_spec.rb b/spree/core/spec/jobs/spree/seller_transfers/execute_pending_job_spec.rb
index 22d24897cd..106d10fe61 100644
--- a/spree/core/spec/jobs/spree/seller_transfers/execute_pending_job_spec.rb
+++ b/spree/core/spec/jobs/spree/seller_transfers/execute_pending_job_spec.rb
@@ -48,6 +48,23 @@ RSpec.describe Spree::SellerTransfers::ExecutePendingJob do
expect(second.reload).to be_completed
end
+ # A reversal parks in `processing` the same way an earning does, and its
+ # amount is negative — sending one would pay the seller the money it exists
+ # to take back.
+ it 'never sends a reversal as a payment' do
+ earning = create(:seller_transfer, :completed, seller: seller, amount: 40,
+ order: create(:order, store: store, seller: seller))
+ reversal = create(:seller_transfer, seller: seller, amount: -15, kind: 'refund_reversal',
+ status: 'processing', reversed_from: earning,
+ order: create(:order, store: store, seller: seller))
+
+ expect_any_instance_of(Spree::PayoutProvider::System).not_to receive(:transfer!).with(reversal)
+
+ described_class.perform_now(seller.id)
+
+ expect(reversal.reload).to be_processing
+ end
+
it 'does nothing for a seller the provider still will not accept' do
allow(Spree::PayoutProvider::System).to receive(:requires_payout_account?).and_return(true)
seller.update!(payouts_enabled_at: nil)
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 8041db3adb..318d5627cc 100644
--- a/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb
+++ b/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb
@@ -82,6 +82,14 @@ module SpreeStripe
account = retrieve_account(seller)
return seller.payouts_enabled? if account.nil?
+ # What Stripe says, written down. The stamp is a cache of a webhook that
+ # can fail to arrive in either direction — a seller left looking at an
+ # unfinished checklist they have finished, or one still counted payable
+ # after Stripe withdrew it. Recording the answer keeps the workflows
+ # that gate money on the column honest without their asking.
+ stamp = account.payouts_enabled ? (seller.payouts_enabled_at || Time.current) : nil
+ seller.update!(payouts_enabled_at: stamp) if seller.payouts_enabled_at != stamp
+
account.payouts_enabled
end
@@ -164,7 +172,7 @@ module SpreeStripe
currency: seller_payout.currency.downcase,
metadata: { spree_seller_payout_id: seller_payout.id }
},
- gateway.api_options.merge(stripe_account: account_id, idempotency_key: idempotency_key(seller_payout))
+ gateway.api_options.merge(stripe_account: account_id, idempotency_key: payout_idempotency_key(seller_payout))
)
# Stored now rather than waiting for the webhook, so the confirmation
@@ -186,6 +194,22 @@ module SpreeStripe
private
+ # Keyed to the earnings being settled, not to the settlement row.
+ #
+ # A payout that times out is marked failed and releases its earnings, so
+ # the next sweep assembles a *new* row for the same money — and a key
+ # derived from that row would be a new key, which is Stripe's cue to send
+ # the money a second time. The transfers are what the two attempts have in
+ # common, so they are what identifies the operation.
+ def payout_idempotency_key(seller_payout)
+ transfer_ids = seller_payout.transfers.order(:id).pluck(:id)
+ digest = Digest::SHA256.hexdigest(
+ "#{seller_payout.seller_id}-#{seller_payout.currency}-#{transfer_ids.join(',')}"
+ )
+
+ "spree-payout-#{digest[0, 32]}"
+ end
+
# Nil rather than raising: a checklist row asking why it is incomplete
# must not take the page down because Stripe is unreachable.
def retrieve_account(seller)