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.

Refuse to settle a share the gateway has not finished with

Damian Legawiecdc9fff911065
diff --git a/packages/dashboard/src/locales/pl.json b/packages/dashboard/src/locales/pl.json
index 7d516b578c..92c33c5bf3 100644
--- a/packages/dashboard/src/locales/pl.json
+++ b/packages/dashboard/src/locales/pl.json
@@ -2310,7 +2310,7 @@
"voided": "Płatność unieważniona"
},
"no_customer": "Brak informacji o kliencie",
- "order_group_help": "To zamówienie zostało złożone razem z innymi w jednym zamówieniu.",
+ "order_group_help": "To zamówienie zostało złożone razem z innymi w ramach jednego zakupu.",
"order_group_part_of": "Część większego zakupu",
"order_group_view_all": "Zobacz wszystkie zamówienia z tego zakupu",
"payment_form": {
diff --git a/spree/core/app/models/spree/refund.rb b/spree/core/app/models/spree/refund.rb
index 23ba540754..d7acffa930 100644
--- a/spree/core/app/models/spree/refund.rb
+++ b/spree/core/app/models/spree/refund.rb
@@ -109,7 +109,15 @@ module Spree
# shared payment the share that should have recorded the refund is never
# found.
def order_is_covered_by_payment
- return if payment.blank? || order_id.blank?
+ return if payment.blank?
+
+ # A shared payment cannot say which of its orders is being put right, so
+ # the caller has to — and nothing can fill it in afterwards. Left blank,
+ # the gateway would be credited while no child's totals or share moved.
+ if order_id.blank?
+ errors.add(:order, :blank) if payment.grouped?
+ return
+ end
covered = if payment.grouped?
payment.payment_splits.exists?(order_id: order_id)
diff --git a/spree/core/app/workflows/spree/orders/cancel.rb b/spree/core/app/workflows/spree/orders/cancel.rb
index 273a418e95..5deb9e3841 100644
--- a/spree/core/app/workflows/spree/orders/cancel.rb
+++ b/spree/core/app/workflows/spree/orders/cancel.rb
@@ -170,7 +170,15 @@ module Spree
remaining = @amount_to_refund
order.payment_splits.includes(:payment).each do |split|
- # Release what this order had reserved but never drew, always — the
+ # A parcel dispatching marks its share taken *before* asking the
+ # gateway, so a share can read captured while the charge is still in
+ # flight. Settling against that would release an authorization the
+ # gateway is about to draw on, or hand back money it never took —
+ # so a payment mid-capture is left for the operator rather than
+ # guessed at.
+ next if capture_in_flight?(split)
+
+ # Release what this order had reserved but never drew — the
# authorization is no longer for anything. Under the row's lock, so
# a dispatch claiming the same share concurrently either draws before
# the release or finds nothing left.
@@ -192,6 +200,34 @@ module Spree
end
end
+ # Whether this share claims money the gateway has not confirmed.
+ #
+ # The claim is written before the charge so two parcels cannot draw the
+ # same share, which leaves a window where the split says captured and the
+ # payment does not. Reported rather than settled silently: cancelling
+ # through it would either release an authorization about to be drawn or
+ # refund money nobody took.
+ #
+ # @return [Boolean]
+ def capture_in_flight?(split)
+ payment = split.payment
+ return false if payment.nil? || split.captured_amount <= 0
+ # A payment settled at checkout carries no capture events and its
+ # shares are captured by definition; only one still being drawn on can
+ # be caught mid-charge.
+ return false if payment.completed?
+
+ return false if split.captured_amount <= payment.captured_amount.to_d
+
+ Rails.error.report(
+ Spree::Core::GatewayError.new('Payment share is mid-capture; cancellation left it for manual settlement'),
+ handled: true,
+ context: { order_id: order.id, payment_split_id: split.id },
+ source: 'spree.core'
+ )
+ true
+ end
+
# Reverses the filed tax document. The canceled sale must stop appearing
# in the merchant's tax liability.
def void_tax
diff --git a/spree/core/spec/workflows/spree/orders/cancel_grouped_spec.rb b/spree/core/spec/workflows/spree/orders/cancel_grouped_spec.rb
index 6d30bcc487..c2174f1bb3 100644
--- a/spree/core/spec/workflows/spree/orders/cancel_grouped_spec.rb
+++ b/spree/core/spec/workflows/spree/orders/cancel_grouped_spec.rb
@@ -46,6 +46,34 @@ RSpec.describe Spree::Orders::Cancel, 'an order placed in a split checkout' do
end
end
+ # A parcel marks its share taken before asking the gateway, so a share can
+ # read captured while the charge is still in flight. Settling through that
+ # would release an authorization about to be drawn, or refund money nobody
+ # took.
+ context 'while a capture is still in flight' do
+ let(:in_flight_payment) do
+ create(:payment, order: nil, cart: nil, order_group: group, amount: 40, status: 'processing')
+ end
+ let!(:claimed_split) do
+ create(:payment_split, payment: in_flight_payment, order: seller_order,
+ authorized_amount: 40, captured_amount: 40)
+ end
+
+ before { seller_split.destroy! }
+
+ it 'leaves the claimed share alone rather than releasing it' do
+ described_class.call(order: seller_order)
+
+ expect(claimed_split.reload.authorized_amount).to eq(40)
+ end
+
+ it 'refunds nothing the gateway has not confirmed' do
+ expect {
+ described_class.call(order: seller_order, refund_payments: true)
+ }.not_to change { Spree::Refund.count }
+ end
+ end
+
context 'with refund_payments' do
it 'gives back what this order was actually paid' do
described_class.call(order: seller_order, refund_payments: true)
diff --git a/spree/core/spec/workflows/spree/refunds/create_grouped_spec.rb b/spree/core/spec/workflows/spree/refunds/create_grouped_spec.rb
index 458ff8adc1..2e74fe494e 100644
--- a/spree/core/spec/workflows/spree/refunds/create_grouped_spec.rb
+++ b/spree/core/spec/workflows/spree/refunds/create_grouped_spec.rb
@@ -47,6 +47,16 @@ RSpec.describe Spree::Refunds::Create, 'against a shared payment' do
expect(result).to be_failure
end
+ # Nothing can fill this in afterwards: a shared payment cannot say which of
+ # its orders is being put right, so a refund without one would credit the
+ # gateway while no child's totals or share moved.
+ it 'refuses a refund that names no order' do
+ result = described_class.call(payment: payment, amount: 10)
+
+ expect(result).to be_failure
+ expect(payment.reload.refunds).to be_empty
+ end
+
it 'stamps the refund with the order it put right' do
refund = described_class.call(payment: payment, amount: 10, order: seller_order).value