Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow cancelling orders that have been fully refunded #1355

Merged
merged 1 commit into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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