From ce258d3d082cb031c3173ab2d882858638ac7a6b Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Thu, 12 Sep 2019 11:49:52 +1000 Subject: [PATCH] convert amount in param --- app/controllers/patrons_controller.rb | 8 +++++++- .../discourse_patrons/patrons_controller_spec.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/controllers/patrons_controller.rb b/app/controllers/patrons_controller.rb index e00f9d0..07a5216 100644 --- a/app/controllers/patrons_controller.rb +++ b/app/controllers/patrons_controller.rb @@ -15,7 +15,7 @@ module DiscoursePatrons begin response = ::Stripe::PaymentIntent.create( - amount: params[:amount], + amount: param_currency_to_number, currency: SiteSetting.discourse_patrons_currency, payment_method_types: ['card'], payment_method: params[:paymentMethodId], @@ -30,5 +30,11 @@ module DiscoursePatrons render json: response end + + private + + def param_currency_to_number + params[:amount].to_s.sub('.', '').to_i + end end end diff --git a/spec/controllers/discourse_patrons/patrons_controller_spec.rb b/spec/controllers/discourse_patrons/patrons_controller_spec.rb index e753de9..a249e2a 100644 --- a/spec/controllers/discourse_patrons/patrons_controller_spec.rb +++ b/spec/controllers/discourse_patrons/patrons_controller_spec.rb @@ -24,6 +24,18 @@ module DiscoursePatrons post :create, params: {}, format: :json expect(response).to have_http_status(200) end + + it 'has the correct amount' do + ::Stripe::PaymentIntent.expects(:create).with(has_entry(:amount, 2000)) + post :create, params: { amount: '20.00' }, format: :json + expect(response).to have_http_status(200) + end + + it 'has no amount' do + ::Stripe::PaymentIntent.expects(:create).with(has_entry(:amount, 0)) + post :create, params: {}, format: :json + expect(response).to have_http_status(200) + end end end end