Extract parent class, validate plans can be downgraded/upgraded
This commit is contained in:
@@ -1,28 +1,12 @@
|
||||
class Account::Subscriptions::DowngradesController < ApplicationController
|
||||
before_action :ensure_admin
|
||||
|
||||
def create
|
||||
portal_session = Stripe::BillingPortal::Session.create(
|
||||
customer: subscription.stripe_customer_id,
|
||||
return_url: account_settings_url(anchor: "subscription"),
|
||||
flow_data: {
|
||||
type: "subscription_update_confirm",
|
||||
subscription_update_confirm: {
|
||||
subscription: subscription.stripe_subscription_id,
|
||||
items: [ { id: stripe_subscription_item_id, price: Plan.paid.stripe_price_id } ]
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
redirect_to portal_session.url, allow_other_host: true
|
||||
end
|
||||
class Account::Subscriptions::DowngradesController < Account::Subscriptions::UpdatePlanController
|
||||
before_action :ensure_downgradeable
|
||||
|
||||
private
|
||||
def subscription
|
||||
@subscription ||= Current.account.subscription
|
||||
def target_plan
|
||||
Plan.paid
|
||||
end
|
||||
|
||||
def stripe_subscription_item_id
|
||||
Stripe::Subscription.retrieve(subscription.stripe_subscription_id).items.data.first.id
|
||||
def ensure_downgradeable
|
||||
head :bad_request unless subscription.plan == Plan.paid_with_extra_storage
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
class Account::Subscriptions::UpdatePlanController < ApplicationController
|
||||
before_action :ensure_admin
|
||||
|
||||
def create
|
||||
portal_session = Stripe::BillingPortal::Session.create(
|
||||
customer: subscription.stripe_customer_id,
|
||||
return_url: account_settings_url(anchor: "subscription"),
|
||||
flow_data: {
|
||||
type: "subscription_update_confirm",
|
||||
subscription_update_confirm: {
|
||||
subscription: subscription.stripe_subscription_id,
|
||||
items: [ { id: stripe_subscription_item_id, price: target_plan.stripe_price_id } ]
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
redirect_to portal_session.url, allow_other_host: true
|
||||
end
|
||||
|
||||
private
|
||||
def target_plan
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def subscription
|
||||
@subscription ||= Current.account.subscription
|
||||
end
|
||||
|
||||
def stripe_subscription_item_id
|
||||
Stripe::Subscription.retrieve(subscription.stripe_subscription_id).items.data.first.id
|
||||
end
|
||||
end
|
||||
@@ -1,28 +1,12 @@
|
||||
class Account::Subscriptions::UpgradesController < ApplicationController
|
||||
before_action :ensure_admin
|
||||
|
||||
def create
|
||||
portal_session = Stripe::BillingPortal::Session.create(
|
||||
customer: subscription.stripe_customer_id,
|
||||
return_url: account_settings_url(anchor: "subscription"),
|
||||
flow_data: {
|
||||
type: "subscription_update_confirm",
|
||||
subscription_update_confirm: {
|
||||
subscription: subscription.stripe_subscription_id,
|
||||
items: [ { id: stripe_subscription_item_id, price: Plan.paid_with_extra_storage.stripe_price_id } ]
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
redirect_to portal_session.url, allow_other_host: true
|
||||
end
|
||||
class Account::Subscriptions::UpgradesController < Account::Subscriptions::UpdatePlanController
|
||||
before_action :ensure_upgradeable
|
||||
|
||||
private
|
||||
def subscription
|
||||
@subscription ||= Current.account.subscription
|
||||
def target_plan
|
||||
Plan.paid_with_extra_storage
|
||||
end
|
||||
|
||||
def stripe_subscription_item_id
|
||||
Stripe::Subscription.retrieve(subscription.stripe_subscription_id).items.data.first.id
|
||||
def ensure_upgradeable
|
||||
head :bad_request unless subscription.plan == Plan.paid
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,7 +6,7 @@ class Account::Subscriptions::DowngradesControllerTest < ActionDispatch::Integra
|
||||
sign_in_as :kevin
|
||||
accounts(:"37s").subscription.update!(
|
||||
stripe_subscription_id: "sub_123",
|
||||
plan_key: Plan.paid_with_extra_storage.key
|
||||
plan: Plan.paid_with_extra_storage
|
||||
)
|
||||
end
|
||||
|
||||
@@ -30,4 +30,11 @@ class Account::Subscriptions::DowngradesControllerTest < ActionDispatch::Integra
|
||||
post account_subscription_downgrade_path
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
test "downgrade requires downgradeable plan" do
|
||||
accounts(:"37s").subscription.update!(plan: Plan.paid)
|
||||
|
||||
post account_subscription_downgrade_path
|
||||
assert_response :bad_request
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,7 +6,7 @@ class Account::Subscriptions::UpgradesControllerTest < ActionDispatch::Integrati
|
||||
sign_in_as :kevin
|
||||
accounts(:"37s").subscription.update!(
|
||||
stripe_subscription_id: "sub_123",
|
||||
plan_key: Plan.paid.key
|
||||
plan: Plan.paid
|
||||
)
|
||||
end
|
||||
|
||||
@@ -30,4 +30,11 @@ class Account::Subscriptions::UpgradesControllerTest < ActionDispatch::Integrati
|
||||
post account_subscription_upgrade_path
|
||||
assert_response :forbidden
|
||||
end
|
||||
|
||||
test "upgrade requires upgradeable plan" do
|
||||
accounts(:"37s").subscription.update!(plan: Plan.paid_with_extra_storage)
|
||||
|
||||
post account_subscription_upgrade_path
|
||||
assert_response :bad_request
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user