Instantly share code, notes, and snippets.

Embed
What would you like to do?
Stripe MRR Calculation
require 'stripe'
require 'ostruct'
# modified from: https://gist.github.com/jacobpatton/a68d228bf2414852d862
#
# puts Stripe::Mrr.new(api_key: 'api_key').mrr
#
module Stripe
class Mrr
attr_reader :api_key
def initialize(args = {})
raise ArgumentError, ":api_key is a required argument" unless args[:api_key]
@api_key = args[:api_key]
Stripe.api_key = @api_key
@charges = []
@time = Time.now.to_i
end
def mrr
subscriptions.inject(0) do |sum, subscription|
amount, interval = subscription.amount, subscription.interval
amount = amount / 12 if interval == 'year'
sum + amount
end
end
def charges
fetch_charges if @charges.empty?
@charges
end
def subscriptions
charges.inject([]) do |collection, charge|
puts charge.id
next collection unless charge.paid
next collection if charge.invoice.nil?
puts charge.invoice
invoice = Stripe::Invoice.retrieve(charge.invoice)
subscription = invoice.lines.data.find {|l| l.type == 'subscription'}
if subscription.nil?
puts 'no subscription'
next collection
end
next collection unless subscription.period.end > @time
collection.concat([OpenStruct.new(amount: invoice.amount_due, interval: subscription.plan.interval)])
end
end
private
def fetch_charges(opts = {})
opts = {limit: 100}.merge!(opts)
collection = Stripe::Charge.all(opts)
@charges.concat(collection.data)
fetch_charges(starting_after: @charges.last.id) if collection.has_more
end
end
end
@bcackerman

This comment has been minimized.

Copy link

bcackerman commented Nov 7, 2014

Got this error: NoMethodError: undefined method `data' for #Stripe::StripeObject:0x007fb2b2726b88

@siong1987

This comment has been minimized.

Copy link
Owner Author

siong1987 commented Nov 11, 2014

line number?

@bcackerman

This comment has been minimized.

Copy link

bcackerman commented Nov 12, 2014

Sorry, line 44

@bcackerman

This comment has been minimized.

Copy link

bcackerman commented Nov 12, 2014

/Users/Ackerman/.rvm/gems/ruby-1.9.3-p484/gems/stripe-1.14.0/lib/stripe/stripe_object.rb:173:in `method_missing': undefined method `data' for #<Stripe::StripeObject:0x007ff9cbd4be18> (NoMethodError)
    from stripe_mrr.rb:44:in `block in subscriptions'
    from stripe_mrr.rb:37:in `each'
    from stripe_mrr.rb:37:in `inject'
    from stripe_mrr.rb:37:in `subscriptions'
    from stripe_mrr.rb:23:in `mrr'
    from stripe_mrr.rb:68:in `<main>'
@bcackerman

This comment has been minimized.

Copy link

bcackerman commented Nov 12, 2014

I did a puts invoice.lines.data right before the error and this is the output

#<Stripe::StripeObject:0x3fd8bec477ec> JSON: {
  "subscriptions": [
    {"id":"sub_4lBuQuiFO22JLs","object":"line_item","type":"subscription","livemode":true,"amount":4900,"currency":"usd","proration":false,"period":{"start":1415742863,"end":1418334863},"subscription":null,"quantity":1,"plan":{"id":"simple","interval":"month","name":"Simple","created":1390971289,"amount":4900,"currency":"usd","object":"plan","livemode":true,"interval_count":1,"metadata":{},"statement_description":null},"description":null,"metadata":{}}
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment