starkinfra 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/lib/creditnote/creditnote.rb +561 -0
  3. data/lib/creditnote/log.rb +126 -0
  4. data/lib/event/attempt.rb +126 -0
  5. data/lib/event/event.rb +125 -7
  6. data/lib/issuingauthorization/issuingauthorization.rb +141 -0
  7. data/lib/issuingbalance/issuingbalance.rb +55 -0
  8. data/lib/issuingbin/issuingbin.rb +89 -0
  9. data/lib/issuingcard/issuingcard.rb +260 -0
  10. data/lib/issuingcard/log.rb +123 -0
  11. data/lib/issuingholder/issuingholder.rb +206 -0
  12. data/lib/issuingholder/log.rb +123 -0
  13. data/lib/issuinginvoice/issuinginvoice.rb +152 -0
  14. data/lib/issuinginvoice/log.rb +120 -0
  15. data/lib/issuingpurchase/issuingpurchase.rb +209 -0
  16. data/lib/issuingpurchase/log.rb +131 -0
  17. data/lib/issuingrule/issuingrule.rb +81 -0
  18. data/lib/issuingtransaction/issuingtransaction.rb +136 -0
  19. data/lib/issuingwithdrawal/issuingwithdrawal.rb +153 -0
  20. data/lib/pixbalance/pixbalance.rb +13 -13
  21. data/lib/pixchargeback/log.rb +129 -0
  22. data/lib/pixchargeback/pixchargeback.rb +225 -0
  23. data/lib/pixclaim/log.rb +135 -0
  24. data/lib/pixclaim/pixclaim.rb +225 -0
  25. data/lib/pixdirector/pixdirector.rb +76 -0
  26. data/lib/pixdomain/certificate.rb +30 -0
  27. data/lib/pixdomain/pixdomain.rb +58 -0
  28. data/lib/pixinfraction/log.rb +129 -0
  29. data/lib/pixinfraction/pixinfraction.rb +212 -0
  30. data/lib/pixkey/log.rb +128 -0
  31. data/lib/pixkey/pixkey.rb +239 -0
  32. data/lib/pixrequest/log.rb +16 -16
  33. data/lib/pixrequest/pixrequest.rb +66 -67
  34. data/lib/pixreversal/log.rb +13 -12
  35. data/lib/pixreversal/pixreversal.rb +72 -71
  36. data/lib/pixstatement/pixstatement.rb +22 -23
  37. data/lib/starkinfra.rb +32 -2
  38. data/lib/user/organization.rb +54 -0
  39. data/lib/user/project.rb +37 -0
  40. data/lib/user/user.rb +20 -0
  41. data/lib/utils/api.rb +10 -2
  42. data/lib/utils/bacenid.rb +19 -0
  43. data/lib/utils/checks.rb +2 -3
  44. data/lib/utils/endtoendid.rb +11 -0
  45. data/lib/utils/parse.rb +13 -13
  46. data/lib/utils/request.rb +1 -1
  47. data/lib/utils/rest.rb +7 -5
  48. data/lib/utils/returnid.rb +11 -0
  49. data/lib/webhook/webhook.rb +124 -0
  50. metadata +45 -24
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5fbb86892e3ea59c31a82ffcffac58475963325ffae8af8fce5214c3a63600c3
4
- data.tar.gz: 7919e49ab49dcbb1f1ea2487563e7ca25e9ff84ede33cb73d35132099d8799a5
3
+ metadata.gz: 0d456d927d988604b83c18adacb7d92fe77f299ba95e89c15ed5ae0e7a5cc4fc
4
+ data.tar.gz: 8ec44ecac1652b99596506d7a6234dde1ebfe94eb1299b70e27ae39809dde463
5
5
  SHA512:
