From 392478adf3e4b9e9502399cc771005f4f783ef2d Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Thu, 6 Apr 2017 14:22:22 +1000 Subject: [PATCH] implement service --- .../discourse_donations/charges_controller.rb | 27 +++++++++---------- .../charges_controller_spec.rb | 2 ++ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/app/controllers/discourse_donations/charges_controller.rb b/app/controllers/discourse_donations/charges_controller.rb index f558cb4..562c871 100644 --- a/app/controllers/discourse_donations/charges_controller.rb +++ b/app/controllers/discourse_donations/charges_controller.rb @@ -10,20 +10,8 @@ module DiscourseDonations if email.nil? || email.empty? response = {} else - Stripe.api_key = SiteSetting.discourse_donations_secret_key - currency = SiteSetting.discourse_donations_currency - - customer = Stripe::Customer.create( - :email => email, - :source => params[:stripeToken] - ) - - response = Stripe::Charge.create( - :customer => customer.id, - :amount => params[:amount], - :description => SiteSetting.discourse_donations_description, - :currency => currency - ) + payment = DiscourseDonations::Stripe.new(secret_key, stripe_options) + response = payment.charge(email, params) end render :json => response @@ -31,6 +19,17 @@ module DiscourseDonations private + def secret_key + SiteSetting.discourse_donations_secret_key + end + + def stripe_options + { + description: SiteSetting.discourse_donations_description, + currency: SiteSetting.discourse_donations_currency + } + end + def email params[:email] || current_user.try(:email) end diff --git a/spec/controllers/discourse_donations/charges_controller_spec.rb b/spec/controllers/discourse_donations/charges_controller_spec.rb index 9d01873..35398ff 100644 --- a/spec/controllers/discourse_donations/charges_controller_spec.rb +++ b/spec/controllers/discourse_donations/charges_controller_spec.rb @@ -7,6 +7,8 @@ module DiscourseDonations before do SiteSetting.stubs(:discourse_donations_secret_key).returns('secret-key-yo') + SiteSetting.stubs(:discourse_donations_description).returns('charity begins at discourse plugin') + SiteSetting.stubs(:discourse_donations_currency).returns('AUD') end it 'responds ok for anonymous users' do