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-05-04 12:39:45 +10:00
|
|
|
output = { 'messages' => [], 'rewards' => [] }
|
|
|
|
|
2017-05-04 12:43:33 +10:00
|
|
|
if create_account
|
2017-05-05 09:57:26 +10:00
|
|
|
if !email.present? || !params[:username].present?
|
2017-05-04 20:11:26 +10:00
|
|
|
output['messages'] << I18n.t('login.missing_user_field')
|
2017-05-04 12:43:33 +10:00
|
|
|
end
|
2017-05-05 09:57:26 +10: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 19:49:30 +10:00
|
|
|
output['messages'] << I18n.t('login.reserved_username')
|
2017-05-04 12:43:33 +10:00
|
|
|
end
|
2017-04-04 12:00:23 +10:00
|
|
|
end
|
|
|
|
|
2017-05-04 12:43:33 +10:00
|
|
|
if output['messages'].present?
|
2017-05-05 10:24:22 +10:00
|
|
|
render(:json => output.merge(success: false)) and return
|
2017-05-04 12:39:45 +10:00
|
|
|
end
|
2017-04-21 12:08:52 +10:00
|
|
|
|
2017-05-04 12:43:33 +10:00
|
|
|
payment = DiscourseDonations::Stripe.new(secret_key, stripe_options)
|
|
|
|
charge = payment.charge(email, params)
|
2017-05-06 16:31:06 +10:00
|
|
|
|
|
|
|
if charge['paid'] == true
|
|
|
|
output['messages'] << I18n.t('donations.payment.success')
|
2017-05-04 12:43:33 +10:00
|
|
|
|
2017-05-10 10:05:53 +10:00
|
|
|
output['rewards'] << { type: :group, name: group_name } if group_name
|
|
|
|
output['rewards'] << { type: :badge, name: badge_name } if badge_name
|
|
|
|
|
|
|
|
if create_account && email.present?
|
2017-05-10 11:17:39 +10:00
|
|
|
args = params.slice(:email, :username, :password, :name).merge(rewards: output['rewards'])
|
|
|
|
Jobs.enqueue(:donation_user, args)
|
2017-04-27 13:34:48 +10:00
|
|
|
end
|
2017-04-21 10:26:43 +10:00
|
|
|
end
|
|
|
|
|
2017-05-04 12:39:45 +10:00
|
|
|
render :json => output
|
2017-04-04 12:00:23 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-04-27 18:28:38 +10:00
|
|
|
def create_account
|
2017-05-10 10:05:53 +10:00
|
|
|
params[:create_account] == 'true' && SiteSetting.discourse_donations_enable_create_accounts
|
2017-04-27 18:28:38 +10:00
|
|
|
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
|