Skip to content
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
13 changes: 13 additions & 0 deletions core/app/models/spree/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Payment < Spree::Base
validates :amount, numericality: true
validates :source, presence: true, if: :source_required?
validates :payment_method, presence: true
validate :amount_not_less_than_refunds, if: :will_save_change_to_amount?

default_scope -> { order(:created_at) }

Expand Down Expand Up @@ -169,6 +170,18 @@ def validate_source
end
end

# A payment can never be worth less than what has already been refunded
# from it, otherwise #credit_allowed and the order's payment total go
# negative.
def amount_not_less_than_refunds
return if amount.nil?

refunded = refunds.sum(:amount)
if amount < refunded
errors.add(:amount, :less_than_refunded, refunded:)
end
end

def source_required?
!!payment_method&.source_required?
end
Expand Down
5 changes: 5 additions & 0 deletions core/app/models/spree/payment/processing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ def capture!(capture_amount = nil)
return false unless amount.positive?

capture_amount ||= money.money.cents
# Refuse before talking to the gateway: capturing less than what was
# already refunded would leave the payment worth less than its refunds.
if ::Money.new(capture_amount, currency).to_d < refunds.sum(:amount)
gateway_error(I18n.t("spree.capture_amount_less_than_refunded"))
end
started_processing!
protect_from_connection_error do
# Standard ActiveMerchant capture usage
Expand Down
5 changes: 5 additions & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,10 @@ en:
price:
does_not_match_order_currency: Line item price currency must match order currency!
not_a_number: is not valid
spree/payment:
attributes:
amount:
less_than_refunded: cannot be less than the amount already refunded (%{refunded}).
spree/price:
attributes:
currency:
Expand Down Expand Up @@ -1140,6 +1144,7 @@ en:
cannot_set_shipping_method_without_address: Cannot set shipping method until customer details are provided.
cannot_update_email: You do not have access to update this user's email address. <br>Please contact a superuser if you need to perform this action.
capture: Capture
capture_amount_less_than_refunded: Cannot capture less than the amount already refunded.
capture_events: Capture events
card_code: Card Code
card_number: Card Number
Expand Down
58 changes: 58 additions & 0 deletions core/spec/models/spree/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,31 @@
payment.capture!(6000)
expect(payment.reload.amount).to eq(60)
end

context "when the payment has refunds" do
before { create(:refund, payment:, amount: 80) }

context "and the amount to capture is less than the refunds" do
it "raises a gateway error without capturing" do
expect(payment.payment_method).not_to receive(:capture)

expect { payment.capture!(6000) }.to raise_error(
Spree::Core::GatewayError,
"Cannot capture less than the amount already refunded."
)
expect(payment.capture_events.count).to eq(0)
expect(payment).to be_pending
end
end

context "and the amount to capture covers the refunds" do
it "captures the payment" do
payment.capture!(8000)
expect(payment.reload.amount).to eq(80)
expect(payment).to be_completed
end
end
end
end

context "if unsuccessful" do
Expand Down Expand Up @@ -685,6 +710,39 @@
end
end

describe "amount validation" do
before do
payment.update!(state: "completed", amount: 100)
create(:refund, payment:, amount: 60)
end

it "is invalid when the amount is less than the refunds total" do
payment.amount = 50
expect(payment).not_to be_valid
expect(payment.errors[:amount]).to include("cannot be less than the amount already refunded (60.0).")
end

it "is valid when the amount equals the refunds total" do
payment.amount = 60
expect(payment).to be_valid
end

context "when an existing payment already has more refunded than its amount" do
before { payment.update_columns(amount: 50) }

it "can still be saved when the amount is not changing" do
payment.response_code = "abc"
expect(payment).to be_valid
expect { payment.save! }.not_to raise_error
end

it "cannot have its amount lowered further" do
payment.amount = 40
expect(payment).not_to be_valid
end
end
end

describe "#can_credit?" do
it "is true if credit_allowed > 0" do
allow(payment).to receive(:credit_allowed).and_return(100)
Expand Down
Loading