docbrown/app/controllers/credits_controller.rb
yheuhtozr c4778d832e
app/controllers & decorators i18n (#16126)
* app/controllers (& decorators) i18n etc

* tidy key names

* update keys

* delete ja.yml

* delete an involved ja.yml

* fix for PR review

* fix for spec

* delete ja.yml
2022-02-03 13:35:56 -05:00

42 lines
1.4 KiB
Ruby

class CreditsController < ApplicationController
before_action :authenticate_user!
before_action :initialize_stripe
def index
@user_unspent_credits_count = current_user.credits.unspent.size
@ledger = Credits::Ledger.call(current_user)
@organizations = current_user.admin_organizations
end
def new
@credit = Credit.new
@purchaser = if params[:organization_id].present? && current_user.org_admin?(params[:organization_id])
Organization.find_by(id: params[:organization_id])
else
current_user
end
@organizations = current_user.admin_organizations
@customer = Payments::Customer.get(current_user.stripe_id_code) if current_user.stripe_id_code
end
def create
not_authorized if params[:organization_id].present? && !current_user.org_admin?(params[:organization_id])
number_to_purchase = params[:credit][:number_to_purchase].to_i
payment = Payments::ProcessCreditPurchase.call(
current_user,
number_to_purchase,
purchase_options: params.slice(:stripe_token, :selected_card, :organization_id),
)
if payment.success?
@purchaser = payment.purchaser
redirect_to credits_path, notice: I18n.t("credits_controller.done", count: number_to_purchase)
else
flash[:error] = payment.error
redirect_to purchase_credits_path
end
end
end