* Allow user to have many orgs * Allow users to handle multi orgs in settings * Make rounded buttons inline * Add multi org function to dashboards * Fix merge conflicts * Fix mistake in merge conflict fix oops * Display the correct membership level * Fix accessibility issues * Display organizations for article editors * Handle submitting org id with preact editors * Make listings work with multiple organizations * Allow listings to have multiple orgs on create * Display the correct number of credits for each org * Move script tag to Webpack * Allow multi orgs for purchasing and viewing credits * Use OrganizationMembership as authorization check * Display multiple organizations for notifications * Allow dashboard to be viewable under multi-orgs * Remove unused method * Add multi-org functionality for article editors * Show pro dashboard buttons for member+ org levels * Leave the correct organization * Allow article API to change org id * Add left-out authorization method oops * Make nav buttons a bit more clear * Fix merge conflict * Fix adding org id for /api/articles and tests * Fix tests for org policy * Use proper logic for displaying org members * Update org actions with new authorization * Use correct org when creating a listing * Remove additional payment charge oops * Mark org notifications as read with authorization * Remove deprecated post_as_organization attribute * Use new org_admin syntax * Remove deprecated org logic for article create and update * Default all RSS posts to not belong to any org * Render org_member page for guest users * Update org policy spec to work with multi orgs * Use org_membership for org traits and move identity code * Use org_member trait * Update to work with multi-orgs * Validate article's org_id if param org_id is blank * Make a let variable * Remove unnecessary eager load for credits * Fix HTML structure and org logic for non-org users * Update credits spec for multi-org * Add test for failed payment when purchased by org * Lint listings_spec * Test that the listing was created under the user * Add tests for POST /listings multi-org * Use double quotes for classes * Fix /manage and a few other multi-org bugs * Fix test for multi org * Use correct method SQL exists? not Rails exist? * Fix reads spec for multi-org * Fix org_controller actions to work with multi org * Test only multi org and not old usage and fix leave_org * Fix org showing user profile img test for multi-org * Fix org logic for users with no orgs * Remove switch org functionality * Update tests and add hidden param for org id * Redirect to the specific organization * Test other org button actions * Use settings_notice instead of legacy notice and refactor * Fix weird extra end issue prob from merge conflicts * Test for with new flash key * Fix user_views_org tests for multi-org * Test for new flash message * Update snapshot with new a11y html * Move styling to stylesheet * Add site admins functionality * Move org_member? method in user model and refactor * Use unspent_credits_count for organizations * Add tests for /listings/new and minor bug fixes * Use .present? in case of empty array * Fix a lingering deprecated method * Use greater than 1 for random numbers * Add tests for counting spent and unspent credits
102 lines
2.8 KiB
Ruby
102 lines
2.8 KiB
Ruby
class CreditsController < ApplicationController
|
|
before_action :authenticate_user!
|
|
|
|
def index
|
|
@credits = current_user.credits.where(spent: false)
|
|
@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 = Stripe::Customer.retrieve(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?
|
|
Credit.new(organization_id: params[:organization_id], cost: cost_per_credit / 100.0)
|
|
else
|
|
Credit.new(user_id: current_user.id, cost: cost_per_credit / 100.0)
|
|
end
|
|
end
|
|
Credit.import credit_objects
|
|
redirect_to "/credits", notice: "#{@number_to_purchase} new credits purchased!"
|
|
end
|
|
|
|
def make_payment
|
|
find_or_create_customer
|
|
find_or_create_card
|
|
update_user_stripe_info
|
|
create_charge
|
|
true
|
|
rescue Stripe::CardError => e
|
|
flash[:error] = e.message
|
|
redirect_to "/credits/purchase"
|
|
false
|
|
end
|
|
|
|
def find_or_create_customer
|
|
@customer = if current_user.stripe_id_code
|
|
Stripe::Customer.retrieve(current_user.stripe_id_code)
|
|
else
|
|
Stripe::Customer.create(
|
|
email: current_user.email,
|
|
)
|
|
end
|
|
end
|
|
|
|
def find_or_create_card
|
|
@card = if params[:stripe_token]
|
|
Stripe::Customer.create_source(
|
|
@customer.id,
|
|
source: params[:stripe_token],
|
|
)
|
|
else
|
|
@customer.sources.retrieve(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
|
|
@amount = generate_cost
|
|
source = Rails.env.test? ? @card.id : (@card || @customer.default_source)
|
|
Stripe::Charge.create(
|
|
customer: @customer.id,
|
|
source: source,
|
|
amount: @amount,
|
|
description: "Purchase of #{@number_to_purchase} credits.",
|
|
currency: "usd",
|
|
)
|
|
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
|