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.

Stop trying to void money the gateway has already taken

Damian Legawiec6273f34ff3cf
diff --git a/spree/core/app/workflows/spree/orders/cancel.rb b/spree/core/app/workflows/spree/orders/cancel.rb
index 6f219a9faf..2912c99f10 100644
--- a/spree/core/app/workflows/spree/orders/cancel.rb
+++ b/spree/core/app/workflows/spree/orders/cancel.rb
@@ -155,23 +155,19 @@ module Spree
end
end
- # A cancellation always releases the gateway's hold; whether it hands
- # money back is the caller's call.
+ # Whether captured money goes back is the caller's call. `cancel!` is
+ # the gateway's own settle-this-payment verb, and on a completed payment
+ # that means a refund, so it is reached only when one was asked for.
#
- # `cancel!` is the gateway's own settle-this-payment verb, and on a
- # captured payment that means a full refund — so it is reached only when
- # a refund was actually asked for. Otherwise the authorization is voided,
- # which releases an uncaptured hold and leaves captured money where it
- # is, for an operator to refund deliberately.
+ # Otherwise nothing is called at all: a completed payment is money
+ # already drawn, which a gateway will not void — Stripe rejects
+ # cancelling a captured PaymentIntent — so there is no hold to release
+ # here and an operator refunds it deliberately. Authorizations never
+ # drawn on are `incomplete`, and the caller above voids those.
def settle_completed_payments
- order.payments.completed.each do |payment|
- if refund_payments || payment.credit_allowed <= 0
- payment.cancel!
- elsif payment.can_void?
- result = Spree.payment_void_workflow.call(payment: payment)
- raise Spree::Core::GatewayError, result.error.value.to_s if result.failure?
- end
- end
+ return unless refund_payments
+
+ order.payments.completed.each(&:cancel!)
end
# What this order has actually been paid. An order placed in a split
diff --git a/spree/core/spec/workflows/spree/orders/cancel_spec.rb b/spree/core/spec/workflows/spree/orders/cancel_spec.rb
index 0218ea971e..abeaed476b 100644
--- a/spree/core/spec/workflows/spree/orders/cancel_spec.rb
+++ b/spree/core/spec/workflows/spree/orders/cancel_spec.rb
@@ -134,15 +134,15 @@ module Spree
let(:order) { create(:completed_order_with_totals) }
let!(:payment) { create(:payment, order: order, amount: order.total, status: 'completed') }
- it 'releases the hold and leaves captured money alone by default' do
+ # Not voided either: the gateway refuses to cancel money it has already
+ # drawn, so touching it would fail the cancellation after the order was
+ # already marked canceled.
+ it 'leaves captured money alone by default, without calling the gateway' do
expect_any_instance_of(Spree::Payment).not_to receive(:cancel!)
- allow(Spree.payment_void_workflow).to receive(:call).
- and_return(Spree::ServiceModule::Result.new(true, nil, nil))
+ expect(Spree.payment_void_workflow).not_to receive(:call).
+ with(hash_including(payment: payment))
subject.call(order: order, canceler: user)
-
- expect(Spree.payment_void_workflow).to have_received(:call).
- with(hash_including(payment: payment))
end
it 'hands it back when asked' do