* Add ProMembership model * Implement ProMembershipsController#create * Implement basic ProMembershipsController#show * Add ProMembership to ledger * Populate user history after pro subscription is created * Add fields for expiration notifications * Add ProMemberships::ExpirationNotifier to notify users of expiring memberships * Add tasks for recurring jobs to notify users of expiration * Add auto_recharge column to ProMembership * Add ProMemberships::Biller (incomplete) * Fix specs * Add ProMembership to Administrate * Fix spec * Add has_enough_credits? to User and Organization * Add Payments::Customer class * Finish ProMembership::Biller functionality * Fix ProMemberships::Creator check for credits * Disable destroy actions for ProMembershipsController * Correctly authenticate ProMembershipsController actions * Make sure only pro user's history can be indexed * Add ProMembershipsController#update action for auto recharge * Use regular AR to save new credits and add touch to the purchaser * Clarify Pro membership create policy * Display information about an existing pro membership * Add UI to show page * Add system test for Pro membership creation * Implement edit membership * Make sure users with pro memberships can access history and dashboard pro * Fix padding issue * Show a different text for a user that has credits but not enough for Pro * Move Pro Membership functionality inside settings * Update Pro Membership link in email notifications * Bust all relevant caches * Add the Pro checkmark around the website * Use Users::ResaveArticlesJob instead of delay * Add/remove user from pro-members chat channel * Use the appropriate Pro checkmark * Remove unfinished pro elements * Remove checkmark JS
104 lines
3 KiB
Ruby
104 lines
3 KiB
Ruby
class CreditsController < ApplicationController
|
|
before_action :authenticate_user!
|
|
|
|
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
|
|
|
|
return unless make_payment
|
|
|
|
credit_objects = Array.new(@number_to_purchase) do
|
|
if params[:organization_id].present?
|
|
@purchaser = Organization.find(params[:organization_id])
|
|
Credit.new(organization_id: params[:organization_id], cost: cost_per_credit / 100.0)
|
|
else
|
|
@purchaser = current_user
|
|
Credit.new(user_id: current_user.id, cost: cost_per_credit / 100.0)
|
|
end
|
|
end
|
|
Credit.import credit_objects
|
|
@purchaser.credits_count = @purchaser.credits.size
|
|
@purchaser.spent_credits_count = @purchaser.credits.spent.size
|
|
@purchaser.unspent_credits_count = @purchaser.credits.unspent.size
|
|
@purchaser.save
|
|
redirect_to credits_path, notice: "#{@number_to_purchase} new credits purchased!"
|
|
end
|
|
|
|
private
|
|
|
|
def make_payment
|
|
find_or_create_customer
|
|
find_or_create_card
|
|
update_user_stripe_info
|
|
create_charge
|
|
true
|
|
rescue Payments::PaymentsError => e
|
|
flash[:error] = e.message
|
|
redirect_to purchase_credits_path
|
|
false
|
|
end
|
|
|
|
def find_or_create_customer
|
|
@customer = if current_user.stripe_id_code
|
|
Payments::Customer.get(current_user.stripe_id_code)
|
|
else
|
|
Payments::Customer.create(email: current_user.email)
|
|
end
|
|
end
|
|
|
|
def find_or_create_card
|
|
@card = if params[:stripe_token]
|
|
Payments::Customer.create_source(@customer.id, params[:stripe_token])
|
|
else
|
|
Payments::Customer.get_source(@customer, params[:selected_card])
|
|
end
|
|
end
|
|
|
|
def update_user_stripe_info
|
|
current_user.update_column(:stripe_id_code, @customer.id) if current_user.stripe_id_code.nil?
|
|
end
|
|
|
|
def create_charge
|
|
Payments::Customer.charge(
|
|
customer: @customer,
|
|
amount: generate_cost,
|
|
description: "Purchase of #{@number_to_purchase} credits.",
|
|
card_id: @card&.id,
|
|
)
|
|
end
|
|
|
|
def generate_cost
|
|
@number_to_purchase * cost_per_credit
|
|
end
|
|
|
|
def cost_per_credit
|
|
if @number_to_purchase < 10
|
|
500
|
|
elsif @number_to_purchase < 100
|
|
400
|
|
elsif @number_to_purchase < 1000
|
|
300
|
|
else
|
|
250
|
|
end
|
|
end
|
|
end
|