2017-02-21 18:21:42 -05:00
|
|
|
require_dependency 'discourse'
|
|
|
|
|
2017-02-23 21:23:11 -05:00
|
|
|
module DiscourseDonations
|
2017-02-16 00:29:42 -05:00
|
|
|
class ChargesController < ActionController::Base
|
2017-02-21 18:21:42 -05:00
|
|
|
include CurrentUser
|
|
|
|
|
2017-02-16 00:29:42 -05:00
|
|
|
skip_before_filter :verify_authenticity_token, only: [:create]
|
|
|
|
|
|
|
|
def create
|
2017-05-03 22:39:45 -04:00
|
|
|
output = { 'messages' => [], 'rewards' => [] }
|
|
|
|
|
2017-05-03 22:43:33 -04:00
|
|
|
if create_account
|
2017-05-04 19:57:26 -04:00
|
|
|
if !email.present? || !params[:username].present?
|
2017-05-04 06:11:26 -04:00
|
|
|
output['messages'] << I18n.t('login.missing_user_field')
|
2017-05-03 22:43:33 -04:00
|
|
|
end
|
2017-05-04 19:57:26 -04:00
|
|
|
if params[:password] && params[:password].length > User.max_password_length
|
|
|
|
output['messages'] << I18n.t('login.password_too_long')
|
|
|
|
end
|
|
|
|
if params[:username] && ::User.reserved_username?(params[:username])
|
2017-05-04 05:49:30 -04:00
|
|
|
output['messages'] << I18n.t('login.reserved_username')
|
2017-05-03 22:43:33 -04:00
|
|
|
end
|
2017-04-03 22:00:23 -04:00
|
|
|
end
|
|
|
|
|
2017-05-03 22:43:33 -04:00
|
|
|
if output['messages'].present?
|
2017-05-04 20:24:22 -04:00
|
|
|
render(:json => output.merge(success: false)) and return
|
2017-05-03 22:39:45 -04:00
|
|
|
end
|
2017-04-20 22:08:52 -04:00
|
|
|
|
2017-05-03 22:43:33 -04:00
|
|
|
payment = DiscourseDonations::Stripe.new(secret_key, stripe_options)
|
|
|
|
charge = payment.charge(email, params)
|
2017-05-06 02:31:06 -04:00
|
|
|
|
|
|
|
if charge['paid'] == true
|
|
|
|
output['messages'] << I18n.t('donations.payment.success')
|
|
|
|
end
|
2017-05-03 22:43:33 -04:00
|
|
|
|
2017-04-27 04:28:38 -04:00
|
|
|
if reward?(payment)
|
|
|
|
if current_user.present?
|
|
|
|
reward = DiscourseDonations::Rewards.new(current_user)
|
|
|
|
if reward.add_to_group(group_name)
|
2017-05-03 22:39:45 -04:00
|
|
|
output['rewards'] << { type: :group, name: group_name }
|
2017-04-27 04:28:38 -04:00
|
|
|
end
|
|
|
|
if reward.grant_badge(badge_name)
|
2017-05-03 22:39:45 -04:00
|
|
|
output['rewards'] << { type: :badge, name: badge_name }
|
2017-04-27 04:28:38 -04:00
|
|
|
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 05:31:11 -04:00
|
|
|
if badge_name.present?
|
|
|
|
store = PluginStore.get('discourse-donations', 'badge:grant') || []
|
|
|
|
PluginStore.set('discourse-donations', 'badge:grant', store << email)
|
|
|
|
end
|
2017-04-26 23:34:48 -04:00
|
|
|
end
|
2017-04-20 20:26:43 -04:00
|
|
|
end
|
|
|
|
|
2017-05-03 22:39:45 -04:00
|
|
|
render :json => output
|
2017-04-03 22:00:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-04-27 04:28:38 -04:00
|
|
|
def create_account
|
|
|
|
params[:create_account] == 'true'
|
|
|
|
end
|
|
|
|
|
|
|
|
def reward?(payment)
|
|
|
|
payment.present? && payment.successful?
|
2017-04-20 20:26:43 -04:00
|
|
|
end
|
|
|
|
|
2017-04-23 21:44:55 -04:00
|
|
|
def group_name
|
|
|
|
SiteSetting.discourse_donations_reward_group_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def badge_name
|
|
|
|
SiteSetting.discourse_donations_reward_badge_name
|
|
|
|
end
|
|
|
|
|
2017-04-06 00:22:22 -04: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-03 22:00:23 -04:00
|
|
|
def email
|
|
|
|
params[:email] || current_user.try(:email)
|
2017-02-16 00:29:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|