Class: Lago::Api::Resources::Customer

Inherits:
Base
  • Object
show all
Defined in:
lib/lago/api/resources/customer.rb

Instance Attribute Summary

Attributes inherited from Base

#client

Instance Method Summary collapse

Methods inherited from Base

#create, #destroy, #get, #get_all, #initialize, #update

Constructor Details

This class inherits a constructor from Lago::Api::Resources::Base

Instance Method Details

#api_resourceObject



9
10
11
# File 'lib/lago/api/resources/customer.rb', line 9

def api_resource
  'customers'
end

#checkout_url(external_customer_id) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/lago/api/resources/customer.rb', line 77

def checkout_url(external_customer_id)
  uri = URI(
    "#{client.base_api_url}#{api_resource}/#{external_customer_id}/checkout_url",
  )

  response = connection.post({}, uri)[root_name]

  JSON.parse(response.to_json, object_class: OpenStruct)
end

#current_usage(external_customer_id, external_subscription_id, apply_taxes: nil, charge_id: nil, charge_code: nil, billable_metric_code: nil, group: nil, filter_by_charge_id: nil, filter_by_charge_code: nil, filter_by_group: nil, full_usage: nil, filter_by_presentation: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists



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 'lib/lago/api/resources/customer.rb', line 21

def current_usage( # rubocop:disable Metrics/ParameterLists
  external_customer_id, external_subscription_id, apply_taxes: nil,
  charge_id: nil, charge_code: nil, billable_metric_code: nil, group: nil,
  filter_by_charge_id: nil, filter_by_charge_code: nil, filter_by_group: nil, full_usage: nil,
  filter_by_presentation: nil
)
  query_params = { external_subscription_id: external_subscription_id }
  query_params[:apply_taxes] = apply_taxes unless apply_taxes.nil?
  query_params[:charge_id] = charge_id unless charge_id.nil?
  query_params[:charge_code] = charge_code unless charge_code.nil?
  query_params[:billable_metric_code] = billable_metric_code unless billable_metric_code.nil?
  group&.each { |k, v| query_params[:"group[#{k}]"] = v }
  query_params[:filter_by_charge_id] = filter_by_charge_id unless filter_by_charge_id.nil?
  query_params[:filter_by_charge_code] = filter_by_charge_code unless filter_by_charge_code.nil?
  filter_by_group&.each { |k, v| query_params[:"filter_by_group[#{k}]"] = v }
  query_params[:full_usage] = full_usage unless full_usage.nil?
  unless filter_by_presentation.nil?
    query_params[:filter_by_presentation] =
      filter_by_presentation_param(filter_by_presentation)
  end
  query_string = URI.encode_www_form(query_params)

  uri = URI("#{client.base_api_url}#{api_resource}/#{external_customer_id}/current_usage?#{query_string}")
  connection.get(uri, identifier: nil)
end

#filter_by_presentation_param(filter_by_presentation) ⇒ Object



134
135
136
# File 'lib/lago/api/resources/customer.rb', line 134

def filter_by_presentation_param(filter_by_presentation)
  filter_by_presentation.is_a?(String) ? filter_by_presentation : JSON.generate(filter_by_presentation)
end

#past_usage(external_customer_id, external_subscription_id, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/lago/api/resources/customer.rb', line 56

def past_usage(external_customer_id, external_subscription_id, options = {})
  uri = URI(
    "#{client.base_api_url}#{api_resource}/#{external_customer_id}/past_usage",
  )

  connection.get_all(
    options.merge(external_subscription_id: external_subscription_id),
    uri,
  )
end

#portal_url(external_customer_id) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/lago/api/resources/customer.rb', line 67

def portal_url(external_customer_id)
  uri = URI(
    "#{client.base_api_url}#{api_resource}/#{external_customer_id}/portal_url",
  )

  response = connection.get(uri, identifier: nil)[root_name]

  JSON.parse(response.to_json, object_class: OpenStruct).portal_url
end

