From 213e879b864f03f630c971f3326fe1a8de082f21 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 20 Apr 2022 09:51:27 +0200 Subject: [PATCH] Fix creating refund with amount in foreign format 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. --- core/app/models/spree/refund.rb | 8 ++++++++ core/spec/models/spree/refund_spec.rb | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/core/app/models/spree/refund.rb b/core/app/models/spree/refund.rb index 78356a7ec75..00158132562 100644 --- a/core/app/models/spree/refund.rb +++ b/core/app/models/spree/refund.rb @@ -31,6 +31,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 diff --git a/core/spec/models/spree/refund_spec.rb b/core/spec/models/spree/refund_spec.rb index d1867761286..6c421a00877 100644 --- a/core/spec/models/spree/refund_spec.rb +++ b/core/spec/models/spree/refund_spec.rb @@ -39,6 +39,14 @@ it "does not attempt to process a transaction" do expect(subject.transaction_id).to be_nil 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