Class: Dependencies::Aviate::Metering
- Inherits:
-
Object
- Object
- Dependencies::Aviate::Metering
- Defined in:
- app/services/dependencies/aviate.rb
Overview
Thin wrapper around the Aviate plugin's Catalog and Metering REST APIs (https://apidocs.killbill.io/aviate-catalog.html, https://apidocs.killbill.io/aviate-metering.html). These are not (yet) exposed by the killbill-client gem, so we call them directly.
Unlike most Kill Bill APIs, these endpoints do NOT accept tenant RBAC (username/password) or
api_key/api_secret alone - they require a JWT obtained via the Aviate Auth API
(https://apidocs.killbill.io/aviate-auth.html), passed as a jwt_token entry in options_for_klient
(see Aviate::EngineController#options_for_klient in killbill-aviate-ui, which stores it in a
jwt_token cookie after logging in via the Aviate Configuration page). If no jwt_token is present,
calls will fail authentication and callers should treat that as "unknown/not Aviate".
Constant Summary collapse
- KILLBILL_AVIATE_PREFIX =
'/plugins/aviate-plugin/v1'- CATALOG_STATE_AVIATE =
'CATALOG_STATE_AVIATE'
Class Method Summary collapse
-
.aviate_catalog?(account_id, options_for_klient) ⇒ Boolean
Returns true if the tenant/account is using an Aviate catalog (as opposed to an XML one).
-
.billing_meter_codes_for_plan(plan_name, account_id, options_for_klient) ⇒ Object
Returns the list of billing meter codes configured on the given plan's usage phases.
-
.submit_usage_event(account_id, billing_meter_code, subscription_id, tracking_id, timestamp, value, options_for_klient) ⇒ Object
Submits a single usage event via the Aviate metering plugin.
Class Method Details
.aviate_catalog?(account_id, options_for_klient) ⇒ Boolean
Returns true if the tenant/account is using an Aviate catalog (as opposed to an XML one). Any error (plugin not installed, missing/invalid JWT, network issue, etc.) is treated as "not Aviate" so that callers can safely fall back to the existing XML-catalog behavior.
23 24 25 26 27 28 29 |
# File 'app/services/dependencies/aviate.rb', line 23 def aviate_catalog?(account_id, ) response = KillBillClient::API.get("#{KILLBILL_AVIATE_PREFIX}/catalog/info", catalog_params(account_id), ()) JSON.parse(response.body)['state'] == CATALOG_STATE_AVIATE rescue StandardError => e Rails.logger.warn("Failed to retrieve Aviate catalog info: #{e.class}: #{e.}") false end |
.billing_meter_codes_for_plan(plan_name, account_id, options_for_klient) ⇒ Object
Returns the list of billing meter codes configured on the given plan's usage phases.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/services/dependencies/aviate.rb', line 32 def billing_meter_codes_for_plan(plan_name, account_id, ) return [] if plan_name.blank? response = KillBillClient::API.get("#{KILLBILL_AVIATE_PREFIX}/catalog/#{plan_name}/plan", catalog_params(account_id), ()) plan = JSON.parse(response.body) codes = [] Array(plan['phases']).each do |phase| Array(phase['usages']).each do |usage| Array(usage['tiers']).each do |tier| Array(tier['blocks']).each do |block| codes << block['billingMeterCode'] if block['billingMeterCode'].present? end end end end codes.uniq rescue StandardError => e Rails.logger.warn("Failed to retrieve Aviate billing meter codes for plan #{plan_name}: #{e.class}: #{e.}") [] end |
.submit_usage_event(account_id, billing_meter_code, subscription_id, tracking_id, timestamp, value, options_for_klient) ⇒ Object
Submits a single usage event via the Aviate metering plugin.
55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/services/dependencies/aviate.rb', line 55 def submit_usage_event(account_id, billing_meter_code, subscription_id, tracking_id, , value, ) body = [{ billingMeterCode: billing_meter_code, subscriptionId: subscription_id, trackingId: tracking_id, timestamp: , value: value }].to_json KillBillClient::API.post("#{KILLBILL_AVIATE_PREFIX}/metering/billing/#{account_id}", body, {}, ()) end |