Class: ForestLiana::StripePaymentsGetter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, secret_key, reference) ⇒ StripePaymentsGetter

Returns a new instance of StripePaymentsGetter.



5
6
7
8
# File 'app/services/forest_liana/stripe_payments_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_payments_getter.rb', line 3

def records
  @records
end

Instance Method Details

#countObject



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

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

#fetch_charges(params) ⇒ Object



47
48
49
50
# File 'app/services/forest_liana/stripe_payments_getter.rb', line 47

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

#limitObject



63
64
65
66
67
68
69
70
71
# File 'app/services/forest_liana/stripe_payments_getter.rb', line 63

def limit
  return 10 unless pagination?

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

#offsetObject



52
53
54
55
56
57
58
59
60
61
# File 'app/services/forest_liana/stripe_payments_getter.rb', line 52

def offset
  return 0 unless pagination?

  number = @params[:page][:number]
  if number && number.to_i > 0
    (number.to_i - 1) * limit
  else
    0
  end
end

#pagination?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/services/forest_liana/stripe_payments_getter.rb', line 73

def pagination?
  @params[:page] && @params[:page][:number]
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
# File 'app/services/forest_liana/stripe_payments_getter.rb', line 14

def perform
  query = { limit: limit, offset: offset }

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

  query['source'] = { object: :card }
  query['include[]'] = 'total_count'

  @charges = fetch_charges(query)
  if @charges.blank?
    @records = []
    return
  end

  @records = @charges.data.map do |d|
    d.created = Time.at(d.created).to_datetime
    d.amount /= 100.00

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

    d
  end
end

#user_collectionObject



77
78
79
80
81
82
# File 'app/services/forest_liana/stripe_payments_getter.rb', line 77

def user_collection
  ForestLiana.integrations
    .try(:[], :stripe)
    .try(:[], :user_collection)
    .try(:constantize)
end

#user_fieldObject



84
85
86
87
88
# File 'app/services/forest_liana/stripe_payments_getter.rb', line 84

def user_field
  ForestLiana.integrations
    .try(:[], :stripe)
    .try(:[], :user_field)
end