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: Seller transfers refused for every split checkout
Łukasz Adamczykc986661bf17f
diff --git a/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb b/spree/providers/stripe/app/models/spree_stripe/payout_provider.rbindex 20b79d7a6d..ecb0d6aec5 100644--- a/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb+++ b/spree/providers/stripe/app/models/spree_stripe/payout_provider.rb@@ -160,6 +160,7 @@ module SpreeStripeseller = seller_transfer.sellergateway = gateway_for(seller.store)+ source_charge = source_charge_for(seller_transfer, gateway)transfer = Stripe::Transfer.create({@@ -168,8 +169,10 @@ module SpreeStripedestination: seller.payout_account_reference(self.class),# Funds the transfer from the customer's own charge, so it settles# with that charge rather than out of the platform's balance.- source_transaction: source_charge_for(seller_transfer, gateway),- transfer_group: seller_transfer.order.order_group&.number || seller_transfer.order.number,+ source_transaction: source_charge,+ # Stripe refuses a second group on a transfer drawn from a charge+ # that already carries one, and the intent is grouped at checkout.+ transfer_group: (transfer_group_for(seller_transfer) if source_charge.blank?),metadata: {spree_seller_transfer_id: seller_transfer.id,spree_order_number: seller_transfer.order.number@@ -270,6 +273,12 @@ module SpreeStripeSpree::Money::Rounding.to_minor_units(record.amount.abs, record.currency)end+ # Ties transfers paid from the platform balance to the checkout they+ # settle — a split checkout's sellers share their group's number.+ def transfer_group_for(seller_transfer)+ seller_transfer.order.order_group&.number || seller_transfer.order.number+ end+# 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.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.rbindex adcee0f549..5cf77aadb2 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@@ -31,6 +31,55 @@ RSpec.describe SpreeStripe::PayoutProvider dodescribed_class.new.transfer!(seller_transfer)end+ # "You cannot use `transfer_group` if the `source_transaction` already has+ # one set" — which refused every split checkout's transfers.+ context 'when the customer\'s own charge funds it' do+ let(:payment) do+ create(:payment, order: order, payment_method: gateway, amount: order.total,+ status: 'completed', metadata: { 'stripe_charge_id' => 'ch_1' })+ end++ before { payment }++ it 'draws on the charge and leaves its group alone' do+ expect(Stripe::Transfer).to receive(:create) do |payload, _options|+ expect(payload[:source_transaction]).to eq('ch_1')+ expect(payload).not_to have_key(:transfer_group)+ Stripe::StripeObject.construct_from(id: 'tr_1')+ end++ described_class.new.transfer!(seller_transfer)+ end+ end++ # Nothing else ties these together, so the group is worth naming.+ context 'when it is paid out of the platform balance' do+ it 'names the order it settles' do+ expect(Stripe::Transfer).to receive(:create) do |payload, _options|+ expect(payload).not_to have_key(:source_transaction)+ expect(payload[:transfer_group]).to eq(order.number)+ Stripe::StripeObject.construct_from(id: 'tr_1')+ end++ described_class.new.transfer!(seller_transfer)+ end++ # A split checkout's sellers each get their own transfer, and the group's+ # number is what reads them back as one basket.+ it 'names the whole checkout when the order was placed alongside others' do+ group = create(:order_group, store: store)+ order.update!(order_group: group)++ expect(Stripe::Transfer).to receive(:create) do |payload, _options|+ expect(payload[:transfer_group]).to eq(group.number)+ expect(payload[:transfer_group]).not_to eq(order.number)+ Stripe::StripeObject.construct_from(id: 'tr_1')+ end++ described_class.new.transfer!(seller_transfer)+ end+ end+# A retry after a timeout must find the movement it already made rather# than making a second one.it 'carries an idempotency key derived from the ledger row' do