Skip to content

Commit

Permalink
Cancel all authorized (pending) payments when cancelling an order
Browse files Browse the repository at this point in the history
E.g., release funds on a credit card auth for cancelled orders whose
payments have been authorized but not yet captured.

This was already happening for store credit payments.
  • Loading branch information
jordan-brough authored and filippoliverani committed Jun 12, 2020
1 parent 0f758dc commit 15c8193
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
18 changes: 15 additions & 3 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -914,9 +914,8 @@ def ensure_available_shipping_rates
end

def after_cancel
shipments.each(&:cancel!)
payments.completed.each { |payment| payment.cancel! unless payment.fully_refunded? }
payments.store_credits.pending.each(&:void_transaction!)
cancel_shipments!
cancel_payments!

send_cancel_email
# rubocop:disable Rails/SkipsModelValidations
Expand All @@ -925,6 +924,19 @@ def after_cancel
recalculate
end

def cancel_shipments!
shipments.each(&:cancel!)
end

def cancel_payments!
payments.each do |payment|
next if payment.fully_refunded?
next unless payment.pending? || payment.completed?

payment.cancel!
end
end

def send_cancel_email
Spree::Config.order_mailer_class.cancel_email(self).deliver_later
end
Expand Down
13 changes: 10 additions & 3 deletions core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true
# jj

require 'rails_helper'

Expand Down Expand Up @@ -131,6 +130,15 @@
end
end

context 'when the payment is pending' do
let(:order) { create(:completed_order_with_pending_payment) }
let(:payment) { order.payments.first }

it 'voids the pending payment' do
expect { subject }.to change { payment.reload.state }.from('pending').to('void')
end
end

context 'with a store credit payment' do
let(:order) { create(:completed_order_with_totals) }
let(:payment) { create(:store_credit_payment, amount: order.total, order: order) }
Expand All @@ -157,7 +165,7 @@
end

it 'refunds the payment' do
expect { subject }.to change { Spree::Refund.count }.by(1)
expect { subject }.to change { Spree::Refund.count }.by(1)
end
end
end
Expand Down Expand Up @@ -1630,7 +1638,6 @@ def generate
expect(subject.display_store_credit_remaining_after_capture.money.cents).to eq(amount_remaining * 100.0)
end
end

end

context 'update_params_payment_source' do
Expand Down

0 comments on commit 15c8193

Please sign in to comment.