Skip to content

Instantly share code, notes, and snippets.

@joelbriggs
Created February 2, 2021 21:53
Show Gist options
  • Save joelbriggs/5fd40b9f1eb4c3a333ae2c16d1e997cc to your computer and use it in GitHub Desktop.
Save joelbriggs/5fd40b9f1eb4c3a333ae2c16d1e997cc to your computer and use it in GitHub Desktop.
Part of a Stripe Subscription solution
module Manager
# Controller for managing account
class AccountController < ManagerController
include Manager::AccountHelper
skip_before_action :redirect_deactivated_account_to_reactivation
before_action :admin_required
def admin_required
redirect_to home_path if current_user.role == 'user'
end
def index
redirect_to manager_account_billing_path
end
protected
def prepare_cards
@credit_cards = @company.stripe_cards.sort_by { |c| [c.exp_year, c.exp_month] }
end
def setup_subscription_status
@subscribed = false
if !@company.active?
status = :inactive
elsif @company.trial_expired?
status = :expired
elsif @company.trial?
status = :trial
elsif @company.stripe_plan.present?
@subscription_plan = SubscriptionPlan.find @district.stripe_plan
@subscribed = true
status = :subscribed
else
# This is an edge case that is not expected to be encountered.
# This will cause the message 'You are not currently subscribed' when passed
# to `subscription_status_message`
status = :unknown
end
@subscription_status = subscription_status_message(status)
end
def subscription_status_message(status)
case status
when :inactive
'Your account has been deactivated.'
when :trial_expired
format 'Your free trial expired on %s',
@company.trial_expiration_date.to_formatted_s(:mdy)
when :trial
format 'Finish setting up your account. You will need to do this before %s',
@company.trial_expiration_date.to_formatted_s(:mdy)
when :subscribed
format 'You are subscribed to the "%s" plan.',
@subscription_plan.name
else
'You are not subscribed.'
end
end
def process_subscription
@subscription_plan = SubscriptionPlan.find(params[:plan])
@result = @company.apply_subscription(params[:plan])
validate_subscription_is_success
@message = "You have been subscribed to #{@subscription_plan.name} plan"
setup_subscription_plan_status
end
def validate_subscription_success
raise 'There was a problem subscribing you' if @company.stripe_subscription_id.blank?
end
end
end
module Manager
module Account
# Controller for managing credit cards connected to an account
class StripeCardsController < Manager::AccountController
include Manager::Account::CardsHelper
# skip_before_action :redirect_expired_trial_to_billing
before_action :set_subscribing, only: :create
before_action :set_card, only: %i[set_default destroy]
def card_from_token(token)
@comapny.stripe_cards.find(token)
end
# GET /stripe_cards/new
def new
@page_title = 'Add a Credit Card'
@credit_card = {}
end
# POST /stripe_cards
# POST /stripe_cards.json
def create
create_credit_card(safe_card_parameters.to_h)
prepare_cards
process_subscription if @subscribing
respond_to do |format|
format.html do
notice = format 'Your new credit card ending in %s has been added to your account. ', @result.last4
redirect_to manager_account_billing_path, notice: notice
end
format.js { render }
end
end
def set_default
if @credit_card.present?
customer = @company.stripe_customer
customer.default_source = @token
customer.save
end
prepare_cards
render
end
# DELETE /stripe_cards/1
# DELETE /stripe_cards/1.json
def destroy
@company.stripe_customer.sources.retrieve(@token).delete if @credit_card.present?
prepare_cards
respond_to do |format|
format.html { redirect_to manager_account_billing_path }
format.js { render }
end
end
protected
def set_subscribing
@subscribing = params[:plan].present?
end
def set_card
@token = params[:id]
@credit_card = card_from_token @token
end
def safe_card_parameters
params.require('credit_card').permit(:expiration, :number, :name, :default)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment