respond with empty json if email does not exist

This commit is contained in:
Rimian Perkins 2017-04-04 12:00:23 +10:00
parent 2d8878b0b7
commit 5d3db5373b
3 changed files with 38 additions and 16 deletions

View File

@ -7,22 +7,34 @@ module DiscourseDonations
skip_before_filter :verify_authenticity_token, only: [:create] skip_before_filter :verify_authenticity_token, only: [:create]
def create def create
if email.nil?
response = {
}
else
Stripe.api_key = SiteSetting.discourse_donations_secret_key Stripe.api_key = SiteSetting.discourse_donations_secret_key
currency = SiteSetting.discourse_donations_currency currency = SiteSetting.discourse_donations_currency
customer = Stripe::Customer.create( customer = Stripe::Customer.create(
:email => params[:email] || current_user.email, :email => email,
:source => params[:stripeToken] :source => params[:stripeToken]
) )
charge = Stripe::Charge.create( response = Stripe::Charge.create(
:customer => customer.id, :customer => customer.id,
:amount => params[:amount], :amount => params[:amount],
:description => SiteSetting.discourse_donations_description, :description => SiteSetting.discourse_donations_description,
:currency => currency :currency => currency
) )
end
render :json => charge render :json => response
end
private
def email
params[:email] || current_user.try(:email)
end end
end end
end end

View File

@ -1,6 +1,6 @@
# name: discourse-donations # name: discourse-donations
# about: Integrating Discourse with Stripe for donations # about: Integrating Discourse with Stripe for donations
# version: 1.7.1 # version: 1.7.2
# url: https://github.com/choiceaustralia/discourse-donations # url: https://github.com/choiceaustralia/discourse-donations
# authors: Rimian Perkins # authors: Rimian Perkins

View File

@ -7,10 +7,20 @@ module DiscourseDonations
before do before do
SiteSetting.stubs(:discourse_donations_secret_key).returns('secret-key-yo') SiteSetting.stubs(:discourse_donations_secret_key).returns('secret-key-yo')
current_user = log_in(:coding_horror)
end 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 post :create
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end