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



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

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

#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



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

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_items(items) ⇒ Object



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

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
# 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]
  }.compact

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

  { root_name => result_hash }
end