#projected_usage(external_customer_id, external_subscription_id, apply_taxes: nil) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/lago/api/resources/customer.rb', line 47

def projected_usage(external_customer_id, external_subscription_id, apply_taxes: nil)
  query_params = { external_subscription_id: external_subscription_id }
  query_params[:apply_taxes] = apply_taxes unless apply_taxes.nil?
  query_string = URI.encode_www_form(query_params)

  uri = URI("#{client.base_api_url}#{api_resource}/#{external_customer_id}/projected_usage?#{query_string}")
  connection.get(uri, identifier: nil)
end

#root_nameObject



13
14
15
# File 'lib/lago/api/resources/customer.rb', line 13

def root_name
  'customer'
end

#walletsObject



17
18
19
# File 'lib/lago/api/resources/customer.rb', line 17

def wallets
  Customers::Wallets.new(client)
end

#whitelist_billing_configuration(billing_params) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/lago/api/resources/customer.rb', line 138

def whitelist_billing_configuration(billing_params)
  (billing_params || {}).slice(
    :invoice_grace_period,
    :subscription_invoice_issuing_date_anchor,
    :subscription_invoice_issuing_date_adjustment,
    :payment_provider,
    :payment_provider_code,
    :provider_customer_id,
    :sync,
    :sync_with_provider,
    :document_locale,
    :provider_payment_methods,
  )
end

#whitelist_integration_customers(integration_customers) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/lago/api/resources/customer.rb', line 164

def whitelist_integration_customers(integration_customers)
  processed_integration_customers = []

  (integration_customers || []).each do |m|
    result = (m || {})
      .slice(:id,
             :external_customer_id,
             :integration_type,
             :integration_code,
             :subsidiary_id,
             :sync_with_provider)

    processed_integration_customers << result unless result.empty?
  end

  processed_integration_customers
end

#whitelist_metadata(metadata) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/lago/api/resources/customer.rb', line 182

def ()
   = []

  ( || []).each do |m|
    result = (m || {}).slice(:id, :key, :value, :display_in_invoice)

     << result unless result.empty?
  end

  
end

#whitelist_params(params) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lago/api/resources/customer.rb', line 87

def whitelist_params(params)
  result_hash = {
    external_id: params[:external_id],
    address_line1: params[:address_line1],
    address_line2: params[:address_line2],
    city: params[:city],
    country: params[:country],
    email: params[:email],
    legal_name: params[:legal_name],
    legal_number: params[:legal_number],
    net_payment_term: params[:net_payment_term],
    tax_identification_number: params[:tax_identification_number],
    logo_url: params[:logo_url],
    name: params[:name],
    firstname: params[:firstname],
    lastname: params[:lastname],
    customer_type: params[:customer_type],
    phone: params[:phone],
    state: params[:state],
    url: params[:url],
    zipcode: params[:zipcode],
    currency: params[:currency],
    tax_codes: params[:tax_codes],
    invoice_custom_section_codes: params[:invoice_custom_section_codes],
    timezone: params[:timezone],
    finalize_zero_amount_invoice: params[:finalize_zero_amount_invoice],
    skip_invoice_custom_sections: params[:skip_invoice_custom_sections],
    billing_entity_code: params[:billing_entity_code],
  }

  whitelist_billing_configuration(params[:billing_configuration]).tap do |config|
    result_hash[:billing_configuration] = config unless config.empty?
  end

  whitelist_shipping_address(params[:shipping_address]).tap do |address|
    result_hash[:shipping_address] = address unless address.empty?
  end

  integration_customers = whitelist_integration_customers(params[:integration_customers])
  result_hash[:integration_customers] = integration_customers unless integration_customers.empty?

   = (params[:metadata])
  result_hash[:metadata] =  unless .empty?

  { root_name => result_hash }
end

#whitelist_shipping_address(address) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'lib/lago/api/resources/customer.rb', line 153

def whitelist_shipping_address(address)
  (address || {}).slice(
    :address_line1,
    :address_line2,
    :city,
    :zipcode,
    :state,
    :country,
  )
end