2017-02-21 18:21:42 -05:00
|
|
|
require_dependency 'discourse'
|
|
|
|
|
2017-02-23 21:23:11 -05:00
|
|
|
module DiscourseDonations
|
2017-11-07 19:45:18 -05:00
|
|
|
class ChargesController < ApplicationController
|
2017-10-15 17:08:52 -04:00
|
|
|
|
2017-10-11 18:48:41 -04:00
|
|
|
skip_before_action :verify_authenticity_token, only: [:create]
|
2017-11-11 22:37:21 -05:00
|
|
|
skip_before_action :check_xhr
|
2017-05-17 00:17:37 -04:00
|
|
|
|
2017-10-11 18:48:41 -04:00
|
|
|
def create
|
2017-11-26 07:24:27 -05:00
|
|
|
Rails.logger.info user_params.inspect
|
2017-10-19 18:50:21 -04:00
|
|
|
|
2017-05-03 22:39:45 -04:00
|
|
|
output = { 'messages' => [], 'rewards' => [] }
|
|
|
|
|
2017-05-03 22:43:33 -04:00
|
|
|
if create_account
|
2017-10-11 18:48:41 -04:00
|
|
|
if !email.present? || !user_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-10-11 18:48:41 -04:00
|
|
|
if user_params[:password] && user_params[:password].length > User.max_password_length
|
2017-05-04 19:57:26 -04:00
|
|
|
output['messages'] << I18n.t('login.password_too_long')
|
|
|
|
end
|
2017-10-11 18:48:41 -04:00
|
|
|
if user_params[:username] && ::User.reserved_username?(user_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?
|
2018-02-02 03:06:43 -05:00
|
|
|
render(json: output.merge(success: false)) && (return)
|
2017-05-03 22:39:45 -04:00
|
|
|
end
|
2017-04-20 22:08:52 -04:00
|
|
|
|
2017-11-26 07:24:27 -05:00
|
|
|
Rails.logger.debug "Creating a Stripe payment"
|
2017-05-03 22:43:33 -04:00
|
|
|
payment = DiscourseDonations::Stripe.new(secret_key, stripe_options)
|
2017-05-23 22:22:00 -04:00
|
|
|
|
|
|
|
begin
|
2017-11-26 07:24:27 -05:00
|
|
|
Rails.logger.debug "Creating a Stripe charge for #{user_params[:amount]}"
|
2018-02-02 03:06:43 -05:00
|
|
|
charge_params = [user_params[:stripeToken], user_params[:amount]]
|
|
|
|
|
|
|
|
if user
|
|
|
|
charge_params.unshift(user, user.email)
|
|
|
|
else
|
|
|
|
charge_params.unshift(nil, email)
|
|
|
|
end
|
|
|
|
|
|
|
|
charge = payment.charge(*charge_params)
|
2017-05-23 22:22:00 -04:00
|
|
|
rescue ::Stripe::CardError => e
|
|
|
|
err = e.json_body[:error]
|
|
|
|
|
|
|
|
output['messages'] << "There was an error (#{err[:type]})."
|
2017-10-16 07:58:50 -04:00
|
|
|
output['messages'] << "Error code: #{err[:code]}" if err[:code]
|
|
|
|
output['messages'] << "Decline code: #{err[:decline_code]}" if err[:decline_code]
|
2017-05-23 22:22:00 -04:00
|
|
|
output['messages'] << "Message: #{err[:message]}" if err[:message]
|
|
|
|
|
2018-02-02 03:06:43 -05:00
|
|
|
render(json: output) && (return)
|
2017-05-23 22:22:00 -04:00
|
|
|
end
|
2017-05-06 02:31:06 -04:00
|
|
|
|
|
|
|
if charge['paid'] == true
|
2018-01-17 19:21:41 -05:00
|
|
|
output['messages'] << I18n.l(Time.now(), format: :long) + ': ' + I18n.t('donations.payment.success')
|
2017-05-03 22:43:33 -04:00
|
|
|
|
2017-05-09 20:05:53 -04: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-10-11 18:48:41 -04:00
|
|
|
args = user_params.to_h.slice(:email, :username, :password, :name).merge(rewards: output['rewards'])
|
2017-05-09 21:17:39 -04:00
|
|
|
Jobs.enqueue(:donation_user, args)
|
2017-04-26 23:34:48 -04:00
|
|
|
end
|
2017-04-20 20:26:43 -04:00
|
|
|
end
|
|
|
|
|
2018-02-02 03:06:43 -05: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
|
2017-10-11 18:48:41 -04:00
|
|
|
user_params[:create_account] == 'true' && SiteSetting.discourse_donations_enable_create_accounts
|
2017-04-27 04:28:38 -04:00
|
|
|
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-10-11 18:48:41 -04:00
|
|
|
def user_params
|
2018-02-02 03:06:43 -05:00
|
|
|
params.permit(:user_id, :name, :username, :email, :password, :stripeToken, :amount, :create_account)
|
2017-10-11 18:48:41 -04:00
|
|
|
end
|
|
|
|
|
2017-04-03 22:00:23 -04:00
|
|
|
def email
|
2018-02-02 03:06:43 -05:00
|
|
|
user_params[:email] || user.try(:email)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user
|
|
|
|
if user_params[:user_id]
|
|
|
|
User.find(user_params[:user_id])
|
|
|
|
else
|
|
|
|
current_user
|
|
|
|
end
|
2017-02-16 00:29:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|