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.

Fund a transfer from the charge, and refuse a reversal we cannot make

Damian Legawiec689996a27441
diff --git a/spree/core/app/workflows/spree/seller_transfers/reverse.rb b/spree/core/app/workflows/spree/seller_transfers/reverse.rb
index c3fd27ff99..8cbd0e8318 100644
--- a/spree/core/app/workflows/spree/seller_transfers/reverse.rb
+++ b/spree/core/app/workflows/spree/seller_transfers/reverse.rb
@@ -126,7 +126,16 @@ module Spree
@provider ||= begin
configured = Spree.payout_providers.find { |candidate| candidate.to_s == @earning.provider }
- (configured || order.store.payout_provider_class).new
+ # No silent fallback to whatever the store uses now. A provider that
+ # is no longer installed still holds the transfer this reverses, and
+ # handing the job to a different one would mark the row reversed
+ # while the money stayed where it was. An operator has to know.
+ if configured.nil?
+ raise Spree::Core::GatewayError,
+ "Payout provider #{@earning.provider} is not registered, so its transfer cannot be reversed"
+ end
+
+ configured.new
end
end
end
diff --git a/spree/core/lib/spree/testing_support/factories/seller_payout_factory.rb b/spree/core/lib/spree/testing_support/factories/seller_payout_factory.rb
index 8cd99f259f..387d3dd6ae 100644
--- a/spree/core/lib/spree/testing_support/factories/seller_payout_factory.rb
+++ b/spree/core/lib/spree/testing_support/factories/seller_payout_factory.rb
@@ -4,7 +4,7 @@ FactoryBot.define do
store { seller.store }
amount { BigDecimal(20) }
currency { 'USD' }
- provider { 'system' }
+ provider { Spree::PayoutProvider::System.provider_key }
status { 'pending' }
trait :completed do
diff --git a/spree/core/lib/spree/testing_support/factories/seller_transfer_factory.rb b/spree/core/lib/spree/testing_support/factories/seller_transfer_factory.rb
index 9ff90387d0..dc674d25ae 100644
--- a/spree/core/lib/spree/testing_support/factories/seller_transfer_factory.rb
+++ b/spree/core/lib/spree/testing_support/factories/seller_transfer_factory.rb
@@ -6,7 +6,7 @@ FactoryBot.define do
amount { BigDecimal(20) }
currency { 'USD' }
kind { 'earning' }
- provider { 'system' }
+ provider { Spree::PayoutProvider::System.provider_key }
status { 'pending' }
trait :completed do
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 6f742bc708..54e41d0faa 100644
--- a/spree/core/spec/workflows/spree/seller_transfers/reverse_spec.rb
+++ b/spree/core/spec/workflows/spree/seller_transfers/reverse_spec.rb
@@ -152,6 +152,29 @@ RSpec.describe Spree::SellerTransfers::Reverse do
end
end
+ # A provider that is no longer installed still holds the transfer. Handing
+ # the reversal to whichever one the store uses now would mark the row
+ # reversed while the money stayed where it was.
+ describe 'when the provider that made the earning is gone' do
+ it 'refuses rather than reversing through somebody else' do
+ create(:seller_transfer, :completed, seller: seller, order: order, amount: 80,
+ provider: 'SpreeGone::PayoutProvider')
+
+ result = described_class.call(order: order, amount: 30)
+
+ expect(result).to be_failure
+ end
+
+ it 'leaves the row for an operator to see' do
+ create(:seller_transfer, :completed, seller: seller, order: order, amount: 80,
+ provider: 'SpreeGone::PayoutProvider')
+
+ described_class.call(order: order, amount: 30)
+
+ expect(Spree::SellerTransfer.reversals_only.last).to be_processing
+ end
+ end
+
# A settlement that has happened is never rewritten: the reversal is simply
# unsettled, so the next sweep nets it off what comes after.
describe 'when the earning was already settled' do
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 a018f37baa..12a78c3cbc 100644
--- a/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb
+++ b/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb
@@ -255,11 +255,15 @@ module SpreeStripe
# The charge that paid for this order, so Stripe can fund the transfer from
# it. Nil when the order was not paid by card — a store-credit sale has no
# charge to draw on, and Stripe then funds from the platform balance.
+ # Stripe funds a transfer from a *charge*, and `response_code` holds the
+ # payment intent rather than the charge it produced — sending that would
+ # fail every card-funded transfer on an id Stripe cannot use. The charge
+ # is recorded on the payment when its session completes.
def source_charge_for(seller_transfer, gateway)
payment = seller_transfer.order.settlement_payments.valid.completed.
find { |candidate| candidate.payment_method_id == gateway.id }
- payment&.response_code
+ payment&.metadata&.dig('stripe_charge_id')
end
end
end
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 3dc80fbc77..e04ee01f2d 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
@@ -64,9 +64,13 @@ RSpec.describe SpreeStripe::PayoutProvider do
context 'when the customer paid by card' do
# Funds the transfer from that charge, so it settles with the charge
# rather than out of the marketplace's own balance.
+ # Stripe funds a transfer from a charge. `response_code` holds the
+ # payment intent that produced it, which Stripe cannot use here — the
+ # charge is recorded on the payment when its session completes.
it 'names the charge as the funding source' do
create(:payment, order: order, payment_method: gateway, status: 'completed',
- response_code: 'ch_123', amount: 100)
+ response_code: 'pi_123', amount: 100,
+ metadata: { 'stripe_charge_id' => 'ch_123' })
expect(Stripe::Transfer).to receive(:create).
with(hash_including(source_transaction: 'ch_123'), anything).