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: Split-checkout orders could never be fulfilled
Łukasz Adamczykdbb074eabbc6
diff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rbindex 01977a12ae..b433ca0ad9 100644--- a/spree/core/app/models/spree/order.rb+++ b/spree/core/app/models/spree/order.rb@@ -697,6 +697,29 @@ module Spreeorder_group_id.present? ? order_group.payments : paymentsend+ # What has been captured against this order, net of refunds.+ #+ # An order placed in a split checkout owns no payments, so its own+ # +payment_total+ stays at zero however much the customer paid — the+ # figure comes from its share of the group's payments instead, the same+ # way {Spree::Orders::UpdateStatuses} derives +payment_status+. The+ # group's own total will not do: once one seller has been captured and+ # another has not, no proportion of it describes either.+ #+ # @return [BigDecimal]+ def net_captured_total+ return payment_total unless grouped?++ payment_splits.sum(&:net_captured_amount)+ end++ # Payments still to be collected for this order, wherever they live.+ #+ # @return [ActiveRecord::Relation<Spree::Payment>, Array<Spree::Payment>]+ def settlement_pending_payments+ grouped? ? settlement_payments.pending : pending_payments+ end+# @return [Boolean] whether this order was placed alongside others in one# checkout, and therefore shares their paymentdef grouped?diff --git a/spree/core/app/workflows/spree/fulfillments/fulfill.rb b/spree/core/app/workflows/spree/fulfillments/fulfill.rbindex cf52e1898a..f78383cfbc 100644--- a/spree/core/app/workflows/spree/fulfillments/fulfill.rb+++ b/spree/core/app/workflows/spree/fulfillments/fulfill.rb@@ -140,7 +140,7 @@ module Spree# a fully discounted or store-credit-paid order was refused dispatch.# Measured against what was owed at checkout, so an arrangement that# collects part of the total up front can still ship.- return if order.payment_total >= order.amount_due_at_checkout+ return if order.net_captured_total >= order.amount_due_at_checkout# Charging later is a deliberate choice, so an authorized-but-uncaptured# order is ready to hand over: on dispatch the money is taken below,@@ -149,7 +149,7 @@ module Spree# Every pending payment has to defer, not just one — on a mixed-tender# order a single deferred payment must not wave through a sibling that# should have been collected at checkout.- pending = order.pending_payments+ pending = order.settlement_pending_paymentsreturn if pending.any? && pending.all? { |payment| payment.payment_method&.capture_at_checkout? == false }failure(@source, Spree.t('fulfillments.errors.order_not_paid'))diff --git a/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb b/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rbindex c27bb51c87..2a12fea22b 100644--- a/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb+++ b/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb@@ -386,6 +386,42 @@ module Spreeendend+ # An order placed in a split checkout owns no payments — the customer paid+ # once, against the group — so its own payment_total is zero and the guard+ # refused to dispatch anything a marketplace ever sold.+ describe 'an order sharing its payment with siblings' do+ let(:group) { create(:order_group, store: store) }+ let(:payment) { create(:payment, amount: order.total * 2, status: 'completed') }++ before do+ order.payments.delete_all+ order.update_columns(order_group_id: group.id, payment_total: 0)+ create(:payment_split, payment: payment, order: order,+ authorized_amount: order.total, captured_amount: order.total)+ end++ it 'hands over when its share of the payment covers what it owed' do+ expect(subject.call(fulfillment: fulfillment)).to be_success+ end++ it 'refuses when its share was never captured' do+ order.payment_splits.update_all(captured_amount: 0)++ result = subject.call(fulfillment: fulfillment)++ expect(result).to be_failure+ expect(result.error.value).to include(Spree.t('fulfillments.errors.order_not_paid'))+ end++ # Gross capture is not the measure: a share given back is money the+ # order no longer holds.+ it 'refuses when its share was captured and then refunded' do+ order.payment_splits.update_all(refunded_amount: order.total)++ expect(subject.call(fulfillment: fulfillment)).to be_failure+ end+ end+describe 'capturing payment on dispatch' do# The factory captures at checkout; undo that so the payment is merely# authorized, which is the state this setting exists to resolve.