do not deduct or refund credits for audio transactions

This commit is contained in:
Tanmay Sharma
2025-10-29 17:46:39 +05:30
parent e10ef03490
commit b9f68fc839
3 changed files with 1 additions and 51 deletions
@@ -1,5 +1,3 @@
# frozen_string_literal: true
module Enterprise::Billing::Concerns::PlanFeatureManager
extend ActiveSupport::Concern
@@ -13,8 +13,7 @@ class Enterprise::Billing::V2::ChangePlanService < Enterprise::Billing::V2::Base
with_locked_account do
billing_intent = create_change_plan_intent(new_pricing_plan_id, quantity)
reserved_intent = reserve_billing_intent(billing_intent)
handle_payment_if_needed(reserved_intent)
reserve_billing_intent(billing_intent)
commit_billing_intent(billing_intent)
update_account_plan(new_pricing_plan_id, quantity)
success_response(new_pricing_plan_id, quantity)
@@ -94,35 +93,6 @@ class Enterprise::Billing::V2::ChangePlanService < Enterprise::Billing::V2::Base
)
end
def handle_payment_if_needed(reserved_intent)
# Check if there's a proration charge that needs to be paid
total_amount = extract_attribute(reserved_intent['amount_details'], 'total')
return if total_amount.nil? || total_amount.to_i <= 0
# Get customer's default payment method
stripe_customer_id = custom_attribute('stripe_customer_id')
raise StandardError, 'No Stripe customer ID found' if stripe_customer_id.blank?
customer = Stripe::Customer.retrieve(stripe_customer_id)
payment_method_id = customer.invoice_settings&.default_payment_method
raise StandardError, 'No default payment method found. Please add a payment method first.' if payment_method_id.blank?
# Create and confirm payment intent for proration charge
Stripe::PaymentIntent.create({
amount: total_amount.to_i,
currency: 'usd',
customer: stripe_customer_id,
payment_method: payment_method_id,
confirm: true,
automatic_payment_methods: { enabled: true, allow_redirects: 'never' },
metadata: {
account_id: account.id,
type: 'plan_change_proration'
}
})
end
def commit_billing_intent(billing_intent)
StripeV2Client.request(
:post,
@@ -15,10 +15,6 @@ class Messages::AudioTranscriptionService < Llm::BaseOpenAiService
transcriptions = transcribe_audio
Rails.logger.info "Audio transcription successful: #{transcriptions}"
{ success: true, transcriptions: transcriptions }
rescue StandardError => e
# Refund credit if transcription failed
refund_credit
raise e
end
private
@@ -30,20 +26,6 @@ class Messages::AudioTranscriptionService < Llm::BaseOpenAiService
account.usage_limits[:captain][:responses][:current_available].positive?
end
def refund_credit
credit_service = Enterprise::Ai::CaptainCreditService.new(account: account)
credit_service.check_and_use_credits(
feature: 'ai_audio_transcription',
amount: -1, # Negative to refund
metadata: {
'message_id' => message.id,
'attachment_id' => attachment.id,
'conversation_id' => message.conversation_id,
'refund' => true
}
)
end
def fetch_audio_file
temp_dir = Rails.root.join('tmp/uploads')
FileUtils.mkdir_p(temp_dir)