Class: Lago::Api::Resources::CreditNote
- Inherits:
-
Base
- Object
- Base
- Lago::Api::Resources::CreditNote
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
Instance Method Details
#api_resource ⇒ Object
9
10
11
|
# File 'lib/lago/api/resources/credit_note.rb', line 9
def api_resource
'credit_notes'
end
|
106
107
108
109
110
111
|
# File 'lib/lago/api/resources/credit_note.rb', line 106
def delete_all_metadata(credit_note_id)
path = "/api/v1/credit_notes/#{credit_note_id}/metadata"
response = connection.destroy(path, identifier: nil)
response['metadata']
end
|
113
114
115
116
117
118
|
# File 'lib/lago/api/resources/credit_note.rb', line 113
def delete_metadata_key(credit_note_id, key)
path = "/api/v1/credit_notes/#{credit_note_id}/metadata/#{key}"
response = connection.destroy(path, identifier: nil)
response['metadata']
end
|
#download(credit_note_id) ⇒ Object
51
52
53
54
55
56
57
58
|
# File 'lib/lago/api/resources/credit_note.rb', line 51
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
71
72
73
74
75
76
77
78
|
# File 'lib/lago/api/resources/credit_note.rb', line 71
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
|
98
99
100
101
102
103
104
|
# File 'lib/lago/api/resources/credit_note.rb', line 98
def merge_metadata(credit_note_id, metadata)
path = "/api/v1/credit_notes/#{credit_note_id}/metadata"
payload = { metadata: whitelist_metadata(metadata) }
response = connection.patch(path, identifier: nil, body: payload)
response['metadata']
end
|
90
91
92
93
94
95
96
|
# File 'lib/lago/api/resources/credit_note.rb', line 90
def replace_metadata(credit_note_id, metadata)
path = "/api/v1/credit_notes/#{credit_note_id}/metadata"
payload = { metadata: whitelist_metadata(metadata) }
response = connection.post(payload, path)
response['metadata']
end
|
#root_name ⇒ Object
13
14
15
|
# File 'lib/lago/api/resources/credit_note.rb', line 13
def root_name
'credit_note'
end
|
#void(credit_note_id) ⇒ Object
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/lago/api/resources/credit_note.rb', line 60
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
80
81
82
83
84
85
86
87
88
|
# File 'lib/lago/api/resources/credit_note.rb', line 80
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
41
42
43
44
45
46
47
48
49
|
# File 'lib/lago/api/resources/credit_note.rb', line 41
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
|
37
38
39
|
# File 'lib/lago/api/resources/credit_note.rb', line 37
def whitelist_metadata(metadata)
metadata&.to_h&.transform_keys(&:to_s)&.transform_values { |v| v&.to_s }
end
|
#whitelist_params(params) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/lago/api/resources/credit_note.rb', line 17
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],
offset_amount_cents: params[:offset_amount_cents],
}.compact
whitelist_items(params[:items] || []).tap do |items|
result_hash[:items] = items unless items.empty?
end
metadata = whitelist_metadata(params[:metadata])
result_hash[:metadata] = metadata if metadata
{ root_name => result_hash }
end
|