2017-02-22 10:21:42 +11:00
|
|
|
require_dependency 'discourse'
|
|
|
|
|
2017-02-24 13:23:11 +11:00
|
|
|
module DiscourseDonations
|
2017-02-16 16:29:42 +11:00
|
|
|
class ChargesController < ActionController::Base
|
2017-02-22 10:21:42 +11:00
|
|
|
include CurrentUser
|
|
|
|
|
2017-02-16 16:29:42 +11:00
|
|
|
skip_before_filter :verify_authenticity_token, only: [:create]
|
|
|
|
|
|
|
|
def create
|
2017-04-20 11:09:59 +10:00
|
|
|
if email.present?
|
2017-04-06 14:22:22 +10:00
|
|
|
payment = DiscourseDonations::Stripe.new(secret_key, stripe_options)
|
|
|
|
response = payment.charge(email, params)
|
2017-04-20 11:09:59 +10:00
|
|
|
else
|
|
|
|
response = {}
|
2017-04-04 12:00:23 +10:00
|
|
|
end
|
|
|
|
|
2017-04-21 12:08:52 +10:00
|
|
|
response['rewards'] = []
|
|
|
|
|
2017-04-21 10:26:43 +10:00
|
|
|
if reward_user?(payment)
|
|
|
|
reward = DiscourseDonations::Rewards.new(current_user)
|
2017-04-21 12:08:52 +10:00
|
|
|
group_name = SiteSetting.discourse_donations_reward_group
|
|
|
|
reward.add_to_group(group_name) if group_name.present?
|
2017-04-21 10:26:43 +10:00
|
|
|
end
|
|
|
|
|
2017-04-04 12:00:23 +10:00
|
|
|
render :json => response
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-04-21 10:26:43 +10:00
|
|
|
def reward_user?(payment)
|
|
|
|
payment.present? && payment.successful? && current_user.present?
|
|
|
|
end
|
|
|
|
|
2017-04-06 14:22:22 +10:00
|
|
|
def secret_key
|
|
|
|
SiteSetting.discourse_donations_secret_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def stripe_options
|
|
|
|
{
|
|
|
|
description: SiteSetting.discourse_donations_description,
|
|
|
|
currency: SiteSetting.discourse_donations_currency
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-04-04 12:00:23 +10:00
|
|
|
def email
|
|
|
|
params[:email] || current_user.try(:email)
|
2017-02-16 16:29:42 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|