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

Deprecate Spree::Payment offsets #5008

Merged
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 @@ -572,7 +572,7 @@ def quantity

def has_non_reimbursement_related_refunds?
refunds.non_reimbursement.exists? ||
payments.offset_payment.exists? # how old versions of spree stored refunds
payments.where("source_type = 'Spree::Payment' AND amount < 0 AND state = 'completed'").exists? # how old versions of spree stored refunds
end

def tax_total
Expand Down
18 changes: 16 additions & 2 deletions core/app/models/spree/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,18 @@ class Payment < Spree::Base
scope :from_credit_card, -> { where(source_type: 'Spree::CreditCard') }
scope :with_state, ->(state) { where(state: state.to_s) }
# "offset" is reserved by activerecord
scope :offset_payment, -> { where("source_type = 'Spree::Payment' AND amount < 0 AND state = 'completed'") }
# TODO: When removing the method we can also:
# - Remove the `.offsets` association
# - Remove the `#offsets_total` method
# - Remove offsets count from the `#credit_allowed` method
# - Remove offsets check from `Spree::Order#has_non_reimbursement_related_refunds?
def self.offset_payment
Spree::Deprecation.warn <<~MSG
`Spree::Payment offsets` are deprecated. Use the refund system (`Spree::Refund`) instead.
MSG

where("source_type = 'Spree::Payment' AND amount < 0 AND state = 'completed'")
end

scope :checkout, -> { with_state('checkout') }
scope :completed, -> { with_state('completed') }
Expand Down Expand Up @@ -101,7 +112,10 @@ def offsets_total
# @return [BigDecimal] the amount of this payment minus the offsets
# (old-style refunds) and refunds
def credit_allowed
amount - (offsets_total.abs + refunds.sum(:amount))
amount - (
(self.class.where("source_type = 'Spree::Payment' AND amount < 0 AND state = 'completed' AND source_id = ?", id).sum(:amount)).abs +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, maybe a variable makes sense? We could add a comment to easily recall which part of the method we need to remove when offset_payment will be gone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that I needed to repeat the inline on Spree::Order#has_non_reimbursement_related_refunds?, and that the code should really be very short-lived (we should remove it in #4989), I'd prefer to keep it simple if you think it's ok 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same suggestion as alberto, but also fine with this give its expected short life

refunds.sum(:amount)
)
end

# @return [Boolean] true when this payment can be credited
Expand Down
25 changes: 23 additions & 2 deletions core/spec/models/spree/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -671,11 +671,18 @@
# Regression test for https://github.com/spree/spree/issues/4403 and https://github.com/spree/spree/issues/4407
it "is the difference between offsets total and payment amount" do
payment.amount = 100
allow(payment).to receive(:offsets_total).and_return(0)
expect(payment.credit_allowed).to eq(100)
allow(payment).to receive(:offsets_total).and_return(-80)
create(:payment, amount: -80, source: payment, source_type: 'Spree::Payment', state: 'completed', payment_method: create(:check_payment_method))
expect(payment.credit_allowed).to eq(20)
end

it "is the difference between refunds total and payment amount" do
payment.amount = 100

expect {
create(:refund, payment: payment, amount: 80)
}.to change { payment.credit_allowed }.from(100).to(20)
end
end

describe "#can_credit?" do
Expand Down Expand Up @@ -1332,4 +1339,18 @@
expect(described_class.valid).to be_empty
end
end

describe '::offset_payment' do
it 'is deprecated' do
expect(Spree::Deprecation).to receive(:warn).with(/offsets` are deprecated/)

described_class.offset_payment
end

it 'still calls the original method' do
Spree::Deprecation.silence do
expect(described_class.offset_payment.class.name).to eq('ActiveRecord::Relation')
end
end
end
end