Class: Kaui::SubscriptionsController
Constant Summary
EngineControllerUtil::SIMPLE_PAGINATION_THRESHOLD
Instance Method Summary
collapse
#check_for_redirect_to_tenant_screen, #current_ability, #current_user, #options_for_klient, #populate_account_details, #retrieve_allowed_users_for_current_user, #retrieve_tenants_for_current_user
Instance Method Details
#create ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'app/controllers/kaui/subscriptions_controller.rb', line 24
def create
plan_name = params.require(:plan_name)
@base_product_name = params[:base_product_name]
@subscription = Kaui::Subscription.new(params.require(:subscription).delete_if { |_key, value| value.blank? })
begin
@bundle, plans_details = lookup_bundle_and_plan_details(@subscription, @base_product_name)
plan_details = plans_details.find { |p| p.plan == plan_name }
raise "Unable to find plan #{plan_name}" if plan_details.nil?
@subscription.plan_name = plan_name
requested_date = params[:type_change] == 'DATE' ? params[:requested_date].presence : nil
override_fixed_price = begin
plan_details.phases.first.prices.blank?
rescue StandardError
false
end
override_recurring_price = !override_fixed_price
phase_type = @bundle.nil? ? plan_details.phases.first.type : @bundle.subscriptions.first.phase_type
overrides = price_overrides(phase_type, override_fixed_price, override_recurring_price)
@subscription.price_overrides = overrides unless overrides.blank?
@subscription.product_category = nil
@subscription = @subscription.create(current_user.kb_username, params[:reason], params[:comment], requested_date, false, options_for_klient)
redirect_to kaui_engine.account_bundles_path(@subscription.account_id), notice: 'Subscription was successfully created'
rescue StandardError => e
@plans = plans_details.nil? ? [] : plans_details.map(&:plan)
if e.is_a?(::KillBillClient::API::BadRequest) && !e.response.nil? && !e.response.body.nil?
error_message = begin
JSON.parse(e.response.body)
rescue StandardError
nil
end
if (!error_message.nil? & !error_message['code'].nil?) && error_message['code'] == 2010 flash.now[:error] = "Unable to create the subscription: a price for this currency hasn't been specified in the catalog"
render :new and return
end
end
flash.now[:error] = "Error while creating the subscription: #{as_string(e)}"
render :new
end
end
|
#destroy ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'app/controllers/kaui/subscriptions_controller.rb', line 115
def destroy
requested_date = params[:requested_date].presence
billing_policy = params[:policy].presence
entitlement_policy = billing_policy && billing_policy == 'START_OF_TERM' ? 'IMMEDIATE' : billing_policy
use_requested_date_for_billing = if requested_date
(params[:use_requested_date_for_billing] || '1') == '1'
else
nil
end
subscription = Kaui::Subscription.find_by_id(params.require(:id), options_for_klient)
subscription.cancel(current_user.kb_username, params[:reason], params[:comment], requested_date, entitlement_policy, billing_policy, use_requested_date_for_billing, options_for_klient)
redirect_to kaui_engine.account_bundles_path(subscription.account_id), notice: 'Subscription was successfully cancelled'
end
|
#edit ⇒ Object
74
75
76
77
78
79
|
# File 'app/controllers/kaui/subscriptions_controller.rb', line 74
def edit
@subscription = Kaui::Subscription.find_by_id(params.require(:id), options_for_klient)
_, plans_details = lookup_bundle_and_plan_details(@subscription)
@plans = Set.new.merge(plans_details.map(&:plan))
end
|
#edit_bcd ⇒ Object
140
141
142
|
# File 'app/controllers/kaui/subscriptions_controller.rb', line 140
def edit_bcd
@subscription = Kaui::Subscription.find_by_id(params.require(:id), options_for_klient)
end
|
#new ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'app/controllers/kaui/subscriptions_controller.rb', line 5
def new
@base_product_name = params[:base_product_name]
@subscription = Kaui::Subscription.new(bundle_id: params[:bundle_id],
account_id: params[:account_id],
product_category: params[:product_category] || 'BASE')
@bundle, plans_details = lookup_bundle_and_plan_details(@subscription, @base_product_name)
@plans = plans_details.map(&:plan)
return unless @plans.empty?
flash[:error] = if @subscription.product_category == 'BASE'
'No plan available in the catalog'
else
"No add-on available in the catalog for product #{@base_product_name}"
end
redirect_to kaui_engine.account_bundles_path(@subscription.account_id), error: 'No available plan'
end
|
#reinstate ⇒ Object
132
133
134
135
136
137
138
|
# File 'app/controllers/kaui/subscriptions_controller.rb', line 132
def reinstate
subscription = Kaui::Subscription.find_by_id(params.require(:id), options_for_klient)
subscription.uncancel(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
redirect_to kaui_engine.account_bundles_path(subscription.account_id), notice: 'Subscription was successfully reinstated'
end
|
#restful_show ⇒ Object
160
161
162
163
|
# File 'app/controllers/kaui/subscriptions_controller.rb', line 160
def restful_show
subscription = Kaui::Subscription.find_by_id(params.require(:id), options_for_klient)
redirect_to kaui_engine.account_bundles_path(subscription.account_id)
end
|
#show ⇒ Object
156
157
158
|
# File 'app/controllers/kaui/subscriptions_controller.rb', line 156
def show
restful_show
end
|
#update ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'app/controllers/kaui/subscriptions_controller.rb', line 81
def update
plan_name = params.require(:plan_name)
requested_date = params[:type_change] == 'DATE' ? params[:requested_date].presence : nil
billing_policy = params[:type_change] == 'POLICY' ? params[:policy].presence : nil
wait_for_completion = params[:wait_for_completion] == '1'
subscription = Kaui::Subscription.find_by_id(params.require(:id), options_for_klient)
input = { planName: plan_name }
current_plan = subscription.prices.select { |price| price['phaseType'] == subscription.phase_type }
override_fixed_price = current_plan.last['recurringPrice'].nil?
override_recurring_price = !override_fixed_price
overrides = price_overrides(subscription.phase_type, override_fixed_price, override_recurring_price)
input[:priceOverrides] = overrides unless overrides.blank?
subscription.change_plan(input,
current_user.kb_username,
params[:reason],
params[:comment],
requested_date,
billing_policy,
nil,
wait_for_completion,
options_for_klient)
redirect_to kaui_engine.account_bundles_path(subscription.account_id), notice: 'Subscription plan successfully changed'
rescue StandardError => e
redirect_to edit_subscription_path(params.require(:id)), flash: { error: "Error while changing subscription: #{as_string(e)}" }
end
|
#update_bcd ⇒ Object
144
145
146
147
148
149
150
151
152
153
154
|
# File 'app/controllers/kaui/subscriptions_controller.rb', line 144
def update_bcd
input_subscription = params.require(:subscription)
subscription = Kaui::Subscription.new
subscription.subscription_id = params.require(:id)
subscription.bill_cycle_day_local = input_subscription['bill_cycle_day_local']
effective_from_date = params['effective_from_date']
subscription.update_bcd(current_user.kb_username, params[:reason], params[:comment], effective_from_date, nil, options_for_klient)
redirect_to kaui_engine.account_bundles_path(input_subscription['account_id']), notice: 'Subscription BCD was successfully changed'
end
|
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'app/controllers/kaui/subscriptions_controller.rb', line 193
def update_tags
subscription_id = params.require(:id)
subscription = Kaui::Subscription.find_by_id(subscription_id, options_for_klient)
tags = []
params.each_key do |key|
next unless key.include? 'tag'
tag_info = key.split('_')
next if (tag_info.size != 2) || (tag_info[0] != 'tag')
tags << tag_info[1]
end
Kaui::Tag.set_for_subscription(subscription_id, tags, current_user.kb_username, params[:reason], params[:comment], options_for_klient)
redirect_to kaui_engine.account_bundles_path(subscription.account_id), notice: 'Subscription tags successfully set'
end
|
#validate_bundle_external_key ⇒ Object
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'app/controllers/kaui/subscriptions_controller.rb', line 165
def validate_bundle_external_key
json_response do
external_key = params.require(:external_key)
begin
bundle = Kaui::Bundle.find_by_external_key(external_key, false, options_for_klient)
rescue KillBillClient::API::NotFound
bundle = nil
end
{ is_found: !bundle.nil? }
end
end
|
#validate_external_key ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'app/controllers/kaui/subscriptions_controller.rb', line 179
def validate_external_key
json_response do
external_key = params.require(:external_key)
begin
subscription = Kaui::Subscription.find_by_external_key(external_key, options_for_klient)
rescue KillBillClient::API::NotFound
subscription = nil
end
{ is_found: !subscription.nil? }
end
end
|