diff --git a/app/controllers/discourse_donations/charges_controller.rb b/app/controllers/discourse_donations/charges_controller.rb index c45122c..308747b 100644 --- a/app/controllers/discourse_donations/charges_controller.rb +++ b/app/controllers/discourse_donations/charges_controller.rb @@ -7,22 +7,34 @@ module DiscourseDonations skip_before_filter :verify_authenticity_token, only: [:create] def create - Stripe.api_key = SiteSetting.discourse_donations_secret_key - currency = SiteSetting.discourse_donations_currency + if email.nil? + response = { - customer = Stripe::Customer.create( - :email => params[:email] || current_user.email, - :source => params[:stripeToken] - ) + } + else + Stripe.api_key = SiteSetting.discourse_donations_secret_key + currency = SiteSetting.discourse_donations_currency - charge = Stripe::Charge.create( - :customer => customer.id, - :amount => params[:amount], - :description => SiteSetting.discourse_donations_description, - :currency => currency - ) + customer = Stripe::Customer.create( + :email => email, + :source => params[:stripeToken] + ) - render :json => charge + response = Stripe::Charge.create( + :customer => customer.id, + :amount => params[:amount], + :description => SiteSetting.discourse_donations_description, + :currency => currency + ) + end + + render :json => response + end + + private + + def email + params[:email] || current_user.try(:email) end end end diff --git a/plugin.rb b/plugin.rb index 78a8033..2745f9e 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,6 +1,6 @@ # name: discourse-donations # about: Integrating Discourse with Stripe for donations -# version: 1.7.1 +# version: 1.7.2 # url: https://github.com/choiceaustralia/discourse-donations # authors: Rimian Perkins diff --git a/spec/controllers/discourse_donations/charges_controller_spec.rb b/spec/controllers/discourse_donations/charges_controller_spec.rb index 09aab13..13b51d4 100644 --- a/spec/controllers/discourse_donations/charges_controller_spec.rb +++ b/spec/controllers/discourse_donations/charges_controller_spec.rb @@ -7,10 +7,20 @@ module DiscourseDonations before do SiteSetting.stubs(:discourse_donations_secret_key).returns('secret-key-yo') - current_user = log_in(:coding_horror) end - it 'responds with ok' do + it 'responds ok for anonymous users' do + post :create, { email: 'foobar@example.com' } + expect(response).to have_http_status(200) + end + + it 'responds ok when the email is empty' do + post :create, { } + expect(response).to have_http_status(200) + end + + it 'responds ok for logged in user' do + current_user = log_in(:coding_horror) post :create expect(response).to have_http_status(200) end