Class: ForestLiana::StripeInvoicesGetter

Inherits:
Object
  • Object
show all
Defined in:
app/services/forest_liana/stripe_invoices_getter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, secret_key, reference) ⇒ StripeInvoicesGetter

Returns a new instance of StripeInvoicesGetter.



5
6
7
8
# File 'app/services/forest_liana/stripe_invoices_getter.rb', line 5

def initialize(params, secret_key, reference)
  @params = params
  Stripe.api_key = ForestLiana.integrations[:stripe][:api_key]
end

Instance Attribute Details

#recordsObject

Returns the value of attribute records.



3
4
5
# File 'app/services/forest_liana/stripe_invoices_getter.rb', line 3

def records
  @records
end

Instance Method Details

#collectionObject



84
85
86
# File 'app/services/forest_liana/stripe_invoices_getter.rb', line 84

def collection
  @params[:collection].singularize.capitalize.constantize
end

#countObject



10
11
12
# File 'app/services/forest_liana/stripe_invoices_getter.rb', line 10

def count
  @invoices.try(:total_count) || 0
end

#ending_beforeObject



64
65
66
67
68
# File 'app/services/forest_liana/stripe_invoices_getter.rb', line 64

def ending_before
  if pagination? && @params[:ending_before]
    @params[:ending_before]
  end
end

#fetch_invoices(params) ⇒ Object



53
54
55
56
# File 'app/services/forest_liana/stripe_invoices_getter.rb', line 53

def fetch_invoices(params)
  return if @params[:id] && params[:customer].blank?
  Stripe::Invoice.all(params)
end

#fieldObject



88
89
90
91
92
93
# File 'app/services/forest_liana/stripe_invoices_getter.rb', line 88

def field
  ForestLiana.integrations[:stripe][:mapping].select { |value|
    value.split('.')[0] == ForestLiana::SchemaUtils
      .find_model_from_table_name(@params[:collection]).try(:name)
  }.first.split('.')[1]
end

#limitObject



70
71
72
73
74
75
76
77
78
# File 'app/services/forest_liana/stripe_invoices_getter.rb', line 70

def limit
  return 10 unless pagination?

  if @params[:page][:size]
    @params[:page][:size].to_i
  else
    10
  end
end

#pagination?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'app/services/forest_liana/stripe_invoices_getter.rb', line 80

def pagination?
  @params[:page]
end

#performObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/forest_liana/stripe_invoices_getter.rb', line 14

def perform
  query = {
    limit: limit,
    starting_after: starting_after,
    ending_before: ending_before
  }

  if @params[:id] && collection && field
    resource = collection.find(@params[:id])
    query[:customer] = resource[field]
  end

  query['include[]'] = 'total_count'
  @invoices = fetch_invoices(query)
  if @invoices.blank?
    @records = []
    return
  end

  @records = @invoices.data.map do |d|
    d.date = Time.at(d.date).to_datetime
    d.period_start = Time.at(d.period_start).to_datetime
    d.period_end = Time.at(d.period_end).to_datetime
    d.subtotal /= 100.00
    d.total /= 100.00
    d.amount_due /= 100.00

    query = {}
    query[field] = d.customer
    if collection
      d.customer = collection.find_by(query)
    else
      d.customer = nil
    end

    d
  end
end

#starting_afterObject



58
59
60
61
62
# File 'app/services/forest_liana/stripe_invoices_getter.rb', line 58

def starting_after
  if pagination? && @params[:starting_after]
    @params[:starting_after]
  end
end