Skip to content

Commit

Permalink
Fix creating refund with amount in foreign format
Browse files Browse the repository at this point in the history
The numericality validation in rails cannot handle prices
in non-float based amounts (Ie. 100,00 EUR).

Using `Spree::LocalizedNumber` to convert the amount before validation.
  • Loading branch information
tvdeyen committed Apr 20, 2022
1 parent 41a412c commit f61e6f0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/app/models/spree/refund.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ def total_amount_reimbursed_for(reimbursement)
end
end

# Sets this price's amount to a new value, parsing it if the new value is
# a string.
#
# @param price [String, #to_d] a new amount
def amount=(price)
self[:amount] = Spree::LocalizedNumber.parse(price)
end

def description
payment.payment_method.name
end
Expand Down
8 changes: 8 additions & 0 deletions core/spec/models/spree/refund_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@
end
end
end

context "with a european price format" do
let(:amount) { "100,00" }

it "creates a refund record" do
expect { subject }.to change { Spree::Refund.count }.by(1)
end
end
end

describe "#perform!" do
Expand Down

0 comments on commit f61e6f0

Please sign in to comment.