From 40608f4494b7558eccf38e2977964d484fea2822 Mon Sep 17 00:00:00 2001 From: Tanmay Deep Sharma Date: Wed, 29 Oct 2025 21:35:08 +0530 Subject: [PATCH] remove update subscription flow overall --- app/policies/account_policy.rb | 4 - config/routes.rb | 1 - .../api/v1/accounts/concerns/billing_v2.rb | 16 -- .../billing/v2/update_subscription_service.rb | 147 ------------------ 4 files changed, 168 deletions(-) delete mode 100644 enterprise/app/services/enterprise/billing/v2/update_subscription_service.rb diff --git a/app/policies/account_policy.rb b/app/policies/account_policy.rb index ee5e2b37f..212af6c34 100644 --- a/app/policies/account_policy.rb +++ b/app/policies/account_policy.rb @@ -59,10 +59,6 @@ class AccountPolicy < ApplicationPolicy @account_user.administrator? end - def update_subscription_quantity? - @account_user.administrator? - end - def change_pricing_plan? @account_user.administrator? end diff --git a/config/routes.rb b/config/routes.rb index b8dc61d1b..28826d84c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -439,7 +439,6 @@ Rails.application.routes.draw do post :v2_topup post :v2_subscribe post :cancel_subscription - post :update_subscription_quantity post :change_pricing_plan end end diff --git a/enterprise/app/controllers/enterprise/api/v1/accounts/concerns/billing_v2.rb b/enterprise/app/controllers/enterprise/api/v1/accounts/concerns/billing_v2.rb index 6fa71998a..97c6c22cc 100644 --- a/enterprise/app/controllers/enterprise/api/v1/accounts/concerns/billing_v2.rb +++ b/enterprise/app/controllers/enterprise/api/v1/accounts/concerns/billing_v2.rb @@ -77,22 +77,6 @@ module Enterprise::Api::V1::Accounts::Concerns::BillingV2 end end - def update_subscription_quantity - service = Enterprise::Billing::V2::UpdateSubscriptionService.new(account: @account) - result = service.update_quantity(quantity: params[:quantity].to_i) - - if result[:success] - # Include account ID and updated attributes for frontend store update - @account.reload - render json: result.merge( - id: @account.id, - custom_attributes: @account.custom_attributes - ) - else - render json: { error: result[:message] }, status: :unprocessable_entity - end - end - def change_pricing_plan service = Enterprise::Billing::V2::ChangePlanService.new(account: @account) result = service.change_plan( diff --git a/enterprise/app/services/enterprise/billing/v2/update_subscription_service.rb b/enterprise/app/services/enterprise/billing/v2/update_subscription_service.rb deleted file mode 100644 index 0f6328fe5..000000000 --- a/enterprise/app/services/enterprise/billing/v2/update_subscription_service.rb +++ /dev/null @@ -1,147 +0,0 @@ -class Enterprise::Billing::V2::UpdateSubscriptionService < Enterprise::Billing::V2::BaseService - include Enterprise::Billing::Concerns::PlanFeatureManager - - # Update subscription quantity using Stripe's V2 Billing Intent API - # Creates a modify billing intent to change the subscription quantity - # - # @param quantity [Integer] The new quantity - # @return [Hash] { success:, message: } - # - def update_quantity(quantity:) - return { success: false, message: 'Invalid quantity' } unless quantity.positive? - - with_locked_account do - billing_intent = create_modify_intent(quantity) - reserve_billing_intent(billing_intent) - commit_billing_intent(billing_intent) - update_account_quantity(quantity) - success_response(quantity) - end - rescue Stripe::StripeError => e - { success: false, message: "Stripe error: #{e.message}" } - rescue StandardError => e - { success: false, message: "Update error: #{e.message}" } - end - - private - - def create_modify_intent(quantity) - subscription_id = fetch_subscription_id - subscription = retrieve_pricing_plan_subscription(subscription_id) - cadence_id, plan_id, plan_version = extract_subscription_details(subscription) - store_next_billing_date(cadence_id) - lookup_key = fetch_plan_lookup_key(plan_id) - component_config = { lookup_key: lookup_key, quantity: quantity } - - params = build_modify_params(subscription_id, cadence_id, plan_id, plan_version, component_config) - - StripeV2Client.request(:post, '/v2/billing/intents', params, stripe_api_options) - end - - def fetch_subscription_id - custom_attribute('stripe_subscription_id').tap do |id| - raise StandardError, 'No pricing plan subscription ID found' if id.blank? - end - end - - def extract_subscription_details(subscription) - cadence = extract_attribute(subscription, :billing_cadence) - plan = extract_attribute(subscription, :pricing_plan) - version = extract_attribute(subscription, :pricing_plan_version) - - raise StandardError, 'No billing cadence found in subscription' if cadence.blank? - raise StandardError, 'No pricing plan found in subscription' if plan.blank? - raise StandardError, 'No pricing plan version found in subscription' if version.blank? - - [cadence, plan, version] - end - - def fetch_plan_lookup_key(plan_id) - Enterprise::Billing::V2::PlanCatalog.lookup_key_for_plan(plan_id).tap do |key| - raise StandardError, "Lookup key not found for pricing plan #{plan_id}" unless key - end - end - - def build_modify_params(subscription_id, cadence_id, plan_id, plan_version, component_config) - { - cadence: cadence_id, - currency: 'usd', - actions: [{ - type: 'modify', - modify: { - type: 'pricing_plan_subscription_details', - pricing_plan_subscription_details: { - pricing_plan_subscription: subscription_id, - new_pricing_plan: plan_id, - new_pricing_plan_version: plan_version, - component_configurations: [component_config] - } - } - }] - } - end - - def reserve_billing_intent(billing_intent) - StripeV2Client.request( - :post, - "/v2/billing/intents/#{billing_intent.id}/reserve", - {}, - stripe_api_options - ) - end - - def commit_billing_intent(billing_intent) - StripeV2Client.request( - :post, - "/v2/billing/intents/#{billing_intent.id}/commit", - {}, - stripe_api_options - ) - end - - def retrieve_pricing_plan_subscription(subscription_id) - StripeV2Client.request( - :get, - "/v2/billing/pricing_plan_subscriptions/#{subscription_id}", - {}, - stripe_api_options - ) - end - - def retrieve_billing_cadence(cadence_id) - StripeV2Client.request( - :get, - "/v2/billing/cadences/#{cadence_id}", - {}, - stripe_api_options - ) - end - - def store_next_billing_date(cadence_id) - cadence = retrieve_billing_cadence(cadence_id) - @next_billing_date = extract_attribute(cadence, :next_billing_date) - end - - def update_account_quantity(quantity) - update_custom_attributes({ - 'pending_subscription_quantity' => quantity, - 'next_billing_date' => @next_billing_date - }) - end - - def success_response(quantity) - { - success: true, - quantity: quantity, - message: "Subscription quantity updated to #{quantity}" - } - end - - def stripe_api_options - { api_key: ENV.fetch('STRIPE_SECRET_KEY', nil), stripe_version: '2025-08-27.preview' } - end - - def extract_attribute(object, key) - object.respond_to?(key) ? object.public_send(key) : object[key.to_s] - end -end