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]
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

View File

@ -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

View File

@ -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