Class: Billingly::RedemptionsControllerBase

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/billingly/redemptions_controller_base.rb

Overview

People who have a SpecialPlanCode can redeem it through the RedemptionsController

Direct Known Subclasses

RedemptionsController

Instance Method Summary collapse

Instance Method Details

#createObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/billingly/redemptions_controller_base.rb', line 7

def create
  code = Billingly::SpecialPlanCode.find_redeemable(params[:promo_code])
  if code.nil?
    flash[:invalid_promo_code] = t('billingly.promo_codes.invalid')
    redirect_to new_redemption_path
    return
  end

  unless current_customer
    session[:current_promo_code] = params[:promo_code] 
    on_redemption_by_non_customer
    return
  end

  unless current_customer.can_subscribe_to?(code.plan)
    flash[:invalid_promo_code_plan] = t('billingly.promo_codes.cannot_subscribe_to_plan')
    redirect_to new_redemption_path
    return
  end

  current_customer.redeem_special_plan_code(code)
  on_redemption
end

#newObject



4
5
# File 'app/controllers/billingly/redemptions_controller_base.rb', line 4

def new
end

#on_redemptionObject

When a code is redeemed successfully by the current customer this callback is called. The default behavior is redirecting to the subscriptions index page.



33
34
35
# File 'app/controllers/billingly/redemptions_controller_base.rb', line 33

def on_redemption
  redirect_to subscriptions_path, notice: t('billingly.promo_codes.redeemed_successfully')
end

#on_redemption_by_non_customerObject

The redemptions page is public, therefore there may be people redeeming valid codes without being a customer.

In such cases, the validated code is stored in the session and this callback is called. You should redirect to your signup page here, and subscribe the customer using the saved code after they sign up. The code and plan associated to this code can be retrieved using #current_promo_code and #current_promo_code_plan



45
46
# File 'app/controllers/billingly/redemptions_controller_base.rb', line 45

def on_redemption_by_non_customer
end