Skip to content

Commit

Permalink
Merge pull request #1355 from Sinetheta/cancel-refunded-orders
Browse files Browse the repository at this point in the history
Allow cancelling orders that have been fully refunded
  • Loading branch information
jhawthorn authored Oct 4, 2017
2 parents 64c2d9b + 9928a9e commit 9818f15
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ def ensure_available_shipping_rates

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

send_cancel_email
Expand Down
5 changes: 5 additions & 0 deletions core/app/models/spree/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ def can_credit?
credit_allowed > 0
end

# @return [Boolean] true when this payment has been fully refunded
def fully_refunded?
refunds.map(&:amount).sum == amount
end

# @return [Array<String>] the actions available on this payment
def actions
sa = source_actions
Expand Down
17 changes: 16 additions & 1 deletion core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
end

describe "#cancel!" do
subject { order.cancel! }

context "with captured store credit" do
let!(:store_credit_payment_method) { create(:store_credit_payment_method) }
let(:order_total) { 500.00 }
Expand All @@ -55,7 +57,20 @@
order.capture_payments!
end

subject { order.cancel! }
it "cancels the order" do
expect{ subject }.to change{ order.can_cancel? }.from(true).to(false)
expect(order).to be_canceled
end
end

context "with fully refunded payment" do
let(:order) { create(:completed_order_with_totals) }
let(:payment_amount) { 50 }
let(:payment) { create(:payment, order: order, amount: payment_amount, state: 'completed') }

before do
create(:refund, payment: payment, amount: payment_amount)
end

it "cancels the order" do
expect{ subject }.to change{ order.can_cancel? }.from(true).to(false)
Expand Down
19 changes: 19 additions & 0 deletions core/spec/models/spree/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,25 @@
end
end

describe "#fully_refunded?" do
subject { payment.fully_refunded? }

before { payment.amount = 100 }

context 'before refund' do
it { is_expected.to be false }
end

context 'when refund total equals payment amount' do
before do
create(:refund, payment: payment, amount: 50)
create(:refund, payment: payment, amount: 50)
end

it { is_expected.to be true }
end
end

describe "#save" do
context "captured payments" do
it "update order payment total" do
Expand Down

0 comments on commit 9818f15

Please sign in to comment.