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
Added a replaceable rule for what checkout must collect
Damian Legawiecbee7163462ea
diff --git a/spree/core/app/models/concerns/spree/purchase/totals.rb b/spree/core/app/models/concerns/spree/purchase/totals.rbindex 991252b9a3..efad64bdcd 100644--- a/spree/core/app/models/concerns/spree/purchase/totals.rb+++ b/spree/core/app/models/concerns/spree/purchase/totals.rb@@ -57,6 +57,16 @@ module Spreetotal.positive? && payment_total >= totalend+ # What has to be paid before this purchase can be placed and+ # dispatched — the whole total unless an arrangement collects only part+ # of it up front. See {Spree::Purchases::AmountDueAtCheckout}, which is+ # where a deposit or net terms would answer differently.+ #+ # @return [BigDecimal]+ def amount_due_at_checkout+ Spree.purchase_amount_due_at_checkout_service.new.call(purchase: self)+ end+# Total fulfillment discount applied by promotions, as a positive amount.## @return [BigDecimal]diff --git a/spree/core/app/models/spree/checkout/default_requirements.rb b/spree/core/app/models/spree/checkout/default_requirements.rbindex 972aaa8364..a925c6029a 100644--- a/spree/core/app/models/spree/checkout/default_requirements.rb+++ b/spree/core/app/models/spree/checkout/default_requirements.rb@@ -98,8 +98,10 @@ module Spree@cart.payment_required?end+ # What checkout has to collect, which is the whole total unless an+ # arrangement collects only part of it up front.def payment_satisfied?- @cart.payments.valid.sum(:amount) >= @cart.total+ @cart.payments.valid.sum(:amount) >= @cart.amount_due_at_checkoutend# Advisory rather than completion-only: the buyer has to be told thediff --git a/spree/core/app/services/spree/purchases/amount_due_at_checkout.rb b/spree/core/app/services/spree/purchases/amount_due_at_checkout.rbnew file mode 100644index 0000000000..36ec0a8653--- /dev/null+++ b/spree/core/app/services/spree/purchases/amount_due_at_checkout.rb@@ -0,0 +1,33 @@+module Spree+ module Purchases+ # How much of a purchase has to be paid before it can be placed and+ # dispatched.+ #+ # The whole total, always, in open source: a buyer pays for what they are+ # taking. The class exists so that arrangements which collect only part of+ # it — a deposit up front with the balance owed later, net terms — can be+ # honoured without patching the half-dozen places that decide whether+ # enough money has arrived.+ #+ # Replace it through {Spree::Dependencies}, the same way a rate provider+ # or a workflow is replaced:+ #+ # Spree::Dependencies.purchase_amount_due_at_checkout_service =+ # 'MyApp::AmountDueOnDepositTerms'+ #+ # A replacement answers a decimal in the purchase's own currency, and must+ # never answer more than the total — asking for more than the goods are+ # worth is refused at checkout rather than collected.+ #+ # Deposits and net terms are designed in+ # docs/plans/6.0-6.1-b2b-payment-terms.md; nothing in open source resolves+ # them, so this is the whole implementation here.+ class AmountDueAtCheckout+ # @param purchase [Spree::Cart, Spree::Order]+ # @return [BigDecimal]+ def call(purchase:)+ purchase.total.to_d+ end+ end+ end+enddiff --git a/spree/core/app/workflows/spree/carts/complete.rb b/spree/core/app/workflows/spree/carts/complete.rbindex 0a9d51f2fb..a0bfb7fce1 100644--- a/spree/core/app/workflows/spree/carts/complete.rb+++ b/spree/core/app/workflows/spree/carts/complete.rb@@ -154,7 +154,10 @@ module Spreeenddef create_draft_order- @order = create_draft_order!(cart)+ # Reloaded because the copier writes the totals with update_columns,+ # which leaves this instance holding the zeros it was built with —+ # and the payment decision immediately below is made against them.+ @order = create_draft_order!(cart).reloadenddef process_payments@@ -387,11 +390,12 @@ module Spreeorder.payments.resetend- # Completion gates on "a valid payment exists covering the total",- # never on paid? — a net-terms order completes with a pending payment.+ # Completion gates on "a valid payment exists covering what is owed at+ # checkout", never on paid? — a net-terms order completes with a+ # pending payment.def payment_covered?(order)order.payments.reset- order.payments.valid.where(status: %w[pending processing completed]).sum(:amount) >= order.total+ order.payments.valid.where(status: %w[pending processing completed]).sum(:amount) >= order.amount_due_at_checkoutend# The FINALIZE phase: the order-side completion workflow owns thediff --git a/spree/core/app/workflows/spree/fulfillments/fulfill.rb b/spree/core/app/workflows/spree/fulfillments/fulfill.rbindex 111d3587be..cf52e1898a 100644--- a/spree/core/app/workflows/spree/fulfillments/fulfill.rb+++ b/spree/core/app/workflows/spree/fulfillments/fulfill.rb@@ -135,7 +135,12 @@ module Spreefailure(@source, Spree.t('fulfillments.errors.backordered_units'))end- return if order.paid?+ # An order that owes nothing is ready to hand over, which the old+ # paid-in-full guard denied it: `paid?` requires a positive total, so+ # 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# Charging later is a deliberate choice, so an authorized-but-uncaptured# order is ready to hand over: on dispatch the money is taken below,diff --git a/spree/core/app/workflows/spree/orders/complete.rb b/spree/core/app/workflows/spree/orders/complete.rbindex 2d2fc2cb09..f73d79ed7e 100644--- a/spree/core/app/workflows/spree/orders/complete.rb+++ b/spree/core/app/workflows/spree/orders/complete.rb@@ -162,7 +162,7 @@ module Spreedef payment_covered?order.payments.reset- order.payments.valid.where(status: %w[pending processing completed]).sum(:amount) >= order.total+ order.payments.valid.where(status: %w[pending processing completed]).sum(:amount) >= order.amount_due_at_checkoutendendenddiff --git a/spree/core/lib/spree/core/dependencies.rb b/spree/core/lib/spree/core/dependencies.rbindex 893f8845fd..d677ab032e 100644--- a/spree/core/lib/spree/core/dependencies.rb+++ b/spree/core/lib/spree/core/dependencies.rb@@ -214,6 +214,11 @@ module Spreeaddress_create_service: 'Spree::Addresses::Create',address_update_service: 'Spree::Addresses::Update',+ # What checkout must collect before a purchase is placed, and before+ # it may be dispatched. The whole total here; replaced where part+ # payment is arranged (deposits, net terms).+ purchase_amount_due_at_checkout_service: 'Spree::Purchases::AmountDueAtCheckout',+payment_create_service: 'Spree::Payments::Create',payment_process_workflow: 'Spree::Payments::Process',payment_capture_workflow: 'Spree::Payments::Capture',diff --git a/spree/core/spec/services/spree/purchases/amount_due_at_checkout_spec.rb b/spree/core/spec/services/spree/purchases/amount_due_at_checkout_spec.rbnew file mode 100644index 0000000000..a2750a7e5c--- /dev/null+++ b/spree/core/spec/services/spree/purchases/amount_due_at_checkout_spec.rb@@ -0,0 +1,95 @@+require 'spec_helper'++RSpec.describe Spree::Purchases::AmountDueAtCheckout do+ let(:store) { @default_store }++ describe 'the open-source answer' do+ it 'is the whole total, so a buyer pays for what they are taking' do+ order = create(:order, store: store)+ create(:line_item, order: order, price: 100, quantity: 2)+ Spree::Orders::RecalculateTotals.call(order: order.reload)++ expect(order.reload.amount_due_at_checkout).to eq(order.total)+ end++ it 'answers for a cart the same way it does for an order' do+ cart = create(:cart, store: store)+ create(:line_item, cart: cart, order: nil, price: 100, quantity: 1)+ Spree::Carts::RecalculateTotals.call(cart: cart.reload)++ expect(cart.reload.amount_due_at_checkout).to eq(cart.total)+ end+ end++ # The point of the class: an arrangement that collects part of the total up+ # front is honoured by checkout and dispatch without either being patched.+ describe 'a replacement that collects part of the total' do+ let(:half_up_front) do+ Class.new do+ def call(purchase:)+ purchase.total.to_d / 2+ end+ end+ end++ around do |example|+ original = Spree::Dependencies.purchase_amount_due_at_checkout_service+ Spree::Dependencies.purchase_amount_due_at_checkout_service = half_up_front+ example.run+ ensure+ Spree::Dependencies.purchase_amount_due_at_checkout_service = original+ Spree::Dependencies.instance_variable_get(:@overrides)&.delete(:purchase_amount_due_at_checkout_service)+ end++ let(:cart) do+ create(:cart, store: store, email: '[email protected]').tap do |cart|+ address = create(:address)+ cart.update!(ship_address: address, bill_address: address)+ create(:line_item, cart: cart, order: nil, price: 100, quantity: 1)+ create(:shipment, cart: cart, order: nil)+ Spree::Carts::RecalculateTotals.call(cart: cart.reload)+ end+ end++ it 'lets checkout stop asking once that part has been paid' do+ create(:payment, cart: cart, order: nil, amount: cart.reload.amount_due_at_checkout, status: 'completed')++ requirements = Spree::Checkout::DefaultRequirements.new(cart.reload).call++ expect(cart.amount_due_at_checkout).to be < cart.total+ expect(requirements).not_to include(a_hash_including(step: 'payment', field: 'payment'))+ end++ it 'completes the order owing the rest' do+ create(:payment, cart: cart, order: nil, amount: cart.reload.amount_due_at_checkout, status: 'completed')++ result = Spree::Carts::Complete.call(cart: cart.reload)++ expect(result).to be_success+ expect(result.value).to be_completed+ expect(result.value.outstanding_balance).to be > 0+ end++ it 'dispatches an order that has paid only that part' do+ create(:payment, cart: cart, order: nil, amount: cart.reload.amount_due_at_checkout, status: 'completed')+ order = Spree::Carts::Complete.call(cart: cart.reload).value+ fulfillment = order.fulfillments.first+ fulfillment.inventory_units.each do |item|+ fulfillment.stock_location.stock_level_or_create(item.variant).update_column(:count_on_hand, 10)+ end++ result = Spree::Fulfillments::Fulfill.call(fulfillment: fulfillment)++ expect(result).to be_success+ expect(order.reload.outstanding_balance).to be > 0+ end++ # The guard still has to refuse an underpaid order — the replacement moves+ # the line, it does not remove it.+ it 'still refuses a cart that has paid less than its arrangement asks' do+ create(:payment, cart: cart, order: nil, amount: 1, status: 'completed')++ expect(Spree::Carts::Complete.call(cart: cart.reload)).to be_failure+ end+ end+end