Class: Profitable::Processors::StripeProcessor

Inherits:
Base
  • Object
show all
Defined in:
lib/profitable/processors/stripe_processor.rb

Instance Attribute Summary

Attributes inherited from Base

#subscription

Instance Method Summary collapse

Methods inherited from Base

#initialize, subscription_data

Constructor Details

This class inherits a constructor from Profitable::Processors::Base

Instance Method Details

#calculate_mrrObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/profitable/processors/stripe_processor.rb', line 4

def calculate_mrr
  data = subscription_data
  return 0 if data.nil?

  # Pay gem v10+ stores items at object['items']['data']
  # Older versions stored at data['subscription_items']
  subscription_items = data.dig('items', 'data') || data['subscription_items']
  return 0 if subscription_items.nil? || subscription_items.empty?

  # Sum MRR from ALL subscription items (not just the first one)
  # Stripe subscriptions can have multiple line items
  total_mrr = 0

  subscription_items.each do |item|
    price_data = item['price'] || item
    next if price_data.nil?

    # Metered items are usage-based, so they do not have a fixed run-rate that
    # belongs in MRR/ARR style metrics. Keep only licensed recurring items here.
    next if price_data.dig('recurring', 'usage_type') == 'metered'

    amount = price_data['unit_amount']
    next if amount.nil?

    # Each item can have its own quantity
    item_quantity = item['quantity'] || 1
    interval = price_data.dig('recurring', 'interval')
    interval_count = price_data.dig('recurring', 'interval_count') || 1

    total_mrr += normalize_to_monthly(amount * item_quantity, interval, interval_count)
  end

  total_mrr
end