discourse-subscriptions/app/controllers/discourse_donations/charges_controller.rb

116 lines
3.3 KiB
Ruby
Raw Normal View History

2017-02-21 18:21:42 -05:00
require_dependency 'discourse'
2017-02-23 21:23:11 -05:00
module DiscourseDonations
class ChargesController < ApplicationController
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
def create
Rails.logger.info user_params.inspect
2017-05-03 22:39:45 -04:00
output = { 'messages' => [], 'rewards' => [] }
2017-05-03 22:43:33 -04:00
if create_account
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
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
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
end
2017-05-03 22:43:33 -04:00
if output['messages'].present?
render(json: output.merge(success: false)) && (return)
2017-05-03 22:39:45 -04:00
end
2017-04-20 22:08:52 -04: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
Rails.logger.debug "Creating a Stripe charge for #{user_params[:amount]}"
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]})."
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]
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
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?
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
end
render json: output
end
private
def create_account
user_params[:create_account] == 'true' && SiteSetting.discourse_donations_enable_create_accounts
end
def reward?(payment)
payment.present? && payment.successful?
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
def user_params
params.permit(:user_id, :name, :username, :email, :password, :stripeToken, :amount, :create_account)
end
def email
user_params[:email] || user.try(:email)
end
def user
if user_params[:user_id]
User.find(user_params[:user_id])
else
current_user
end
end
end
end