6
- metadata.gz: bb74731c1f7fb3a4f94a26b4d3d55dc7a0cf6bf0edea08bd41ba98df965785d36415d13e5dbc07b410dc7ae1ca5e7cbdc2ac4bfff9f9bbd645f554185a58860a
7
- data.tar.gz: cabce3e0129abd42c15b38d521107da0cf21d1742c3b945f9ed3c0b8d845dc747882ed8356615b23d21a932f69a7e3956781393dc0ef841f10c43fd935477d8b
6
+ metadata.gz: 79ac2d80db5a48923b6d0c34fbecd148a9b1e9ea82f5d4d6dc46bec8011dc8959fcdffbb3a40d4661a64a6d0516a24a13b2cfa94e5d70bbe7aec38c8511f1024
7
+ data.tar.gz: '03180e37ba1c743016d8356a6851f7659e994b302ba55144747d9d9be216eaefad569779128f6f90f8a6ed7c5aece229d353e7ccfa683faa9c3aad8e8dd7d82c'
@@ -0,0 +1,561 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative('../utils/resource')
4
+ require_relative('../utils/rest')
5
+ require_relative('../utils/checks')
6
+ require_relative('../utils/parse')
7
+
8
+ module StarkInfra
9
+ # # CreditNote object
10
+ #
11
+ # CreditNotes are used to generate CCB contracts between you and your customers.
12
+ # When you initialize a CreditNote, the entity will not be automatically
13
+ # created in the Stark Infra API. The 'create' function sends the objects
14
+ # to the Stark Infra API and returns the list of created objects.
15
+ #
16
+ # ## Parameters (required):
17
+ # - template_id [string]: ID of the contract template on which the credit note will be based. ex: template_id='0123456789101112'
18
+ # - name [string]: credit receiver's full name. ex: name='Edward Stark'
19
+ # - tax_id [string]: credit receiver's tax ID (CPF or CNPJ). ex: '20.018.183/0001-80'
20
+ # - nominal_amount [integer]: amount in cents transferred to the credit receiver, before deductions. ex: nominal_amount=11234 (= R$ 112.34)
21
+ # - scheduled [DateTime, Date or string, default now]: date of transfer execution. ex: scheduled=DateTime.new(2020, 3, 10, 10, 30, 0, 0)
22
+ # - invoices [list of CreditNote::Invoice objects]: list of Invoice objects to be created and sent to the credit receiver. ex: invoices=[CreditNote::Invoice.new(), CreditNote::Invoice.new()]
23
+ # - payment [CreditNote::Transfer object]: payment entity to be created and sent to the credit receiver. ex: payment=CreditNote::Transfer.new()
24
+ # - signers [list of CreditNote::Signer objects]: signer's name, contact and delivery method for the signature request. ex: signers=[CreditNote::Signer.new(), CreditNote::Signer.new()]
25
+ # - external_id [string]: a string that must be unique among all your CreditNotes, used to avoid resource duplication. ex: 'my-internal-id-123456'
26
+ #
27
+ # ## Parameters (conditionally required):
28
+ # - payment_type [string]: payment type, inferred from the payment parameter if it is not a hash. ex: 'transfer'
29
+ #
30
+ # Parameters (optional):
31
+ # - rebate_amount [integer, default nil]: credit analysis fee deducted from lent amount. ex: rebate_amount=11234 (= R$ 112.34)
32
+ # - tags [list of strings, default nil]: list of strings for reference when searching for CreditNotes. ex: tags=['employees', 'monthly']
33
+ #
34
+ # Attributes (return-only):
35
+ # - id [string]: unique id returned when the CreditNote is created. ex: '5656565656565656'
36
+ # - amount [integer]: CreditNote value in cents. ex: 1234 (= R$ 12.34)
37
+ # - expiration [integer]: time interval in seconds between due date and expiration date. ex: 123456789
38
+ # - document_id [string]: ID of the signed document to execute this CreditNote. ex: '4545454545454545'
39
+ # - status [string]: current status of the CreditNote. ex: 'canceled', 'created', 'expired', 'failed', 'processing', 'signed', 'success'
40
+ # - transaction_ids [list of strings]: ledger transaction ids linked to this CreditNote. ex: ['19827356981273']
41
+ # - workspace_id [string]: ID of the Workspace that generated this CreditNote. ex: '4545454545454545'
42
+ # - tax_amount [integer]: tax amount included in the CreditNote. ex: 100
43
+ # - interest [float]: yearly effective interest rate of the credit note, in percentage. ex: 12.5
44
+ # - created [DateTime]: creation datetime for the CreditNote. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
45
+ # - updated [DateTime]: latest update datetime for the CreditNote. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
46
+ class CreditNote < StarkInfra::Utils::Resource
47
+ attr_reader :template_id, :name, :tax_id, :nominal_amount, :scheduled, :invoices, :payment, :signers, :external_id, :payment_type, :rebate_amount, :tags, :id, :amount, :expiration, :document_id, :status, :transaction_ids, :workspace_id, :tax_amount, :interest, :created, :updated
48
+ def initialize(
49
+ template_id:, name:, tax_id:, nominal_amount:, scheduled:, invoices:, payment:,
50
+ signers:, external_id:, payment_type: nil, rebate_amount: nil, tags: nil, id: nil, amount: nil,
51
+ expiration: nil, document_id: nil, status: nil, transaction_ids: nil, workspace_id: nil,
52
+ tax_amount: nil, interest: nil, created: nil, updated: nil
53
+ )
54
+ super(id)
55
+ @template_id = template_id
56
+ @name = name
57
+ @tax_id = tax_id
58
+ @nominal_amount = nominal_amount
59
+ @scheduled = scheduled
60
+ @invoices = CreditNote::Invoice.parse_invoices(invoices)
61
+ @signers = CreditNote::Signer.parse_signers(signers)
62
+ @external_id = external_id
63
+ @rebate_amount = rebate_amount
64
+ @tags = tags
65
+ @amount = amount
66
+ @expiration = expiration
67
+ @document_id = document_id
68
+ @status = status
69
+ @transaction_ids = transaction_ids
70
+ @workspace_id = workspace_id
71
+ @tax_amount = tax_amount
72
+ @interest = interest
73
+ @created = StarkInfra::Utils::Checks.check_datetime(created)
74
+ @updated = StarkInfra::Utils::Checks.check_datetime(updated)
75
+
76
+ payment_info = CreditNote.parse_payment(payment, payment_type)
77
+ @payment = payment_info['payment']
78
+ @payment_type = payment_info['payment_type']
79
+ end
80
+
81
+ # # Create CreditNotes
82
+ #
83
+ # Send a list of CreditNote objects for creation in the Stark Infra API
84
+ #
85
+ # ## Parameters (required):
86
+ # - notes [list of CreditNote objects]: list of CreditNote objects to be created in the API
87
+ #
88
+ # ## Parameters (optional):
89
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
90
+ #
91
+ # ## Return:
92
+ # - list of CreditNote objects with updated attributes
93
+ def self.create(notes, user: nil)
94
+ StarkInfra::Utils::Rest.post(entities: notes, user: user, **resource)
95
+ end
96
+
97
+ # # Retrieve a specific CreditNote
98
+ #
99
+ # Receive a single CreditNote object previously created in the Stark Infra API by passing its id
100
+ #
101
+ # ## Parameters (required):
102
+ # - id [string]: object unique id. ex: '5656565656565656'
103
+ #
104
+ # ## Parameters (optional):
105
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
106
+ #
107
+ # ## Return:
108
+ # - CreditNote object with updated attributes
109
+ def self.get(id, user: nil)
110
+ StarkInfra::Utils::Rest.get_id(id: id, user: user, **resource)
111
+ end
112
+
113
+ # # Retrieve CreditNotes
114
+ #
115
+ # Receive a generator of CreditNote objects previously created in the Stark Infra API
116
+ #
117
+ # ## Parameters (optional):
118
+ # - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
119
+ # - after [Date or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
120
+ # - before [Date or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
121
+ # - status [string, default nil]: filter for status of retrieved objects. ex: 'success' or 'failed'
122
+ # - tags [list of strings, default nil]: tags to filter retrieved objects. ex: ['tony', 'stark']
123
+ # - ids [list of strings, default nil]: list of ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545']
124
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
125
+ #
126
+ # ## Return:
127
+ # - generator of CreditNote objects with updated attributes
128
+ def self.query(limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, user: nil)
129
+ after = StarkInfra::Utils::Checks.check_date(after)
130
+ before = StarkInfra::Utils::Checks.check_date(before)
131
+ StarkInfra::Utils::Rest.get_stream(
132
+ limit: limit,
133
+ after: after,
134
+ before: before,
135
+ status: status,
136
+ tags: tags,
137
+ ids: ids,
138
+ user: user,
139
+ **resource
140
+ )
141
+ end
142
+
143
+ # # Retrieve paged CreditNotes
144
+ #
145
+ # Receive a list of up to 100 CreditNote objects previously created in the Stark infra API and the cursor to the next page.
146
+ # Use this function instead of query if you want to manually page your notes.
147
+ #
148
+ # ## Parameters (optional):
149
+ # - cursor [string, default nil]: cursor returned on the previous page function call
150
+ # - limit [integer, default 100]: maximum number of objects to be retrieved. Max = 100. ex: 35
151
+ # - after [Date or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
152
+ # - before [Date or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
153
+ # - status [string, default nil]: filter for status of retrieved objects. ex: 'paid' or 'registered'
154
+ # - tags [list of strings, default nil]: tags to filter retrieved objects. ex: ['tony', 'stark']
155
+ # - ids [list of strings, default nil]: list of ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545']
156
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
157
+ #
158
+ # ## Return:
159
+ # - list of CreditNote objects with updated attributes
160
+ # - cursor to retrieve the next page of CreditNote objects
161
+ def self.page(cursor: nil, limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, user: nil)
162
+ after = StarkInfra::Utils::Checks.check_date(after)
163
+ before = StarkInfra::Utils::Checks.check_date(before)
164
+ StarkInfra::Utils::Rest.get_page(
165
+ cursor: cursor,
166
+ limit: limit,
167
+ after: after,
168
+ before: before,
169
+ status: status,
170
+ tags: tags,
171
+ ids: ids,
172
+ user: user,
173
+ **resource
174
+ )
175
+ end
176
+
177
+ # # Cancel a specific CreditNote
178
+ #
179
+ # Cancel a single CreditNote object previously created in the Stark Infra API by passing its id
180
+ #
181
+ # ## Parameters (required):
182
+ # - id [string]: object unique id. ex: '5656565656565656'
183
+ #
184
+ # ## Parameters (optional):
185
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
186
+ #
187
+ # ## Return:
188
+ # - canceled CreditNote object with updated attributes
189
+ def self.cancel(id, user: nil)
190
+ StarkInfra::Utils::Rest.delete_id(id: id, user: user, **resource)
191
+ end
192
+
193
+ def self.parse_payment(payment, payment_type)
194
+ resource_map = { 'transfer' => Transfer.resource[:resource_maker] }
195
+ if payment.is_a?(Hash)
196
+ begin
197
+ parsed_payment = StarkInfra::Utils::API.from_api_json(resource_map[payment_type], payment)
198
+ return { 'payment' => parsed_payment, 'payment_type' => payment_type }
199
+ rescue StandardError
200
+ return { 'payment' => payment, 'payment_type' => payment_type }
201
+ end
202
+ end
203
+
204
+ return { 'payment' => payment, 'payment_type' => payment_type} if payment_type
205
+ if payment.class == StarkInfra::Transfer
206
+ return { 'payment' => payment, 'payment_type' => 'transfer'}
207
+ end
208
+ raise 'payment must be either ' + 'a dictionary, ' \
209
+ 'a CreditNote.Transfer, but not a ' + payment.class.to_s
210
+ end
211
+
212
+ def self.resource
213
+ {
214
+ resource_name: 'CreditNote',
215
+ resource_maker: proc { |json|
216
+ CreditNote.new(
217
+ id: json['id'],
218
+ template_id: json['template_id'],
219
+ name: json['name'],
220
+ tax_id: json['tax_id'],
221
+ nominal_amount: json['nominal_amount'],
222
+ scheduled: json['scheduled'],
223
+ invoices: json['invoices'],
224
+ payment: json['payment'],
225
+ signers: json['signers'],
226
+ external_id: json['external_id'],
227
+ payment_type: json['payment_type'],
228
+ rebate_amount: json['rebate_amount'],
229
+ tags: json['tags'],
230
+ amount: json['amount'],
231
+ expiration: json['expiration'],
232
+ document_id: json['document_id'],
233
+ status: json['status'],
234
+ transaction_ids: json['transaction_ids'],
235
+ workspace_id: json['workspace_id'],
236
+ tax_amount: json['tax_amount'],
237
+ interest: json['interest'],
238
+ created: json['created'],
239
+ updated: json['updated']
240
+ )
241
+ }
242
+ }
243
+ end
244
+
245
+ # # CreditNote::Transfer object
246
+ #
247
+ # Transfer sent to the credit receiver after the contract is signed.
248
+ #
249
+ # ## Parameters (required):
250
+ # - name [string]: receiver full name. ex: 'Anthony Edward Stark'
251
+ # - tax_id [string]: receiver tax ID (CPF or CNPJ) with or without formatting. ex: '01234567890' or '20.018.183/0001-80'
252
+ # - bank_code [string]: code of the receiver bank institution in Brazil. ex: '20018183'
253
+ # - branch_code [string]: receiver bank account branch. Use '-' in case there is a verifier digit. ex: '1357-9'
254
+ # - account_number [string]: receiver bank account number. Use '-' before the verifier digit. ex: '876543-2'
255
+ #
256
+ # ## Parameters (optional):
257
+ # - account_type [string, default 'checking']: Receiver bank account type. This parameter only has effect on Pix Transfers. ex: 'checking', 'savings', 'salary' or 'payment'
258
+ # - tags [list of strings, default nil]: list of strings for reference when searching for transfers. ex: ['employees', 'monthly']
259
+ #
260
+ # ## Attributes (return-only):
261
+ # - amount [integer]: amount in cents to be transferred. ex: 1234 (= R$ 12.34)
262
+ # - external_id [string]: url safe string that must be unique among all your transfers. Duplicated external_ids will cause failures. By default, this parameter will block any transfer that repeats amount and receiver information on the same date. ex: 'my-internal-id-123456'
263
+ # - scheduled [DateTime, Date or string]: date or datetime when the transfer will be processed. May be pushed to next business day if necessary. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
264
+ # - description [string]: optional description to override default description to be shown in the bank statement. ex: 'Payment for service #1234'
265
+ # - id [string]: unique id returned when the transfer is created. ex: '5656565656565656'
266
+ # - fee [integer]: fee charged when the Transfer is processed. ex: 200 (= R$ 2.00)
267
+ # - status [string]: current transfer status. ex: 'success' or 'failed'
268
+ # - transaction_ids [list of strings]: ledger Transaction IDs linked to this Transfer (if there are two, the second is the chargeback). ex: ['19827356981273']
269
+ # - created [DateTime]: creation datetime for the transfer. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
270
+ # - updated [DateTime]: latest update datetime for the transfer. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
271
+ class Transfer < StarkInfra::Utils::Resource
272
+ attr_reader :tax_id, :name, :bank_code, :branch_code, :account_number, :amount, :account_type, :external_id, :scheduled, :description, :tags, :fee, :status, :created, :updated, :transaction_ids
273
+ def initialize(
274
+ name:, tax_id:, bank_code:, branch_code:, account_number:, account_type: nil,
275
+ tags: nil, id: nil, amount: nil, external_id: nil, scheduled: nil,
276
+ description: nil, fee: nil, status: nil, created: nil, updated: nil,
277
+ transaction_ids: nil
278
+ )
279
+ super(id)
280
+ @name = name
281
+ @tax_id = tax_id
282
+ @bank_code = bank_code
283
+ @branch_code = branch_code
284
+ @account_number = account_number
285
+ @account_type = account_type
286
+ @tags = tags
287
+ @amount = amount
288
+ @external_id = external_id
289
+ @scheduled = scheduled
290
+ @description = description
291
+ @fee = fee
292
+ @status = status
293
+ @transaction_ids = transaction_ids
294
+ @created = StarkInfra::Utils::Checks.check_datetime(created)
295
+ @updated = StarkInfra::Utils::Checks.check_datetime(updated)
296
+ end
297
+
298
+ def self.resource
299
+ {
300
+ resource_name: 'Transfer',
301
+ resource_maker: proc { |json|
302
+ Transfer.new(
303
+ id: json['id'],
304
+ tax_id: json['tax_id'],
305
+ name: json['name'],
306
+ bank_code: json['bank_code'],
307
+ branch_code: json['branch_code'],
308
+ account_number: json['account_number'],
309
+ amount: json['amount'],
310
+ account_type: json['account_type'],
311
+ external_id: json['external_id'],
312
+ scheduled: json['scheduled'],
313
+ description: json['description'],
314
+ tags: json['tags'],
315
+ fee: json['fee'],
316
+ status: json['status'],
317
+ created: json['created'],
318
+ updated: json['updated'],
319
+ transaction_ids: json['transaction_ids']
320
+ )
321
+ }
322
+ }
323
+ end
324
+ end
325
+
326
+ # # CreditNote::Signer object
327
+ #
328
+ # The Signer object stores the CreditNote signer's information.
329
+ #
330
+ # ## Parameters (required):
331
+ # - name [string]: signer's name. ex: 'Tony Stark'
332
+ # - contact [string]: signer's contact information. ex: 'tony@starkindustries.com'
333
+ # - method [string]: delivery method for the contract. ex: 'link'
334
+ class Signer < StarkInfra::Utils::SubResource
335
+ attr_reader :name, :contact, :method
336
+ def initialize(name:, contact:, method:)
337
+ @name = name
338
+ @contact = contact
339
+ @method = method
340
+ end
341
+
342
+ def self.parse_signers(signers)
343
+ return signers if signers.nil?
344
+
345
+ parsed_signers = []
346
+ signers.each do |signer|
347
+ unless signer.is_a? Signer
348
+ signer = StarkInfra::Utils::API.from_api_json(resource[:resource_maker], signer)
349
+ end
350
+ parsed_signers << signer
351
+ end
352
+ parsed_signers
353
+ end
354
+
355
+ def self.resource
356
+ {
357
+ resource_name: 'Signer',
358
+ resource_maker: proc { |json|
359
+ Signer.new(
360
+ name: json['name'],
361
+ contact: json['contact'],
362
+ method: json['method']
363
+ )
364
+ }
365
+ }
366
+ end
367
+ end
368
+
369
+ # CreditNote::Invoice object
370
+ # Invoice object to be issued after contract signature and paid by the credit receiver.
371
+ #
372
+ # ## Parameters (required):
373
+ # - amount [integer]: Invoice value in cents. Minimum = 1 (any value will be accepted). ex: 1234 (= R$ 12.34)
374
+ #
375
+ # ## Parameters (optional):
376
+ # - due [DateTime, Date or string, default now + 2 days]: Invoice due date in UTC ISO format. ex: '2020-10-28T17:59:26.249976+00:00' for immediate invoices and '2020-10-28' for scheduled invoices
377
+ # - expiration [integer, default 5097600 (59 days)]: time interval in seconds between due date and expiration date. ex: 123456789
378
+ # - fine [float, default 2.0]: Invoice fine for overdue payment in %. ex: 2.5
379
+ # - interest [float, default 1.0]: Invoice monthly interest in overdue payment in %. ex: 5.2
380
+ # - tags [list of strings, default nil]: list of strings for tagging
381
+ # - descriptions [list of CreditNote::Invoice::Description objects, default nil]: list Description objects. ex: [CreditNote::Invoice::Description.new()]
382
+ #
383
+ # ## Attributes (return-only):
384
+ # - id [string]: unique id returned when Invoice is created. ex: '5656565656565656'
385
+ # - name [string]: payer name. ex: 'Iron Bank S.A.'
386
+ # - tax_id [string]: payer tax ID (CPF or CNPJ) with or without formatting. ex: '01234567890' or '20.018.183/0001-80'
387
+ # - pdf [string]: public Invoice PDF URL. ex: 'https://invoice.starkbank.com/pdf/d454fa4e524441c1b0c1a729457ed9d8'
388
+ # - link [string]: public Invoice webpage URL. ex: 'https://my-workspace.sandbox.starkbank.com/invoicelink/d454fa4e524441c1b0c1a729457ed9d8'
389
+ # - nominal_amount [integer]: Invoice emission value in cents (will change if invoice is updated, but not if it's paid). ex: 400000
390
+ # - fine_amount [integer]: Invoice fine value calculated over nominal_amount. ex: 20000
391
+ # - interest_amount [integer]: Invoice interest value calculated over nominal_amount. ex: 10000
392
+ # - discount_amount [integer]: Invoice discount value calculated over nominal_amount. ex: 3000
393
+ # - discounts [list of CreditNote::Invoice::Discount objects]: list of Discount objects. ex: [CreditNote::Invoice::Discount.new()]
394
+ # - brcode [string]: BR Code for the Invoice payment. ex: '00020101021226800014br.gov.bcb.pix2558invoice.starkbank.com/f5333103-3279-4db2-8389-5efe335ba93d5204000053039865802BR5913Arya Stark6009Sao Paulo6220051656565656565656566304A9A0'
395
+ # - status [string]: current Invoice status. ex: 'registered' or 'paid'
396
+ # - fee [integer]: fee charged by this Invoice. ex: 200 (= R$ 2.00)
397
+ # - transaction_ids [list of strings]: ledger transaction ids linked to this Invoice (if there are more than one, all but the first are reversals or failed reversal chargebacks). ex: ['19827356981273']
398
+ # - created [DateTime]: creation datetime for the Invoice. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
399
+ # - updated [DateTime]: latest update datetime for the Invoice. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
400
+ class Invoice < StarkInfra::Utils::Resource
401
+ attr_reader :id, :amount, :due, :expiration, :fine, :interest, :tags, :descriptions, :name, :tax_id, :pdf, :link, :nominal_amount, :fine_amount, :interest_amount, :discount_amount, :discounts, :brcode, :status, :fee, :transaction_ids, :created, :updated
402
+ def initialize(
403
+ amount:, due: nil, expiration: nil, fine: nil, interest: nil, tags: nil, descriptions: nil,
404
+ id: nil, name: nil, tax_id: nil, pdf: nil, link: nil, nominal_amount: nil, fine_amount: nil,
405
+ interest_amount: nil, discount_amount: nil, discounts: nil, brcode: nil, status: nil, fee: nil,
406
+ transaction_ids: nil, created: nil, updated: nil
407
+ )
408
+ super(id)
409
+ @tax_id = tax_id
410
+ @amount = amount
411
+ @due = due
412
+ @expiration = expiration
413
+ @fine = fine
414
+ @interest = interest
415
+ @tags = tags
416
+ @descriptions = Description.parse_descriptions(descriptions)
417
+ @name = name
418
+ @tax_id = tax_id
419
+ @pdf = pdf
420
+ @link = link
421
+ @nominal_amount = nominal_amount
422
+ @fine_amount = fine_amount
423
+ @interest_amount = interest_amount
424
+ @discount_amount = discount_amount
425
+ @discounts = Discount.parse_discounts(discounts)
426
+ @brcode = brcode
427
+ @status = status
428
+ @fee = fee
429
+ @transaction_ids = transaction_ids
430
+ @created = StarkInfra::Utils::Checks.check_datetime(created)
431
+ @updated = StarkInfra::Utils::Checks.check_datetime(updated)
432
+ end
433
+
434
+ def self.parse_invoices(invoices)
435
+ return invoices if invoices.nil?
436
+ parsed_invoices = []
437
+ invoices.each do |invoice|
438
+ unless invoice.is_a? Invoice
439
+ invoice = StarkInfra::Utils::API.from_api_json(resource[:resource_maker], invoice)
440
+ end
441
+ parsed_invoices << invoice
442
+ end
443
+ parsed_invoices
444
+ end
445
+
446
+ def self.resource
447
+ {
448
+ resource_name: 'Invoice',
449
+ resource_maker: proc { |json|
450
+ Invoice.new(
451
+ id: json['id'],
452
+ amount: json['amount'],
453
+ due: json['due'],
454
+ expiration: json['expiration'],
455
+ fine: json['fine'],
456
+ interest: json['interest'],
457
+ tags: json['tags'],
458
+ descriptions: json['descriptions'],
459
+ name: json['name'],
460
+ tax_id: json['tax_id'],
461
+ pdf: json['pdf'],
462
+ link: json['link'],
463
+ nominal_amount: json['nominal_amount'],
464
+ fine_amount: json['fine_amount'],
465
+ interest_amount: json['interest_amount'],
466
+ discount_amount: json['discount_amount'],
467
+ discounts: json['discounts'],
468
+ brcode: json['brcode'],
469
+ status: json['status'],
470
+ fee: json['fee'],
471
+ transaction_ids: json['transaction_ids'],
472
+ created: json['created'],
473
+ updated: json['updated']
474
+ )
475
+ }
476
+ }
477
+ end
478
+
479
+ # # CreditNote::Invoice::Discount object
480
+ #
481
+ # Invoice discount information.
482
+ #
483
+ # ## Parameters (required):
484
+ # - percentage [float]: percentage of discount applied until specified due date
485
+ # - due [DateTime or string]: due datetime for the discount
486
+ class Discount < StarkInfra::Utils::SubResource
487
+ attr_reader :percentage, :due
488
+ def initialize(percentage:, due:)
489
+ @percentage = percentage
490
+ @due = due
491
+ end
492
+
493
+ def self.parse_discounts(discounts)
494
+ return discounts if discounts.nil?
495
+
496
+ parsed_discounts = []
497
+ discounts.each do |discount|
498
+ unless discount.is_a? Discount
499
+ discount = StarkInfra::Utils::API.from_api_json(resource[:resource_maker], discount)
500
+ end
501
+ parsed_discounts << discount
502
+ end
503
+ parsed_discounts
504
+ end
505
+
506
+ def self.resource
507
+ {
508
+ resource_name: 'Discount',
509
+ resource_maker: proc { |json|
510
+ Discount.new(
511
+ percentage: json['percentage'],
512
+ due: json['due']
513
+ )
514
+ }
515
+ }
516
+ end
517
+ end
518
+
519
+ # # CreditNote::Invoice::Description object
520
+ #
521
+ # Invoice description information.
522
+ #
523
+ # ## Parameters (required):
524
+ # - key [string]: Description for the value. ex: 'Taxes'
525
+ #
526
+ # ## Parameters (optional):
527
+ # - value [string]: amount related to the described key. ex: 'R$100,00'
528
+ class Description < StarkInfra::Utils::SubResource
529
+ attr_reader :percentage, :due
530
+ def initialize(key:, value:)
531
+ @key = key
532
+ @value = value
533
+ end
534
+
535
+ def self.parse_descriptions(descriptions)
536
+ return descriptions if descriptions.nil?
537
+ parsed_descriptions = []
538
+ descriptions.each do |description|
539
+ unless description.is_a? Description
540
+ description = StarkInfra::Utils::API.from_api_json(resource[:resource_maker], description)
541
+ end
542
+ parsed_descriptions << description
543
+ end
544
+ parsed_descriptions
545
+ end
546
+
547
+ def self.resource
548
+ {
549
+ resource_name: 'Description',
550
+ resource_maker: proc { |json|
551
+ Description.new(
552
+ key: json['key'],
553
+ value: json['value']
554
+ )
555
+ }
556
+ }
557
+ end
558
+ end
559
+ end
560
+ end
561
+ end