From 689525ce853b3f84548aa5329058a6c0dfeb0cd5 Mon Sep 17 00:00:00 2001 From: Tanmay Deep Sharma Date: Mon, 17 Nov 2025 11:57:27 +0530 Subject: [PATCH] fix credit addition for topup and monthly --- app/helpers/billing_helper.rb | 5 ++++- .../billing/handle_stripe_event_service.rb | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/helpers/billing_helper.rb b/app/helpers/billing_helper.rb index e2ada7e86..65c0b55b5 100644 --- a/app/helpers/billing_helper.rb +++ b/app/helpers/billing_helper.rb @@ -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) diff --git a/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb b/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb index fac927eef..4d2978b07 100644 --- a/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb +++ b/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb @@ -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?