class StripeDeclineRate

Public Class Methods

new(api_key) click to toggle source
# File lib/stripe_decline_rate.rb, line 6
def initialize(api_key)
  Stripe.api_key = api_key
end

Public Instance Methods

base_params(from, to) click to toggle source
# File lib/stripe_decline_rate.rb, line 60
def base_params(from, to)
  {
    limit: 100,
    created: {
      gte: from.to_time.to_i,
      lt: to.to_time.to_i
    }
  }
end
charge_fingerprints(params) click to toggle source
# File lib/stripe_decline_rate.rb, line 70
def charge_fingerprints(params)
  list = Stripe::Event.all(params)
  fingerprints = unique_fingerprints(list)
  while list[:has_more] do
    last_event_id = list[:data].last[:id]
    list = Stripe::Event.all(params.merge(starting_after: last_event_id))
    fingerprints.merge(unique_fingerprints(list))
  end
  fingerprints
end
decline_charge_params(from, to) click to toggle source
# File lib/stripe_decline_rate.rb, line 54
def decline_charge_params(from, to)
  base_params(from, to).merge({
    type: 'charge.failed'
  })
end
decline_ratio(from, to) click to toggle source
# File lib/stripe_decline_rate.rb, line 34
def decline_ratio(from, to)
  declined_count = declined_charge_fingerprints(from, to).count
  successful_count = successful_charge_fingerprints(from, to).count
  declined_count.to_f / (declined_count + successful_count).to_f
end
declined_charge_fingerprints(from, to) click to toggle source
# File lib/stripe_decline_rate.rb, line 44
def declined_charge_fingerprints(from, to)
  charge_fingerprints(decline_charge_params(from, to))
end
formatted_decline_ratio(from, to) click to toggle source
# File lib/stripe_decline_rate.rb, line 30
def formatted_decline_ratio(from, to)
  "#{(decline_ratio(from, to) * 100).round(2)}%"
end
latest() click to toggle source
# File lib/stripe_decline_rate.rb, line 10
def latest
  saturday = starting_saturday
  sunday = starting_sunday

  10.times do |i|
    puts "#{sunday} through #{saturday}: #{formatted_decline_ratio(sunday, saturday)}"

    sunday = sunday - 7
    saturday = saturday - 7
  end
end
starting_saturday() click to toggle source
# File lib/stripe_decline_rate.rb, line 22
def starting_saturday
  (Date.today - Date.today.wday).prev_day
end
starting_sunday() click to toggle source
# File lib/stripe_decline_rate.rb, line 26
def starting_sunday
  Date.today - Date.today.wday - 7
end
successful_charge_fingerprints(from, to) click to toggle source
# File lib/stripe_decline_rate.rb, line 40
def successful_charge_fingerprints(from, to)
  charge_fingerprints(successful_charge_params(from, to))
end
successful_charge_params(from, to) click to toggle source
# File lib/stripe_decline_rate.rb, line 48
def successful_charge_params(from, to)
  base_params(from, to).merge({
    type: 'charge.succeeded'
  })
end
unique_fingerprints(list) click to toggle source
# File lib/stripe_decline_rate.rb, line 81
def unique_fingerprints(list)
  list[:data].map do |event|
    event[:data][:object][:source][:fingerprint]
  end.to_set
end