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-10-15 22:08:52 +01:00
|
|
|
protect_from_forgery prepend: true
|
|
|
|
protect_from_forgery with: :exception
|
|
|
|
|
2017-10-11 23:48:41 +01:00
|
|
|
skip_before_action :verify_authenticity_token, only: [:create]
|
2017-05-17 14:17:37 +10:00
|
|
|
|
2017-10-11 23:48:41 +01:00
|
|
|
def create
|
2017-10-19 23:50:21 +01:00
|
|
|
Rails.logger.debug params.inspect
|
|
|
|
Rails.logger.debug user_params.inspect
|
|
|
|
|
2017-05-04 12:39:45 +10:00
|
|
|
output = { 'messages' => [], 'rewards' => [] }
|
|
|
|
|
2017-05-04 12:43:33 +10:00
|
|
|
if create_account
|
2017-10-11 23:48:41 +01:00
|
|
|
if !email.present? || !user_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-10-11 23:48:41 +01:00
|
|
|
if user_params[:password] && user_params[:password].length > User.max_password_length
|
2017-05-05 09:57:26 +10:00
|
|
|
output['messages'] << I18n.t('login.password_too_long')
|
|
|
|
end
|
2017-10-11 23:48:41 +01:00
|
|
|
if user_params[:username] && ::User.reserved_username?(user_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)
|
2017-05-24 12:22:00 +10:00
|
|
|
|
|
|
|
begin
|
2017-10-11 23:48:41 +01:00
|
|
|
charge = payment.charge(email, opts: user_params)
|
2017-05-24 12:22:00 +10:00
|
|
|
rescue ::Stripe::CardError => e
|
|
|
|
err = e.json_body[:error]
|
|
|
|
|
|
|
|
output['messages'] << "There was an error (#{err[:type]})."
|
2017-10-16 12:58:50 +01:00
|
|
|
output['messages'] << "Error code: #{err[:code]}" if err[:code]
|
|
|
|
output['messages'] << "Decline code: #{err[:decline_code]}" if err[:decline_code]
|
2017-05-24 12:22:00 +10:00
|
|
|
output['messages'] << "Message: #{err[:message]}" if err[:message]
|
|
|
|
|
|
|
|
render(:json => output) and return
|
|
|
|
end
|
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-10-11 23:48:41 +01:00
|
|
|
args = user_params.to_h.slice(:email, :username, :password, :name).merge(rewards: output['rewards'])
|
2017-05-10 11:17:39 +10:00
|
|
|
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-10-11 23:48:41 +01:00
|
|
|
user_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-10-11 23:48:41 +01:00
|
|
|
def user_params
|
|
|
|
params.permit(:name, :username, :email, :password, :stripeToken, :amount, :create_account)
|
|
|
|
end
|
|
|
|
|
2017-04-04 12:00:23 +10:00
|
|
|
def email
|
2017-10-11 23:48:41 +01:00
|
|
|
user_params[:email] || current_user.try(:email)
|
2017-02-16 16:29:42 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|