fix credit addition for topup and monthly

This commit is contained in:
Tanmay Deep Sharma
2025-11-17 11:57:27 +05:30
parent 6abecdfd43
commit 689525ce85
2 changed files with 20 additions and 3 deletions
+4 -1
View File
@@ -8,7 +8,10 @@ module BillingHelper
# Return false if not plans are configured, so that no checks are enforced
return false if default_plan.blank?
account.custom_attributes['plan_name'].nil? || account.custom_attributes['plan_name'] == default_plan['name']
# Handle both string and hash formats for default_plan
default_plan_name = default_plan.is_a?(Hash) ? default_plan['name'] : default_plan
account.custom_attributes['plan_name'].nil? || account.custom_attributes['plan_name'] == default_plan_name
end
def conversations_this_month(account)
@@ -89,14 +89,29 @@ class Enterprise::Billing::HandleStripeEventService
amount = extract_credit_amount(grant)
return if amount.zero?
grant_type = extract_grant_type(grant)
service = Enterprise::Billing::V2::CreditManagementService.new(account: account)
service.add_response_topup_credits(amount)
if grant_type == 'monetary'
service.add_response_topup_credits(amount)
else
service.sync_monthly_response_credits(amount)
end
end
def extract_credit_grant_id(grant_object)
grant_object.respond_to?(:id) ? grant_object.id : grant_object['id']
end
def extract_grant_type(grant)
amount_object = extract_attribute(grant, :amount)
return 'monetary' if amount_object.blank?
type = extract_attribute(amount_object, :type)
return 'monetary' if type.blank?
type
end
def extract_credit_amount(grant)
# First, try to get credits from metadata
metadata = extract_attribute(grant, :metadata)
@@ -104,7 +119,6 @@ class Enterprise::Billing::HandleStripeEventService
credits = extract_attribute(metadata, :credits)
return credits.to_i if credits.present? && credits.to_i.positive?
end
amount = extract_attribute(grant, :amount)
return 0 if amount.blank?