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-04 15:05:50 +10:00
|
|
|
if create_account && (email.nil? || email.empty?)
|
2017-04-04 13:10:31 +10:00
|
|
|
response = {'message' => 'Please enter your email address'}
|
2017-04-04 13:44:13 +10:00
|
|
|
elsif create_account && params[:username].nil?
|
|
|
|
response = {'message' => 'Please enter a username'}
|
2017-04-04 11:18:35 +10:00
|
|
|
else
|
2017-04-06 14:22:22 +10:00
|
|
|
payment = DiscourseDonations::Stripe.new(secret_key, stripe_options)
|
|
|
|
response = payment.charge(email, params)
|
2017-04-04 12:00:23 +10:00
|
|
|
end
|
|
|
|
|
2017-04-24 10:06:47 +10:00
|
|
|
response['rewards'] = []
|
2017-04-21 12:08:52 +10:00
|
|
|
|
2017-04-27 18:28:38 +10:00
|
|
|
if reward?(payment)
|
|
|
|
if current_user.present?
|
|
|
|
reward = DiscourseDonations::Rewards.new(current_user)
|
|
|
|
if reward.add_to_group(group_name)
|
|
|
|
response['rewards'] << { type: :group, name: group_name }
|
|
|
|
end
|
|
|
|
if reward.grant_badge(badge_name)
|
|
|
|
response['rewards'] << { type: :badge, name: badge_name }
|
|
|
|
end
|
|
|
|
elsif email.present?
|
|
|
|
if group_name.present?
|
|
|
|
store = PluginStore.get('discourse-donations', 'group:add') || []
|
|
|
|
PluginStore.set('discourse-donations', 'group:add', store << email)
|
|
|
|
end
|
2017-04-27 19:31:11 +10:00
|
|
|
if badge_name.present?
|
|
|
|
store = PluginStore.get('discourse-donations', 'badge:grant') || []
|
|
|
|
PluginStore.set('discourse-donations', 'badge:grant', store << email)
|
|
|
|
end
|
2017-04-27 13:34:48 +10:00
|
|
|
end
|
2017-04-21 10:26:43 +10:00
|
|
|
end
|
|
|
|
|
2017-04-04 12:00:23 +10:00
|
|
|
render :json => response
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-04-27 18:28:38 +10:00
|
|
|
def create_account
|
|
|
|
params[:create_account] == 'true'
|
|
|
|
end
|
|
|
|
|
|
|
|
def reward?(payment)
|
|
|
|
payment.present? && payment.successful?
|
2017-04-21 10:26:43 +10:00
|
|
|
end
|
|
|
|
|
2017-04-24 11:44:55 +10:00
|
|
|
def group_name
|
|
|
|
SiteSetting.discourse_donations_reward_group_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def badge_name
|
|
|
|
SiteSetting.discourse_donations_reward_badge_name
|
|
|
|
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
|