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.

Tell core when a transfer's outcome is unknown, as a payout already does

Damian Legawiec76618deff160
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 1724cd7564..20b79d7a6d 100644
--- a/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb
+++ b/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb
@@ -27,6 +27,24 @@ module SpreeStripe
# go round the same flow again.
PENDING_REASONS = %w[under_review requirements.pending_verification platform_paused].freeze
+ # Failures that leave the answer with Stripe rather than in the response,
+ # so core must not assume the money stayed put.
+ #
+ # A dropped connection or a timeout may have arrived all the same. An
+ # `APIError` is Stripe's own 5xx, which says nothing about whether the
+ # request was processed before it broke. An `IdempotencyError` means the
+ # key is already held against different parameters, so *something* was
+ # recorded under it.
+ #
+ # `Stripe::APIError` is spelled with a capitalised initialism; there is no
+ # `Stripe::ApiError`, and naming one in a rescue raises NameError at the
+ # moment the rescue is needed.
+ AMBIGUOUS_ERRORS = [
+ Stripe::APIConnectionError,
+ Stripe::APIError,
+ Stripe::IdempotencyError
+ ].freeze
+
def self.display_name
'Stripe Connect'
end
@@ -162,6 +180,12 @@ module SpreeStripe
seller_transfer.update!(status: 'completed', reference: transfer.id)
seller_transfer
+ rescue *AMBIGUOUS_ERRORS => e
+ # As with a settlement: the transfer may have been made before the answer
+ # was lost, so core parks the earning rather than offering it to the
+ # retry job, which would send the same money again once Stripe has
+ # forgotten the key.
+ raise Spree::Core::AmbiguousGatewayError, e.message
end
# Sends a settlement from the seller's connected account to their bank.
@@ -195,7 +219,7 @@ module SpreeStripe
# matches on Stripe's own id instead of guessing by amount.
seller_payout.update!(reference: payout.id)
seller_payout
- rescue Stripe::APIConnectionError, Stripe::IdempotencyError => e
+ rescue *AMBIGUOUS_ERRORS => e
# Nobody knows whether the money moved. The request may have reached
# Stripe and created the payout before the answer was lost, so core must
# not put these earnings back on the pile — the idempotency key stops a
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 d8c6238f77..adcee0f549 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
@@ -61,6 +61,31 @@ RSpec.describe SpreeStripe::PayoutProvider do
expect { described_class.new.transfer!(seller_transfer) }.to raise_error(Spree::Core::GatewayError)
end
+ # Core parks an earning it cannot account for so nothing retries it, but
+ # it can only do that if the provider says which kind of failure this was.
+ context 'when the answer is lost on the way back' do
+ [
+ [Stripe::APIConnectionError, 'timed out'],
+ [Stripe::APIError, 'internal error'],
+ [Stripe::IdempotencyError, 'key reused']
+ ].each do |error_class, message|
+ it "reports #{error_class} as an outcome nobody knows" do
+ allow(Stripe::Transfer).to receive(:create).and_raise(error_class, message)
+
+ expect { described_class.new.transfer!(seller_transfer) }.
+ to raise_error(Spree::Core::AmbiguousGatewayError)
+ end
+ end
+ end
+
+ # A refusal is definite: the money did not move, and the earning is owed.
+ it 'lets an outright refusal through as itself' do
+ allow(Stripe::Transfer).to receive(:create).
+ and_raise(Stripe::InvalidRequestError.new('no such destination', 'destination'))
+
+ expect { described_class.new.transfer!(seller_transfer) }.to raise_error(Stripe::InvalidRequestError)
+ end
+
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.
@@ -189,6 +214,15 @@ RSpec.describe SpreeStripe::PayoutProvider do
expect { described_class.new.pay!(payout) }.
to raise_error(Spree::Core::AmbiguousGatewayError)
end
+
+ # Stripe's own 5xx says nothing about whether it processed the request
+ # before it broke.
+ it 'says the same when Stripe itself errors' do
+ allow(Stripe::Payout).to receive(:create).and_raise(Stripe::APIError, 'internal error')
+
+ expect { described_class.new.pay!(payout) }.
+ to raise_error(Spree::Core::AmbiguousGatewayError)
+ end
end
# A refusal is definite, and core is right to release the earnings for it.