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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix: A payout the bank sent back still read as settled
Łukasz Adamczyk840bee1651d5
diff --git a/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rb b/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rbindex 24c1b31162..9bf1437ea9 100644--- a/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rb+++ b/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rb@@ -220,11 +220,19 @@ module SpreeStripereturn if seller.nil?payout = find_payout(seller, object)- return if payout.nil? || payout.completed?+ return if payout.nil?if status == 'paid'+ # A redelivery of a settlement already completed changes nothing.+ return if payout.completed?+Spree.seller_payout_complete_workflow.call(seller_payout: payout, reference: object.id)else+ # Answered even on a completed settlement. A bank can return a payout+ # days after it was paid, and the money is back in the seller's+ # balance whether or not our books had moved on — left alone, the+ # ledger reports a seller settled while Stripe still holds their+ # earnings. Failing it releases them for the next sweep to send again.payout.fail!endenddiff --git a/spree/providers/stripe/spec/models/spree_stripe/gateway/connect_spec.rb b/spree/providers/stripe/spec/models/spree_stripe/gateway/connect_spec.rbindex b19c308409..753e88e079 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@@ -161,6 +161,32 @@ RSpec.describe SpreeStripe::Gateway::Connect doexpect(seller.seller_transfers.unsettled.sum(:amount)).to eq(40)end+ # A bank can return a payout days after paying it — a closed account, a+ # rejected transfer — and Stripe says so with payout.failed. Ignoring it+ # because our books had already moved on leaves the ledger reporting a+ # seller settled while Stripe holds their earnings.+ context 'for a settlement already marked paid' do+ let!(:payout) do+ create(:seller_payout, seller: seller, currency: 'USD', reference: 'po_1', status: 'completed')+ end++ it 'takes the settlement back' do+ gateway.handle_payout_webhook(raw_body, headers)++ expect(payout.reload).to be_failed+ end++ it 'owes the seller again, since the money is back in their balance' do+ create(:seller_transfer, :completed, seller: seller, payout: payout, amount: 40,+ order: create(:order, store: store, seller: seller))++ gateway.handle_payout_webhook(raw_body, headers)++ expect(seller.seller_transfers.unsettled.sum(:amount)).to eq(40)+ expect(seller.balance('USD')).to eq(40)+ end+ end+# The settlement whose outcome was never known has been holding its# earnings precisely so nothing could send them twice. Stripe saying the# money did not move is the definite answer that frees them.