Class: Lago::Api::Resources::CreditNote

Inherits:
Base
  • Object
show all
Defined in:
lib/lago/api/resources/credit_note.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



7
8
9
# File 'lib/lago/api/resources/credit_note.rb', line 7

def api_resource
  'credit_notes'
end

#download(credit_note_id) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/lago/api/resources/credit_note.rb', line 41

def download(credit_note_id)
  path = "/api/v1/credit_notes/#{credit_note_id}/download"
  response = connection.post({}, path)

  return response unless response.is_a?(Hash)

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

#estimate(params) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/lago/api/resources/credit_note.rb', line 61

def estimate(params)
  uri = URI("#{client.base_api_url}#{api_resource}/estimate")

  payload = whitelist_estimate_params(params)
  response = connection.post(payload, uri)['estimated_credit_note']

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

#root_nameObject



11
12
13
# File 'lib/lago/api/resources/credit_note.rb', line 11

def root_name
  'credit_note'
end

#void(credit_note_id) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/lago/api/resources/credit_note.rb', line 50

def void(credit_note_id)
  path = "/api/v1/credit_notes/#{credit_note_id}/void"
  response = connection.put(
    path,
    identifier: nil,
    body: {}
  )

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

#whitelist_estimate_params(params) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/lago/api/resources/credit_note.rb', line 70

def whitelist_estimate_params(params)
  result_hash = { invoice_id: params[:invoice_id] }.compact

  whitelist_items(params[:items] || []).tap do |items|
    result_hash[:items] = items unless items.empty?
  end

  { root_name => result_hash }
end

#whitelist_items(items) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/lago/api/resources/credit_note.rb', line 31

def whitelist_items(items)
  items.each_with_object([]) do |item, result|
    filtered_item = (item || {}).slice(
      :fee_id, :amount_cents
    )

    result << filtered_item unless filtered_item.empty?
  end
end

#whitelist_params(params) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lago/api/resources/credit_note.rb', line 15

def whitelist_params(params)
  result_hash = {
    invoice_id: params[:invoice_id],
    reason: params[:reason],
    refund_status: params[:refund_status],
    credit_amount_cents: params[:credit_amount_cents],
    refund_amount_cents: params[:refund_amount_cents],
  }.compact

  whitelist_items(params[:items] || []).tap do |items|
    result_hash[:items] = items unless items.empty?
  end

  { root_name => result_hash }
end