starkinfra 0.0.1 → 0.1.0

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