remove update subscription flow overall
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user