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: A cross-border seller could never be paid

Łukasz Adamczykb0659a735dc2
diff --git a/docs/plans/6.0-multi-vendor-marketplace.md b/docs/plans/6.0-multi-vendor-marketplace.md
index 688c63ad91..0a81abdb02 100644
--- a/docs/plans/6.0-multi-vendor-marketplace.md
+++ b/docs/plans/6.0-multi-vendor-marketplace.md
@@ -637,12 +637,19 @@ end
```ruby
# seller.rb — per currency; every ledger row carries `currency`, so a multi-currency
# store accrues separate balances and the payout sweep runs per (seller, currency).
-def balance(currency) # what we still owe the seller in that currency
- seller_transfers.completed.where(currency: currency).sum(:amount) -
- seller_payouts.completed.where(currency: currency).sum(:amount)
+# Answered in the SETTLEMENT currency (2026-09-14): what a payout can actually send.
+def balance(currency) # what we still owe the seller, in a currency they can be paid in
+ earnings = seller_transfers.completed.settling_in(currency)
+
+ earnings.sum(&:settlement_amount) -
+ earnings.select { |t| t.payout&.completed? }.sum(&:settlement_amount)
end
```
+**Earned and settled are two figures (RESOLVED 2026-09-14, shipped).** A transfer is denominated in the **order's** currency, because that is what the customer paid; a connected account settles in **its own**, and the provider converts on the way in. With only one figure the ledger accrued money the account could never pay out — a GB seller on a US marketplace was owed USD against an account holding only GBP, so `available_payout` answered zero and the sweep halted forever. `spree_seller_transfers` therefore carries `settled_amount` / `settled_currency` alongside `amount` / `currency`, written by the provider from what it reports actually arrived (`Transfer.destination_payment.balance_transaction` for Stripe). Both are nullable: nil means no provider has reported a settlement, and the earned figures stand in — which is right for `PayoutProvider::System`, where the operator settles offline in the order's currency. Payouts are swept, summed and denominated in the settled currency, since that is the only money the account can move; a reversal prorates from its earning's settled figure so a clawback comes back at the rate the money went out at, and lands in the batch that pays the earning it cancels. A balance is one position per (sale currency, settlement currency) pair, showing both sides.
+
+**This is not a retreat from "nothing is ever converted".** Spree still holds no exchange rates and performs no conversion. It records a conversion the provider performed and reported, as a fact, beside the original — two actuals, never added together. The standing no-FX rule (per-currency commission values, per-currency balances, reporting totals never summed across currencies) is untouched.
+
```ruby
# Pluggable execution strategy — CORE base + system no-op; basic StripeConnect ships
# with the monorepo Stripe gateway (2026-07-15 amendment); Enterprise extends it.
diff --git a/packages/admin-sdk/src/types/generated/SellerBalance.ts b/packages/admin-sdk/src/types/generated/SellerBalance.ts
index 6ebf3e66f6..551a8bc2bb 100644
--- a/packages/admin-sdk/src/types/generated/SellerBalance.ts
+++ b/packages/admin-sdk/src/types/generated/SellerBalance.ts
@@ -1,12 +1,16 @@
-// Typelizer digest 057e4e4879a02e9f5abcfc31c153abc6
+// Typelizer digest 44a6a4f3d5c25e767689c0ff23f83033
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
interface SellerBalance {
currency: string;
+ settlement_currency: string;
+ converted: boolean;
seller_id: string;
earned: string;
display_earned: string;
+ payable: string;
+ display_payable: string;
paid: string;
display_paid: string;
balance: string;
diff --git a/packages/admin-sdk/src/types/generated/SellerTransfer.ts b/packages/admin-sdk/src/types/generated/SellerTransfer.ts
index d89406c523..e5e6fa4b37 100644
--- a/packages/admin-sdk/src/types/generated/SellerTransfer.ts
+++ b/packages/admin-sdk/src/types/generated/SellerTransfer.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 3ad2a94c7494765fab044af8ad62ec0c
+// Typelizer digest 9a02cabd4c6efba9fdb452f6074d9741
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {SellerTransferStatus} from '@/types'
@@ -16,6 +16,9 @@ interface SellerTransfer {
updated_at: string;
amount: string;
display_amount: string;
+ settled_amount: string | null;
+ settled_currency: string | null;
+ converted: boolean;
seller_id: string;
order_id: string | null;
payout_id: string | null;
diff --git a/packages/dashboard/src/components/spree/sellers/seller-balance-card.tsx b/packages/dashboard/src/components/spree/sellers/seller-balance-card.tsx
index 8df9f4977c..8009bad203 100644
--- a/packages/dashboard/src/components/spree/sellers/seller-balance-card.tsx
+++ b/packages/dashboard/src/components/spree/sellers/seller-balance-card.tsx
@@ -21,6 +21,10 @@ import { ReadRow } from './seller-read-row'
* Nothing is ever converted between currencies, so a seller trading in two
* has two positions rather than one total. Renders nothing until their first
* fulfilled sale — an empty card on every new seller would only be noise.
+ *
+ * A seller whose account settles in another currency has two sides to one
+ * position: what the sales were worth, and what their account received once
+ * the provider converted. The owed figure is the one a payout can send.
*/
export function SellerBalanceCard({ seller }: { seller: Seller }) {
const { t } = useTranslation()
@@ -60,12 +64,23 @@ export function SellerBalanceCard({ seller }: { seller: Seller }) {
</CardHeader>
<CardContent className="flex flex-col gap-4">
{balances.map((balance, index) => (
- <div key={balance.currency} className="flex flex-col gap-3">
+ <div key={`${balance.currency}-${balance.settlement_currency}`} className="flex flex-col gap-3">
{index > 0 && <Separator />}
- <ReadRow label={t('admin.payouts.balance.owed', { currency: balance.currency })}>
+ <ReadRow label={t('admin.payouts.balance.owed', { currency: balance.settlement_currency })}>
<span className="font-medium">{balance.display_balance}</span>
</ReadRow>
- <ReadRow label={t('admin.payouts.balance.earned')}>{balance.display_earned}</ReadRow>
+ <ReadRow
+ label={
+ balance.converted
+ ? t('admin.payouts.balance.earned_in', { currency: balance.currency })
+ : t('admin.payouts.balance.earned')
+ }
+ >
+ {balance.display_earned}
+ </ReadRow>
+ {balance.converted && (
+ <ReadRow label={t('admin.payouts.balance.payable')}>{balance.display_payable}</ReadRow>
+ )}
<ReadRow label={t('admin.payouts.balance.paid')}>{balance.display_paid}</ReadRow>
<ReadRow label={t('admin.payouts.balance.pending')}>{balance.display_pending}</ReadRow>
</div>
diff --git a/packages/dashboard/src/locales/ar.json b/packages/dashboard/src/locales/ar.json
index b137ec8207..8e8bdd6c6a 100644
--- a/packages/dashboard/src/locales/ar.json
+++ b/packages/dashboard/src/locales/ar.json
@@ -5597,7 +5597,9 @@
"owed": "المستحق ({{currency}})",
"earned": "المكتسب",
"paid": "المدفوع",
- "pending": "في الطريق"
+ "pending": "في الطريق",
+ "earned_in": "Earned ({{currency}})",
+ "payable": "Payable"
},
"complete": {
"title": "وضع علامة مدفوعة على هذه الدفعة",
diff --git a/packages/dashboard/src/locales/de.json b/packages/dashboard/src/locales/de.json
index 5d54f8145d..067f6c6083 100644
--- a/packages/dashboard/src/locales/de.json
+++ b/packages/dashboard/src/locales/de.json
@@ -5563,7 +5563,9 @@
"owed": "Offen ({{currency}})",
"earned": "Verdient",
"paid": "Ausgezahlt",
- "pending": "Unterwegs"
+ "pending": "Unterwegs",
+ "earned_in": "Earned ({{currency}})",
+ "payable": "Payable"
},
"complete": {
"title": "Auszahlung als bezahlt markieren",
diff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.json
index 5dab51cf98..c57da2196e 100644
--- a/packages/dashboard/src/locales/en.json
+++ b/packages/dashboard/src/locales/en.json
@@ -5563,7 +5563,9 @@
"owed": "Owed ({{currency}})",
"earned": "Earned",
"paid": "Paid out",
- "pending": "On its way"
+ "pending": "On its way",
+ "earned_in": "Earned ({{currency}})",
+ "payable": "Payable"
},
"complete": {
"title": "Mark this payout as paid",
diff --git a/packages/dashboard/src/locales/fr.json b/packages/dashboard/src/locales/fr.json
index 419187165c..8319b5dc04 100644
--- a/packages/dashboard/src/locales/fr.json
+++ b/packages/dashboard/src/locales/fr.json
@@ -5563,7 +5563,9 @@
"owed": "Dû ({{currency}})",
"earned": "Gagné",
"paid": "Versé",
- "pending": "En route"
+ "pending": "En route",
+ "earned_in": "Earned ({{currency}})",
+ "payable": "Payable"
},
"complete": {
"title": "Marquer ce versement comme payé",
diff --git a/packages/dashboard/src/locales/pl.json b/packages/dashboard/src/locales/pl.json
index ef1b266dd2..08398b9974 100644
--- a/packages/dashboard/src/locales/pl.json
+++ b/packages/dashboard/src/locales/pl.json
@@ -5627,7 +5627,9 @@
"owed": "Do zapłaty ({{currency}})",
"earned": "Zarobione",
"paid": "Wypłacone",
- "pending": "W drodze"
+ "pending": "W drodze",
+ "earned_in": "Earned ({{currency}})",
+ "payable": "Payable"
},
"complete": {
"title": "Oznacz wypłatę jako zapłaconą",
diff --git a/packages/dashboard/src/locales/zh-CN.json b/packages/dashboard/src/locales/zh-CN.json
index 1eafe026d9..06a46b86f1 100644
--- a/packages/dashboard/src/locales/zh-CN.json
+++ b/packages/dashboard/src/locales/zh-CN.json
@@ -5551,7 +5551,9 @@
"owed": "应付({{currency}})",
"earned": "已赚取",
"paid": "已打款",
- "pending": "在途"
+ "pending": "在途",
+ "earned_in": "Earned ({{currency}})",
+ "payable": "Payable"
},
"complete": {
"title": "将此打款标记为已支付",
diff --git a/packages/seller-dashboard/src/components/finance/balance-summary.tsx b/packages/seller-dashboard/src/components/finance/balance-summary.tsx
index d99bbea274..6279def3c6 100644
--- a/packages/seller-dashboard/src/components/finance/balance-summary.tsx
+++ b/packages/seller-dashboard/src/components/finance/balance-summary.tsx
@@ -9,6 +9,10 @@ import { useTranslation } from 'react-i18next'
* positions rather than one total. `pending` is money the payout provider has
* not confirmed yet — shown beside the balance so a seller who has shipped
* does not read their own order as unpaid.
+ *
+ * A position has two sides when the seller banks in a currency other than the
+ * one they sold in: the balance is what their account will actually be paid,
+ * and the sale's own figure is shown beneath it. Both are recorded figures.
*/
export function BalanceSummary({ balances }: { balances: Balance[] }) {
const { t } = useTranslation()
@@ -27,14 +31,22 @@ export function BalanceSummary({ balances }: { balances: Balance[] }) {
return (
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
{balances.map((balance) => (
- <Card key={balance.currency}>
+ <Card key={`${balance.currency}-${balance.settlement_currency}`}>
<CardHeader>
- <CardTitle>{t('earnings.balance.owed', { currency: balance.currency })}</CardTitle>
+ <CardTitle>{t('earnings.balance.owed', { currency: balance.settlement_currency })}</CardTitle>
+ {balance.converted && (
+ <CardDescription>
+ {t('earnings.balance.converted', { currency: balance.currency })}
+ </CardDescription>
+ )}
</CardHeader>
<CardContent className="flex flex-col gap-3">
<span className="text-2xl font-semibold tabular-nums">{balance.display_balance}</span>
<dl className="flex flex-col gap-1 text-sm">
<Figure label={t('earnings.balance.earned')} value={balance.display_earned} />
+ {balance.converted && (
+ <Figure label={t('earnings.balance.payable')} value={balance.display_payable} />
+ )}
<Figure label={t('earnings.balance.paid')} value={balance.display_paid} />
<Figure label={t('earnings.balance.pending')} value={balance.display_pending} />
</dl>
diff --git a/packages/seller-dashboard/src/locales/en.json b/packages/seller-dashboard/src/locales/en.json
index d92d8fbc6c..9b5893d204 100644
--- a/packages/seller-dashboard/src/locales/en.json
+++ b/packages/seller-dashboard/src/locales/en.json
@@ -270,11 +270,13 @@
},
"earnings": {
"balance": {
+ "converted": "Earned in {{currency}}, paid in your own currency",
"earned": "Earned",
"empty": "Once an order of yours is fulfilled, what it earned shows up here.",
"load_error": "Could not load your balance.",
"owed": "Owed to you ({{currency}})",
"paid": "Paid out",
+ "payable": "Payable to you",
"pending": "On its way",
"title": "Balance"
},
diff --git a/packages/seller-sdk/src/types/generated/Balance.ts b/packages/seller-sdk/src/types/generated/Balance.ts
index e81942e9a8..2542440253 100644
--- a/packages/seller-sdk/src/types/generated/Balance.ts
+++ b/packages/seller-sdk/src/types/generated/Balance.ts
@@ -1,11 +1,15 @@
-// Typelizer digest 599d7c1a5853edef15f4d9f0ed9cf177
+// Typelizer digest 8ec044a8e36c6f5378bd7c970afeb944
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
type Balance = {
currency: string;
+ settlement_currency: string;
+ converted: boolean;
earned: string;
display_earned: string;
+ payable: string;
+ display_payable: string;
paid: string;
display_paid: string;
balance: string;
diff --git a/packages/seller-sdk/src/types/generated/Transfer.ts b/packages/seller-sdk/src/types/generated/Transfer.ts
index ef19ba153b..9349e6faa9 100644
--- a/packages/seller-sdk/src/types/generated/Transfer.ts
+++ b/packages/seller-sdk/src/types/generated/Transfer.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 13a899486fe193e935361492c1d7a3ef
+// Typelizer digest 4553207a1283578addb2d05b376edc7c
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
@@ -13,6 +13,9 @@ type Transfer = {
updated_at: string;
amount: string;
display_amount: string;
+ settled_amount: string | null;
+ settled_currency: string | null;
+ converted: boolean;
order_id: string | null;
payout_id: string | null;
reversed_from_id: string | null;
diff --git a/spree/api/app/controllers/spree/api/v3/admin/sellers/payouts_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/sellers/payouts_controller.rb
index cc885a3021..e79bf57d44 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/sellers/payouts_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/sellers/payouts_controller.rb
@@ -93,7 +93,7 @@ module Spree
end
def currencies_owed
- @seller.seller_transfers.unsettled.distinct.pluck(:currency)
+ @seller.seller_transfers.unsettled.settlement_currencies
end
def serialize_payout(payout)
diff --git a/spree/api/app/serializers/spree/api/v3/admin/seller_balance_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/seller_balance_serializer.rb
index adebc0d73e..4834309d65 100644
--- a/spree/api/app/serializers/spree/api/v3/admin/seller_balance_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/admin/seller_balance_serializer.rb
@@ -7,18 +7,26 @@ module Spree
class SellerBalanceSerializer < V3::BaseSerializer
typelize seller_id: :string,
currency: :string,
+ settlement_currency: :string,
+ converted: :boolean,
earned: :string, display_earned: :string,
+ payable: :string, display_payable: :string,
paid: :string, display_paid: :string,
balance: :string, display_balance: :string,
pending: :string, display_pending: :string
_attributes.delete(:id)
- attributes :currency
+ # Two sides when the account settles in another currency: what the
+ # sales were worth, and what the account received once the provider
+ # converted. Never added together.
+ attributes :currency, :settlement_currency
+
+ attribute(:converted) { |balance| balance.converted? }
attribute(:seller_id) { |balance| balance.seller&.prefixed_id }
- %i[earned paid balance pending].each do |figure|
+ %i[earned payable paid balance pending].each do |figure|
attribute(figure) { |balance| balance.public_send(figure).to_s }
attribute(:"display_#{figure}") { |balance| balance.public_send(:"display_#{figure}").to_s }
end
diff --git a/spree/api/app/serializers/spree/api/v3/admin/seller_transfer_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/seller_transfer_serializer.rb
index 2845b23e93..f40dc2b012 100644
--- a/spree/api/app/serializers/spree/api/v3/admin/seller_transfer_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/admin/seller_transfer_serializer.rb
@@ -19,6 +19,9 @@ module Spree
provider: [:string, comment: 'Payout provider class name. Built-in: Spree::PayoutProvider::System. Provider gems register more.'],
amount: :string,
currency: :string,
+ settled_amount: 'string | null',
+ settled_currency: 'string | null',
+ converted: :boolean,
reference: 'string | null',
display_amount: :string
@@ -29,6 +32,12 @@ module Spree
attribute(:amount) { |transfer| transfer.amount&.to_s }
attribute(:display_amount) { |transfer| transfer.display_amount.to_s }
+ # What the seller's account received, when the provider converted on
+ # the way in. Null when it settled in the currency of the sale.
+ attribute(:settled_amount) { |transfer| transfer.settled_amount&.to_s }
+ attribute(:settled_currency) { |transfer| transfer.settled_currency }
+ attribute(:converted) { |transfer| transfer.converted? }
+
%i[seller order payout reversed_from refund].each do |association|
attribute(:"#{association}_id") { |transfer| transfer.public_send(association)&.prefixed_id }
end
diff --git a/spree/api/app/serializers/spree/api/v3/seller/balance_serializer.rb b/spree/api/app/serializers/spree/api/v3/seller/balance_serializer.rb
index ece798e2a6..ed57bac6e3 100644
--- a/spree/api/app/serializers/spree/api/v3/seller/balance_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/seller/balance_serializer.rb
@@ -7,16 +7,24 @@ module Spree
# A computed value rather than a record, so it carries no id.
class BalanceSerializer < V3::BaseSerializer
typelize currency: :string,
+ settlement_currency: :string,
+ converted: :boolean,
earned: :string, display_earned: :string,
+ payable: :string, display_payable: :string,
paid: :string, display_paid: :string,
balance: :string, display_balance: :string,
pending: :string, display_pending: :string
_attributes.delete(:id)
- attributes :currency
+ # Two sides when the account settles in another currency: what the
+ # sales were worth, and what the account received once the provider
+ # converted. Never added together.
+ attributes :currency, :settlement_currency
- %i[earned paid balance pending].each do |figure|
+ attribute(:converted) { |balance| balance.converted? }
+
+ %i[earned payable paid balance pending].each do |figure|
attribute(figure) { |balance| balance.public_send(figure).to_s }
attribute(:"display_#{figure}") { |balance| balance.public_send(:"display_#{figure}").to_s }
end
diff --git a/spree/api/app/serializers/spree/api/v3/seller/transfer_serializer.rb b/spree/api/app/serializers/spree/api/v3/seller/transfer_serializer.rb
index bdd76cab70..7cbb22b9fa 100644
--- a/spree/api/app/serializers/spree/api/v3/seller/transfer_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/seller/transfer_serializer.rb
@@ -14,6 +14,9 @@ module Spree
currency: :string,
provider: :string,
amount: :string,
+ settled_amount: 'string | null',
+ settled_currency: 'string | null',
+ converted: :boolean,
display_amount: :string,
reference: 'string | null',
order_id: 'string | null',
@@ -28,6 +31,12 @@ module Spree
attribute(:amount) { |transfer| transfer.amount&.to_s }
attribute(:display_amount) { |transfer| transfer.display_amount.to_s }
+ # What this seller's account received, when the provider converted
+ # on the way in. Null when it settled in the currency of the sale.
+ attribute(:settled_amount) { |transfer| transfer.settled_amount&.to_s }
+ attribute(:settled_currency) { |transfer| transfer.settled_currency }
+ attribute(:converted) { |transfer| transfer.converted? }
+
%i[order payout reversed_from].each do |association|
attribute(:"#{association}_id") { |transfer| transfer.public_send(association)&.prefixed_id }
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/sellers/balances_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/sellers/balances_controller_spec.rb
index fc80113af4..ea346dd9c5 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/sellers/balances_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/sellers/balances_controller_spec.rb
@@ -11,9 +11,13 @@ RSpec.describe Spree::Api::V3::Admin::Sellers::BalancesController, type: :contro
describe 'GET #index' do
it "answers the seller's position per currency" do
- create(:seller_transfer, :completed, seller: seller, amount: 40,
+ create(:seller_transfer, :completed, seller: seller, amount: 25,
+ order: create(:completed_order_with_totals, store: store, seller: seller))
+ # A settlement claims the earnings it covers; one claiming none is not
+ # something the sweep can produce.
+ settled = create(:seller_payout, :completed, seller: seller, amount: 15)
+ create(:seller_transfer, :completed, seller: seller, amount: 15, payout: settled,
order: create(:completed_order_with_totals, store: store, seller: seller))
- create(:seller_payout, :completed, seller: seller, amount: 15)
get :index, params: { seller_id: seller.prefixed_id }, as: :json
diff --git a/spree/api/spec/controllers/spree/api/v3/seller/balances_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/balances_controller_spec.rb
index b19e415850..2808687c79 100644
--- a/spree/api/spec/controllers/spree/api/v3/seller/balances_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/seller/balances_controller_spec.rb
@@ -26,10 +26,13 @@ RSpec.describe Spree::Api::V3::Seller::BalancesController, type: :controller do
describe 'GET #index' do
it 'answers one position per currency' do
- earn(40)
+ earn(25)
earn(10, status: 'pending')
earn(30, currency: 'EUR')
- create(:seller_payout, :completed, seller: seller, amount: 15)
+ # A settlement claims the earnings it covers; one claiming none is not
+ # something the sweep can produce.
+ settled = create(:seller_payout, :completed, seller: seller, amount: 15)
+ earn(15).update!(payout: settled)
# Another seller's ledger must not bleed in.
create(:seller_transfer, :completed, seller: create(:seller, :approved, store: store), amount: 999)
diff --git a/spree/core/app/jobs/spree/seller_payouts/sweep_seller_job.rb b/spree/core/app/jobs/spree/seller_payouts/sweep_seller_job.rb
index 536ddf8771..73fbc58f11 100644
--- a/spree/core/app/jobs/spree/seller_payouts/sweep_seller_job.rb
+++ b/spree/core/app/jobs/spree/seller_payouts/sweep_seller_job.rb
@@ -62,8 +62,11 @@ module Spree
# Only the currencies this seller actually has earnings waiting in — a
# marketplace trading in three currencies should not run three sweeps for
# a seller who sold in one.
+ # The currencies the seller's account can be paid in, which is not the
+ # same as the currencies they sold in — a cross-border account settles
+ # in its own.
def currencies_owed(seller)
- seller.seller_transfers.unsettled.distinct.pluck(:currency)
+ seller.seller_transfers.unsettled.settlement_currencies
end
end
end
diff --git a/spree/core/app/models/spree/seller.rb b/spree/core/app/models/spree/seller.rb
index cb1f7fbc9d..59f59b6fa2 100644
--- a/spree/core/app/models/spree/seller.rb
+++ b/spree/core/app/models/spree/seller.rb
@@ -268,13 +268,17 @@ module Spree
# answer while nobody knows, and it cannot be paid twice by mistake: those
# earnings stay claimed by the settlement holding them until it resolves.
#
- # @param currency [String]
+ # Answered in the currency the money can actually be sent in: a seller whose
+ # account settles in another currency is owed what their account holds, not
+ # what the sale was priced in.
+ #
+ # @param currency [String] a settlement currency
# @return [BigDecimal]
def balance(currency)
- earned = seller_transfers.completed.where(currency: currency).sum(:amount)
- settled = seller_payouts.completed.where(currency: currency).sum(:amount)
+ earnings = seller_transfers.completed.settling_in(currency).includes(:payout).to_a
- earned - settled
+ earnings.sum(&:settlement_amount) -
+ earnings.select { |transfer| transfer.payout&.completed? }.sum(&:settlement_amount)
end
# The seller's position in every currency they have earned or been paid
@@ -284,12 +288,16 @@ module Spree
# every transfer failed would otherwise show a row of zeros on a money
# screen, which reads as a balance rather than as nothing having happened.
#
+ # One position per (sale currency, settlement currency) pair, so a seller
+ # who sells in dollars and banks in pounds sees both sides of the one
+ # relationship rather than two unrelated ledgers.
+ #
# @return [Array<Spree::SellerBalance>]
def balances
- currencies = seller_transfers.where.not(status: 'failed').distinct.pluck(:currency) |
- seller_payouts.completed.distinct.pluck(:currency)
+ pairs = seller_transfers.where.not(status: 'failed').
+ distinct.pluck(:currency, Spree::SellerTransfer.arel_settlement_currency)
- currencies.sort.map { |currency| Spree::SellerBalance.for(self, currency) }
+ pairs.sort.map { |currency, settlement| Spree::SellerBalance.for(self, currency, settlement) }
end
# The seller's account with whichever provider pays them — a Stripe Connect
diff --git a/spree/core/app/models/spree/seller_balance.rb b/spree/core/app/models/spree/seller_balance.rb
index 77c4864e83..aaf5ac27ab 100644
--- a/spree/core/app/models/spree/seller_balance.rb
+++ b/spree/core/app/models/spree/seller_balance.rb
@@ -6,45 +6,76 @@ module Spree
# only presents it. One per currency — a marketplace paying a seller in two
# currencies has two ledgers, and adding them would answer a question nobody
# asked.
+ #
+ # A position has two sides when the seller's account settles in a currency
+ # other than the one they sold in: what the sale was worth, and what their
+ # account received once the provider converted it. Both are recorded figures.
+ # They are never added together, and Spree converts nothing itself.
class SellerBalance
include ActiveModel::Model
include ActiveModel::Attributes
attribute :seller
+ # What the sales were priced in.
attribute :currency, :string
+ # What the seller's account holds, and is paid in. The same as `currency`
+ # unless the provider converted on the way in.
+ attribute :settlement_currency, :string
# What the seller has earned: completed transfers, reversals included.
attribute :earned, :decimal, default: 0
- # What has reached them: completed settlements.
- attribute :paid, :decimal, default: 0
# Earnings still with the provider — a transfer not yet confirmed, or one
# whose outcome is unknown. Shown so a shipped order does not read as
# unpaid while the money is in flight.
attribute :pending, :decimal, default: 0
+ # The same completed earnings, as their account received them.
+ attribute :payable, :decimal, default: 0
+ # How much of that has reached them, through settlements that completed.
+ attribute :paid, :decimal, default: 0
extend Spree::DisplayMoney
- money_methods :earned, :paid, :pending, :balance
+ money_methods :earned, :pending
# @param seller [Spree::Seller]
- # @param currency [String]
+ # @param currency [String] what the sales were priced in
+ # @param settlement_currency [String] what the account settles in
# @return [Spree::SellerBalance]
- def self.for(seller, currency)
- transfers = seller.seller_transfers.where(currency: currency)
+ def self.for(seller, currency, settlement_currency = currency)
+ transfers = seller.seller_transfers.where(currency: currency).settling_in(settlement_currency)
+ completed = transfers.completed.to_a
new(
seller: seller,
currency: currency,
- earned: transfers.completed.sum(:amount),
+ settlement_currency: settlement_currency,
+ earned: completed.sum(&:amount),
pending: transfers.with_status('pending', 'processing', 'unresolved').sum(:amount),
- paid: seller.seller_payouts.completed.where(currency: currency).sum(:amount)
+ payable: completed.sum(&:settlement_amount),
+ # Read through the transfers a settlement claimed rather than off the
+ # settlement's own total: two sale currencies can settle into one, and
+ # the payout's figure would then count against both of them.
+ paid: completed.select { |transfer| transfer.payout&.completed? }.sum(&:settlement_amount)
)
end
- # What the marketplace still owes — the same figure {Spree::Seller#balance}
- # answers, which is what the payout sweep settles.
+ # What the marketplace still owes, in the currency it can actually be sent
+ # in — which is what the payout sweep settles.
#
# @return [BigDecimal]
def balance
- earned - paid
+ payable - paid
+ end
+
+ # @return [Boolean] whether the provider converted this seller's earnings
+ def converted?
+ settlement_currency != currency
+ end
+
+ # Formatted in the settlement currency rather than the sale's, since these
+ # are what the account holds.
+ %i[payable paid balance].each do |figure|
+ define_method(:"display_#{figure}") do
+ Spree::Money.new(public_send(figure), currency: settlement_currency)
+ end
end
end
end
diff --git a/spree/core/app/models/spree/seller_payout.rb b/spree/core/app/models/spree/seller_payout.rb
index 2ff1456c8a..f5f66de4c2 100644
--- a/spree/core/app/models/spree/seller_payout.rb
+++ b/spree/core/app/models/spree/seller_payout.rb
@@ -65,8 +65,10 @@ module Spree
# @return [BigDecimal] what the transfers this payout holds actually come
# to, for checking a stored amount against its parts
+ # Summed on what the seller's account actually received, since that is
+ # what this payout moves.
def transfers_total
- transfers.sum(:amount)
+ transfers.sum(&:settlement_amount)
end
# Gives up on this settlement and puts its earnings back.
diff --git a/spree/core/app/models/spree/seller_transfer.rb b/spree/core/app/models/spree/seller_transfer.rb
index 6d0f39f913..073bd9af4a 100644
--- a/spree/core/app/models/spree/seller_transfer.rb
+++ b/spree/core/app/models/spree/seller_transfer.rb
@@ -81,6 +81,23 @@ module Spree
# Earned and confirmed, but not yet swept into a settlement — what the next
# payout will pick up.
scope :unsettled, -> { completed.where(payout_id: nil) }
+ # Rows whose money can be paid out in this currency. A seller's account
+ # settles in its own currency, so what a payout can move is the settled
+ # currency, not the one the sale was priced in.
+ scope :settling_in, ->(currency) { where(arel_settlement_currency.eq(currency)) }
+
+ # COALESCE rather than two queries, so a row that predates its provider
+ # reporting a settlement still groups under the currency it was earned in.
+ def self.arel_settlement_currency
+ Arel::Nodes::NamedFunction.new('COALESCE', [arel_table[:settled_currency], arel_table[:currency]])
+ end
+
+ # The currencies this seller can actually be paid in.
+ #
+ # @return [Array<String>]
+ def self.settlement_currencies
+ distinct.pluck(arel_settlement_currency)
+ end
self.whitelisted_ransackable_attributes = %w[amount currency kind status provider reference created_at seller_id order_id payout_id]
self.whitelisted_ransackable_associations = %w[seller order payout refund]
@@ -105,5 +122,27 @@ module Spree
def earning?
kind == 'earning'
end
+
+ # What the seller's account actually holds for this row, and in what.
+ #
+ # A sale is priced in the customer's currency; an account settles in its
+ # own, and the provider converts on the way in. Both figures are recorded
+ # facts — Spree holds no exchange rates and converts nothing — and these
+ # answer the earned side whenever no settlement was reported.
+ #
+ # @return [BigDecimal]
+ def settlement_amount
+ settled_amount || amount
+ end
+
+ # @return [String]
+ def settlement_currency
+ settled_currency.presence || currency
+ end
+
+ # @return [Boolean] whether the provider converted this on the way in
+ def converted?
+ settled_currency.present? && settled_currency != currency
+ end
end
end
diff --git a/spree/core/app/workflows/spree/seller_payouts/sweep.rb b/spree/core/app/workflows/spree/seller_payouts/sweep.rb
index 511e6f3fb3..feb1240a09 100644
--- a/spree/core/app/workflows/spree/seller_payouts/sweep.rb
+++ b/spree/core/app/workflows/spree/seller_payouts/sweep.rb
@@ -66,14 +66,17 @@ module Spree
halt!(seller)
end
+ # Selected and summed on what the seller's account holds, not on what the
+ # sale was priced in: a cross-border account settles in its own currency,
+ # and that is the only figure a payout can move.
def collect_transfers
- rows = seller.seller_transfers.unsettled.where(currency: currency).order(:created_at, :id).to_a
+ rows = seller.seller_transfers.unsettled.settling_in(currency).order(:created_at, :id).to_a
halt!(seller) if rows.empty?
@transfers = payable_within(rows)
halt!(seller) if @transfers.empty?
- @amount = @transfers.sum(&:amount)
+ @amount = @transfers.sum(&:settlement_amount)
end
# Every reversal counts, whatever is available: it is money already taken
@@ -84,12 +87,12 @@ module Spree
def payable_within(rows)
return rows if @available.nil?
- reversals, earnings = rows.partition { |row| row.amount.negative? }
- running = reversals.sum(&:amount)
+ reversals, earnings = rows.partition { |row| row.settlement_amount.negative? }
+ running = reversals.sum(&:settlement_amount)
reversals + earnings.take_while do |earning|
- fits = (running + earning.amount) <= @available
- running += earning.amount if fits
+ fits = (running + earning.settlement_amount) <= @available
+ running += earning.settlement_amount if fits
fits
end
end
diff --git a/spree/core/app/workflows/spree/seller_transfers/reverse.rb b/spree/core/app/workflows/spree/seller_transfers/reverse.rb
index 9805e740c0..88894c07c7 100644
--- a/spree/core/app/workflows/spree/seller_transfers/reverse.rb
+++ b/spree/core/app/workflows/spree/seller_transfers/reverse.rb
@@ -155,21 +155,44 @@ module Spree
next nil if bounded <= 0
Spree::SellerTransfer.create!(
- store: @earning.store,
- seller: @earning.seller,
- order: order,
- reversed_from: @earning,
- refund: refund,
- # Negative, so what a seller has earned is the plain sum of the rows.
- amount: -bounded,
- currency: @earning.currency,
- kind: 'refund_reversal',
- provider: @earning.provider,
- status: 'pending'
+ {
+ store: @earning.store,
+ seller: @earning.seller,
+ order: order,
+ reversed_from: @earning,
+ refund: refund,
+ # Negative, so what a seller has earned is the plain sum of the rows.
+ amount: -bounded,
+ currency: @earning.currency,
+ kind: 'refund_reversal',
+ provider: @earning.provider,
+ status: 'pending'
+ }.merge(settlement_of(bounded))
)
end
end
+ # A clawback has to settle where its earning settled. Payouts are swept by
+ # settlement currency, so a reversal left in the sale's currency would
+ # never join the batch that pays the earning it cancels — the seller would
+ # be paid in full for goods that came back, and the row would sit in a
+ # currency their account cannot pay.
+ #
+ # Prorated from the earning's own settled figure rather than converted at
+ # today's rate, so the money comes back at the rate it went out at.
+ def settlement_of(bounded)
+ return {} if @earning.settled_amount.blank? || @earning.amount.zero?
+
+ share = @earning.settled_amount * (bounded / @earning.amount)
+
+ {
+ settled_amount: -Spree::Money::Rounding.quantize(
+ share, Spree::Money::Rounding.precision(@earning.settlement_currency)
+ ),
+ settled_currency: @earning.settled_currency
+ }
+ end
+
def execute_reversal
provider.reverse!(reversal)
rescue Spree::Core::AmbiguousGatewayError => e
diff --git a/spree/core/db/migrate/20260914000001_add_settlement_to_spree_seller_transfers.rb b/spree/core/db/migrate/20260914000001_add_settlement_to_spree_seller_transfers.rb
new file mode 100644
index 0000000000..1a066bc559
--- /dev/null
+++ b/spree/core/db/migrate/20260914000001_add_settlement_to_spree_seller_transfers.rb
@@ -0,0 +1,15 @@
+# What a seller actually received, when their account settles in a currency
+# other than the one the sale was priced in. Nullable because nil is meaningful:
+# no provider has reported a settlement, so the earned figures stand in.
+#
+# This records a conversion the provider performed and reported. Spree still
+# holds no exchange rates and converts nothing itself.
+class AddSettlementToSpreeSellerTransfers < ActiveRecord::Migration[8.1]
+ def change
+ add_column :spree_seller_transfers, :settled_amount, :decimal, precision: 10, scale: 2
+ add_column :spree_seller_transfers, :settled_currency, :string
+
+ add_index :spree_seller_transfers, [:seller_id, :settled_currency, :status],
+ name: 'index_seller_transfers_on_seller_and_settled_currency'
+ end
+end
diff --git a/spree/core/spec/models/spree/seller_balance_spec.rb b/spree/core/spec/models/spree/seller_balance_spec.rb
index 6a6de7ba0f..b7029eed68 100644
--- a/spree/core/spec/models/spree/seller_balance_spec.rb
+++ b/spree/core/spec/models/spree/seller_balance_spec.rb
@@ -17,21 +17,23 @@ RSpec.describe Spree::SellerBalance, type: :model do
earn(25, status: 'pending')
earn(10, status: 'unresolved')
earn(7, status: 'failed')
- create(:seller_payout, :completed, seller: seller, amount: 50)
+ settled = create(:seller_payout, :completed, seller: seller, amount: 50)
+ earn(50).update!(payout: settled)
create(:seller_payout, seller: seller, amount: 15)
balance = described_class.for(seller, 'USD')
- expect(balance.earned).to eq(65)
+ expect(balance.earned).to eq(115)
expect(balance.paid).to eq(50)
- expect(balance.balance).to eq(15)
+ expect(balance.balance).to eq(65)
expect(balance.pending).to eq(35)
- expect(balance.display_balance.to_s).to eq('$15.00')
+ expect(balance.display_balance.to_s).to eq('$65.00')
end
it 'answers what Seller#balance answers' do
earn(40)
- create(:seller_payout, :completed, seller: seller, amount: 10)
+ settled = create(:seller_payout, :completed, seller: seller, amount: 10)
+ earn(10).update!(payout: settled)
expect(described_class.for(seller, 'USD').balance).to eq(seller.balance('USD'))
end
diff --git a/spree/core/spec/models/spree/seller_spec.rb b/spree/core/spec/models/spree/seller_spec.rb
index b3e0ec99c6..c9599c0cd8 100644
--- a/spree/core/spec/models/spree/seller_spec.rb
+++ b/spree/core/spec/models/spree/seller_spec.rb
@@ -373,9 +373,12 @@ describe Spree::Seller do
it 'is what has been earned less what has been settled' do
earn(40)
earn(30)
- create(:seller_payout, :completed, seller: seller, amount: 50)
+ # A settlement claims the earnings it covers; one claiming none is not
+ # something the sweep can produce, and it is the claim that debits.
+ settled = create(:seller_payout, :completed, seller: seller, amount: 50)
+ earn(50).update!(payout: settled)
- expect(seller.balance('USD')).to eq(20)
+ expect(seller.balance('USD')).to eq(70)
end
it 'counts only confirmed earnings' do
@@ -403,10 +406,11 @@ describe Spree::Seller do
it 'is one position per currency the seller was ever paid or earned in, in order' do
earn(40)
earn(30, currency: 'EUR')
- create(:seller_payout, :completed, seller: seller, amount: 5, currency: 'GBP')
+ settled = create(:seller_payout, :completed, seller: seller, amount: 5, currency: 'GBP')
+ earn(5, currency: 'GBP').update!(payout: settled)
expect(seller.balances.map(&:currency)).to eq(%w[EUR GBP USD])
- expect(seller.balances.map(&:balance)).to eq([30, -5, 40])
+ expect(seller.balances.map(&:balance)).to eq([30, 0, 40])
end
it 'is empty before the first sale' do
diff --git a/spree/core/spec/models/spree/seller_transfer_spec.rb b/spree/core/spec/models/spree/seller_transfer_spec.rb
index b97334b490..1238a36871 100644
--- a/spree/core/spec/models/spree/seller_transfer_spec.rb
+++ b/spree/core/spec/models/spree/seller_transfer_spec.rb
@@ -26,6 +26,47 @@ RSpec.describe Spree::SellerTransfer, type: :model do
end
end
+ # A sale is priced in the customer's currency; the seller's account settles in
+ # its own. Both figures are recorded, and the settlement one is what a payout
+ # can actually move.
+ describe 'settlement' do
+ let(:seller) { create(:seller, :approved, store: store) }
+
+ def transfer(**attrs)
+ create(:seller_transfer, seller: seller, currency: 'USD', amount: 69.80,
+ order: create(:order, store: store, seller: seller), **attrs)
+ end
+
+ it 'answers what the account received when the provider converted' do
+ row = transfer(settled_amount: 51.60, settled_currency: 'GBP')
+
+ expect(row.settlement_amount).to eq(51.60)
+ expect(row.settlement_currency).to eq('GBP')
+ expect(row).to be_converted
+ end
+
+ it 'falls back to the sale when no settlement was reported' do
+ row = transfer
+
+ expect(row.settlement_amount).to eq(69.80)
+ expect(row.settlement_currency).to eq('USD')
+ expect(row).not_to be_converted
+ end
+
+ it 'is not a conversion when the provider settled in the same currency' do
+ expect(transfer(settled_amount: 69.80, settled_currency: 'USD')).not_to be_converted
+ end
+
+ it 'groups by the currency a payout could send' do
+ converted = transfer(settled_amount: 51.60, settled_currency: 'GBP')
+ plain = transfer
+
+ expect(Spree::SellerTransfer.settling_in('GBP')).to contain_exactly(converted)
+ expect(Spree::SellerTransfer.settling_in('USD')).to contain_exactly(plain)
+ expect(Spree::SellerTransfer.where(seller: seller).settlement_currencies).to match_array(%w[GBP USD])
+ end
+ end
+
describe '#reversible_amount' do
let(:earning) { create(:seller_transfer, :completed, seller: seller, order: order, amount: 40) }
diff --git a/spree/core/spec/workflows/spree/seller_payouts/sweep_spec.rb b/spree/core/spec/workflows/spree/seller_payouts/sweep_spec.rb
index 3ac925cd61..0b3a754905 100644
--- a/spree/core/spec/workflows/spree/seller_payouts/sweep_spec.rb
+++ b/spree/core/spec/workflows/spree/seller_payouts/sweep_spec.rb
@@ -190,6 +190,57 @@ RSpec.describe Spree::SellerPayouts::Sweep do
end
end
+ # A cross-border account settles in its own currency, so what a payout can
+ # move is the settled figure — the sale's currency is money that account can
+ # never send.
+ describe 'when the seller banks in another currency' do
+ def earn_converted(amount, settled, currency: 'USD', settled_currency: 'GBP', **attrs)
+ create(:seller_transfer, seller: seller, currency: currency, amount: amount, status: 'completed',
+ settled_amount: settled, settled_currency: settled_currency,
+ order: create(:order, store: store, seller: seller, currency: currency), **attrs)
+ end
+
+ it 'settles in the currency the account holds' do
+ earn_converted(69.80, 51.60)
+
+ payout = described_class.call(seller: seller, currency: 'GBP').value
+
+ expect(payout.currency).to eq('GBP')
+ expect(payout.amount).to eq(51.60)
+ end
+
+ it 'has nothing to settle in the currency the sale was priced in' do
+ earn_converted(69.80, 51.60)
+
+ expect(described_class.call(seller: seller, currency: 'USD').value).to eq(seller)
+ end
+
+ # Left in the sale's currency a clawback would never join the batch that
+ # pays the earning it cancels, and the seller would keep both.
+ it 'nets a clawback that settled alongside its earning' do
+ earning = earn_converted(69.80, 51.60)
+ earn_converted(-21.60, -15.97, kind: 'refund_reversal', reversed_from: earning)
+
+ expect(described_class.call(seller: seller, currency: 'GBP').value.amount).to eq(35.63)
+ end
+
+ # The ceiling comes back from the provider in the account's own currency,
+ # so the rows it is compared against have to be in that currency too.
+ it 'compares what is available against the settled figures' do
+ earn_converted(69.80, 51.60)
+ allow_any_instance_of(Spree::PayoutProvider::System).to receive(:available_payout).and_return(40)
+
+ expect { described_class.call(seller: seller, currency: 'GBP') }.
+ not_to change { Spree::SellerPayout.count }
+ end
+
+ it 'leaves a seller who banks in the currency they sold in alone' do
+ earn(40)
+
+ expect(described_class.call(seller: seller, currency: 'USD').value.amount).to eq(40)
+ end
+ end
+
describe 'when the provider will not pay' do
before do
allow_any_instance_of(Spree::PayoutProvider::System).to receive(:pay!).and_raise(StandardError, 'gateway down')
diff --git a/spree/core/spec/workflows/spree/seller_transfers/reverse_spec.rb b/spree/core/spec/workflows/spree/seller_transfers/reverse_spec.rb
index 91a5323ab9..5de90096cf 100644
--- a/spree/core/spec/workflows/spree/seller_transfers/reverse_spec.rb
+++ b/spree/core/spec/workflows/spree/seller_transfers/reverse_spec.rb
@@ -143,6 +143,35 @@ RSpec.describe Spree::SellerTransfers::Reverse do
end
end
+ # Payouts are swept by settlement currency. A clawback left in the sale's
+ # currency would never join the batch paying the earning it cancels.
+ describe 'when the earning settled in another currency' do
+ it 'takes the money back where the earning landed' do
+ create(:seller_transfer, :completed, seller: seller, order: order, amount: 80,
+ settled_amount: 60, settled_currency: 'GBP')
+
+ reversal = described_class.call(order: order, amount: 30).value
+
+ expect(reversal.settled_currency).to eq('GBP')
+ expect(reversal.currency).to eq('USD')
+ end
+
+ # 30 refunded on a 100 order that earned 80 claws back 24, which at the rate
+ # that earning actually settled at is 18 of the 60 that arrived.
+ it 'claws back at the rate the money went out at' do
+ create(:seller_transfer, :completed, seller: seller, order: order, amount: 80,
+ settled_amount: 60, settled_currency: 'GBP')
+
+ expect(described_class.call(order: order, amount: 30).value.settled_amount).to eq(-18)
+ end
+
+ it 'records no settlement when the earning had none' do
+ earn(80)
+
+ expect(described_class.call(order: order, amount: 30).value.settled_amount).to be_nil
+ end
+ end
+
describe 'what it will not take back' do
it 'refuses to claw back more than was credited' do
earn(80)
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 915a6738f2..dc00ba561b 100644
--- a/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb
+++ b/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb
@@ -176,12 +176,18 @@ module SpreeStripe
metadata: {
spree_seller_transfer_id: seller_transfer.id,
spree_order_number: seller_transfer.order.number
- }
+ },
+ # What the seller's own account received. An account settles in its
+ # own currency and Stripe converts on the way in, so the sale's
+ # figure is not the payable one.
+ expand: ['destination_payment.balance_transaction']
}.compact,
gateway.api_options.merge(idempotency_key: idempotency_key(seller_transfer))
)
- seller_transfer.update!(status: 'completed', reference: transfer.id)
+ seller_transfer.update!(
+ { status: 'completed', reference: transfer.id }.merge(settlement_of(transfer))
+ )
seller_transfer
rescue *AMBIGUOUS_ERRORS => e
# As with a settlement: the transfer may have been made before the answer
@@ -273,6 +279,23 @@ module SpreeStripe
private
+ # What landed on the seller's account, read off the balance transaction of
+ # the payment the transfer created there. Empty when Stripe did not expand
+ # it, which leaves the ledger reading the earned figures rather than
+ # inventing a settlement.
+ #
+ # @return [Hash]
+ def settlement_of(transfer)
+ balance_transaction = transfer.try(:destination_payment).try(:balance_transaction)
+ return {} if balance_transaction.blank? || balance_transaction.try(:currency).blank?
+
+ {
+ settled_amount: Spree::Money::Rounding.from_minor_units(balance_transaction.amount,
+ balance_transaction.currency),
+ settled_currency: balance_transaction.currency.upcase
+ }
+ end
+
def complete_without_sending(seller_transfer)
seller_transfer.update!(status: 'completed')
seller_transfer
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 4194d2a4b6..fb3aaabbe5 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
@@ -166,6 +166,53 @@ RSpec.describe SpreeStripe::PayoutProvider do
end
end
+ # An account settles in its own currency and Stripe converts on the way in, so
+ # what the ledger owes and what the account can send are different figures.
+ describe 'what the seller actually received' do
+ def transfer_settling(minor, currency)
+ Stripe::StripeObject.construct_from(
+ id: 'tr_1',
+ destination_payment: { balance_transaction: { amount: minor, currency: currency } }
+ )
+ end
+
+ it 'records the converted figure the account received' do
+ allow(Stripe::Transfer).to receive(:create).and_return(transfer_settling(5_160, 'gbp'))
+
+ described_class.new.transfer!(seller_transfer)
+
+ expect(seller_transfer.reload.settled_amount).to eq(51.60)
+ expect(seller_transfer.settled_currency).to eq('GBP')
+ end
+
+ it 'asks Stripe for it, since the transfer alone does not say' do
+ expect(Stripe::Transfer).to receive(:create).
+ with(hash_including(expand: ['destination_payment.balance_transaction']), anything).
+ and_return(transfer_settling(4_250, 'usd'))
+
+ described_class.new.transfer!(seller_transfer)
+ end
+
+ it 'records a same-currency settlement too, so nothing has to be inferred' do
+ allow(Stripe::Transfer).to receive(:create).and_return(transfer_settling(4_250, 'usd'))
+
+ described_class.new.transfer!(seller_transfer)
+
+ expect(seller_transfer.reload.settled_currency).to eq('USD')
+ expect(seller_transfer).not_to be_converted
+ end
+
+ # Better to read the earned figure than to invent a settlement.
+ it 'leaves it unrecorded when Stripe did not say' do
+ allow(Stripe::Transfer).to receive(:create).and_return(Stripe::StripeObject.construct_from(id: 'tr_1'))
+
+ described_class.new.transfer!(seller_transfer)
+
+ expect(seller_transfer.reload.settled_amount).to be_nil
+ expect(seller_transfer.settlement_currency).to eq('USD')
+ end
+ end
+
describe '#pay!' do
let(:payout) { create(:seller_payout, seller: seller, amount: 42.5, currency: 'USD') }