starkinfra 0.0.1 → 0.1.0

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 +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
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative('../utils/resource')
4
+ require_relative('../utils/rest')
5
+ require_relative('../utils/checks')
6
+ require_relative('creditnote')
7
+
8
+ module StarkInfra
9
+ class CreditNote
10
+ # # CreditNote::Log object
11
+ #
12
+ # Every time a CreditNote entity is modified, a corresponding CreditNote::Log
13
+ # is generated for the entity. This log is never generated by the
14
+ # user.
15
+ #
16
+ # ## Attributes:
17
+ # - id [string]: unique id returned when the log is created. ex: '5656565656565656'
18
+ # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
19
+ # - type [string]: type of the CreditNote event which triggered the log creation. Options: 'canceled', 'created', 'expired', 'failed', 'refunded', 'registered', 'sending', 'sent', 'signed', 'success'
20
+ # - errors [list of strings]: list of errors linked to this CreditNote event.
21
+ # - note [CreditNote]: CreditNote entity to which the log refers to.
22
+ class Log < StarkInfra::Utils::Resource
23
+ attr_reader :id, :created, :type, :errors, :note
24
+ def initialize(id:, created:, type:, errors:, note:)
25
+ super(id)
26
+ @created = StarkInfra::Utils::Checks.check_datetime(created)
27
+ @type = type
28
+ @errors = errors
29
+ @note = note
30
+ end
31
+
32
+ # # Retrieve a specific Log
33
+ #
34
+ # Receive a single Log object previously created by the Stark Infra API by passing its id
35
+ #
36
+ # ## Parameters (required):
37
+ # - id [string]: object unique id. ex: '5656565656565656'
38
+ #
39
+ # ## Parameters (optional):
40
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
41
+ #
42
+ # ## Return:
43
+ # - Log object with updated attributes
44
+ def self.get(id, user: nil)
45
+ StarkInfra::Utils::Rest.get_id(id: id, user: user, **resource)
46
+ end
47
+
48
+ # # Retrieve Logs
49
+ #
50
+ # Receive a generator of Log objects previously created in the Stark Infra API
51
+ #
52
+ # ## Parameters (optional):
53
+ # - ids [list of strings, default nil]: Log ids to filter PixKey Logs. ex: ['5656565656565656']
54
+ # - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
55
+ # - after [Date or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
56
+ # - before [Date or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
57
+ # - types [list of strings, default nil]: filter retrieved objects by types. Options: 'canceled', 'created', 'expired', 'failed', 'refunded', 'registered', 'sending', 'sent', 'signed', 'success'
58
+ # - note_ids [list of strings, default nil]: list of CreditNote ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545']
59
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
60
+ #
61
+ # ## Return:
62
+ # - list of Log objects with updated attributes
63
+ def self.query(limit: nil, after: nil, before: nil, types: nil, note_ids: nil, user: nil)
64
+ after = StarkInfra::Utils::Checks.check_date(after)
65
+ before = StarkInfra::Utils::Checks.check_date(before)
66
+ StarkInfra::Utils::Rest.get_stream(
67
+ limit: limit,
68
+ after: after,
69
+ before: before,
70
+ types: types,
71
+ note_ids: note_ids,
72
+ user: user,
73
+ **resource
74
+ )
75
+ end
76
+
77
+ # # Retrieve paged Logs
78
+ #
79
+ # Receive a list of up to 100 Log objects previously created in the Stark Infra API and the cursor to the next page.
80
+ # Use this function instead of query if you want to manually page your notes.
81
+ #
82
+ # ## Parameters (optional):
83
+ # - cursor [string, default nil]: cursor returned on the previous page function call
84
+ # - limit [integer, default 100]: maximum number of objects to be retrieved. Max = 100. ex: 35
85
+ # - after [Date or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
86
+ # - before [Date or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
87
+ # - types [list of strings, default nil]: filter retrieved objects by types. Options: 'canceled', 'created', 'expired', 'failed', 'refunded', 'registered', 'sending', 'sent', 'signed', 'success'
88
+ # - note_ids [list of strings, default nil]: list of CreditNote ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545']
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 Log objects with updated attributes
93
+ # - Cursor to retrieve the next page of Log objects
94
+ def self.page(cursor: nil, limit: nil, after: nil, before: nil, types: nil, note_ids: nil, user: nil)
95
+ after = StarkInfra::Utils::Checks.check_date(after)
96
+ before = StarkInfra::Utils::Checks.check_date(before)
97
+ StarkInfra::Utils::Rest.get_page(
98
+ cursor: cursor,
99
+ limit: limit,
100
+ after: after,
101
+ before: before,
102
+ types: types,
103
+ note_ids: note_ids,
104
+ user: user,
105
+ **resource
106
+ )
107
+ end
108
+
109
+ def self.resource
110
+ note_maker = StarkInfra::CreditNote.resource[:resource_maker]
111
+ {
112
+ resource_name: 'CreditNoteLog',
113
+ resource_maker: proc { |json|
114
+ Log.new(
115
+ id: json['id'],
116
+ created: json['created'],
117
+ type: json['type'],
118
+ errors: json['errors'],
119
+ note: StarkInfra::Utils::API.from_api_json(note_maker, json['note'])
120
+ )
121
+ }
122
+ }
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative('../utils/resource')
4
+ require_relative('../utils/rest')
5
+ require_relative('../utils/checks')
6
+ require_relative('event')
7
+
8
+ module StarkInfra
9
+ class Event
10
+ # # Event::Attempt object
11
+ #
12
+ # When an Event delivery fails, an event attempt will be registered.
13
+ # It carries information meant to help you debug event reception issues.
14
+ #
15
+ # ## Attributes:
16
+ # - id [string]: unique id that identifies the delivery attempt. ex: '5656565656565656'
17
+ # - code [string]: delivery error code. ex: badHttpStatus, badConnection, timeout
18
+ # - message [string]: delivery error full description. ex: 'HTTP POST request returned status 404'
19
+ # - event_id [string]: ID of the Event whose delivery failed. ex: '4848484848484848'
20
+ # - webhook_id [string]: ID of the Webhook that triggered this event. ex: '5656565656565656'
21
+ # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
22
+ class Attempt < StarkInfra::Utils::Resource
23
+ attr_reader :id, :code, :message, :event_id, :webhook_id, :created
24
+ def initialize(id:, code:, message:, event_id:, webhook_id:, created:)
25
+ super(id)
26
+ @code = code
27
+ @message = message
28
+ @event_id = event_id
29
+ @webhook_id = webhook_id
30
+ @created = StarkInfra::Utils::Checks.check_datetime(created)
31
+ end
32
+
33
+ # # Retrieve a specific Event::Attempt
34
+ #
35
+ # Receive a single Event::Attempt object previously created by the Stark Infra API by its id
36
+ #
37
+ # ## Parameters (required):
38
+ # - id [string]: object unique id. ex: '5656565656565656'
39
+ #
40
+ # ## Parameters (optional):
41
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
42
+ #
43
+ # ## Return:
44
+ # - Event::Attempt object with updated attributes
45
+ def self.get(id, user: nil)
46
+ StarkInfra::Utils::Rest.get_id(id: id, user: user, **resource)
47
+ end
48
+
49
+ # # Retrieve Event::Attempts
50
+ #
51
+ # Receive a generator of Event::Attempt objects previously created in the Stark Infra API
52
+ #
53
+ # ## Parameters (optional):
54
+ # - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
55
+ # - after [Date or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
56
+ # - before [Date or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
57
+ # - event_ids [list of strings, default nil]: list of Event ids to filter attempts. ex: ['5656565656565656', '4545454545454545']
58
+ # - webhook_ids [list of strings, default nil]: list of Webhook ids to filter attempts. ex: ['5656565656565656', '4545454545454545']
59
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
60
+ #
61
+ # ## Return:
62
+ # - generator of Event::Attempt objects with updated attributes
63
+ def self.query(limit: nil, after: nil, before: nil, event_ids: nil, webhook_ids: nil, user: nil)
64
+ after = StarkInfra::Utils::Checks.check_date(after)
65
+ before = StarkInfra::Utils::Checks.check_date(before)
66
+ StarkInfra::Utils::Rest.get_stream(
67
+ limit: limit,
68
+ after: after,
69
+ before: before,
70
+ event_ids: event_ids,
71
+ webhook_ids: webhook_ids,
72
+ user: user,
73
+ **resource
74
+ )
75
+ end
76
+
77
+ # # Retrieve paged Event::Attempts
78
+ #
79
+ # Receive a list of up to 100 Attempt objects previously created in the Stark Infra API and the cursor to the next page.
80
+ # Use this function instead of query if you want to manually page your requests.
81
+ #
82
+ # ## Parameters (optional):
83
+ # - cursor [string, default nil]: cursor returned on the previous page function call
84
+ # - limit [integer, default 100]: maximum number of objects to be retrieved. Max = 100. ex: 35
85
+ # - after [Date or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
86
+ # - before [Date or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
87
+ # - event_ids [list of strings, default nil]: list of Event ids to filter attempts. ex: ['5656565656565656', '4545454545454545']
88
+ # - webhook_ids [list of strings, default nil]: list of Webhook ids to filter attempts. ex: ['5656565656565656', '4545454545454545']
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 Event::Attempt objects with updated attributes
93
+ # - cursor to retrieve the next page of Attempt objects
94
+ def self.page(cursor: nil, limit: nil, after: nil, before: nil, event_ids: nil, webhook_ids: nil, user: nil)
95
+ after = StarkInfra::Utils::Checks.check_date(after)
96
+ before = StarkInfra::Utils::Checks.check_date(before)
97
+ return StarkInfra::Utils::Rest.get_page(
98
+ cursor: cursor,
99
+ limit: limit,
100
+ after: after,
101
+ before: before,
102
+ event_ids: event_ids,
103
+ webhook_ids: webhook_ids,
104
+ user: user,
105
+ **resource
106
+ )
107
+ end
108
+
109
+ def self.resource
110
+ {
111
+ resource_name: 'EventAttempt',
112
+ resource_maker: proc { |json|
113
+ Attempt.new(
114
+ id: json['id'],
115
+ code: json['code'],
116
+ message: json['message'],
117
+ event_id: json['event_id'],
118
+ webhook_id: json['webhook_id'],
119
+ created: json['created']
120
+ )
121
+ }
122
+ }
123
+ end
124
+ end
125
+ end
126
+ end
data/lib/event/event.rb CHANGED
@@ -11,7 +11,6 @@ require_relative('../pixrequest/log')
11
11
  require_relative('../pixreversal/log')
12
12
  require_relative('../utils/parse')
13
13
 
14
-
15
14
  module StarkInfra
16
15
  # # Webhook Event object
17
16
  #
@@ -21,7 +20,7 @@ module StarkInfra
21
20
  #
22
21
  # ## Attributes:
23
22
  # - id [string]: unique id returned when the event is created. ex: '5656565656565656'
24
- # - log [Log]: a Log object from one the subscription services (TransferLog, InvoiceLog, BoletoLog, BoletoPaymentlog or UtilityPaymentLog)
23
+ # - log [Log]: a Log object from one the subscription services (PixRequestLog, PixReversalLog)
25
24
  # - created [DateTime]: creation datetime for the notification event. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
26
25
  # - is_delivered [bool]: true if the event has been successfully delivered to the user url. ex: False
27
26
  # - workspace_id [string]: ID of the Workspace that generated this event. Mostly used when multiple Workspaces have Webhooks registered to the same endpoint. ex: '4545454545454545'
@@ -36,35 +35,155 @@ module StarkInfra
36
35
  @subscription = subscription
37
36
 
38
37
  resource = {
38
+ 'credit-note': StarkInfra::CreditNote::Log.resource,
39
39
  'pix-request.in': StarkInfra::PixRequest::Log.resource,
40
40
  'pix-request.out': StarkInfra::PixRequest::Log.resource,
41
41
  'pix-reversal.in': StarkInfra::PixReversal::Log.resource,
42
42
  'pix-reversal.out': StarkInfra::PixReversal::Log.resource,
43
+ 'pix-key': StarkInfra::PixKey::Log.resource,
44
+ 'pix-claim': StarkInfra::PixClaim::Log.resource,
45
+ 'pix-infraction': StarkInfra::PixInfraction::Log.resource,
46
+ 'pix-chargeback': StarkInfra::PixChargeback::Log.resource,
47
+ 'issuing-card': StarkInfra::IssuingCard::Log.resource,
48
+ 'issuing-invoice': StarkInfra::IssuingInvoice::Log.resource,
49
+ 'issuing-purchase': StarkInfra::IssuingPurchase::Log.resource
43
50
  }[subscription.to_sym]
44
51
 
45
52
  @log = log
46
53
  @log = StarkInfra::Utils::API.from_api_json(resource[:resource_maker], log) unless resource.nil?
47
54
  end
48
55
 
56
+ # # Retrieve a specific notification Event
57
+ #
58
+ # Receive a single notification Event object previously created in the Stark Infra API by passing its id
59
+ #
60
+ # ## Parameters (required):
61
+ # - id [string]: object unique id. ex: '5656565656565656'
62
+ #
63
+ # ## Parameters (optional):
64
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
65
+ #
66
+ # ## Return:
67
+ # - Event object with updated attributes
68
+ def self.get(id, user: nil)
69
+ StarkInfra::Utils::Rest.get_id(id: id, user: user, **resource)
70
+ end
71
+
72
+ # # Retrieve notification Events
73
+ #
74
+ # Receive a generator of notification Event objects previously created in the Stark Infra API
75
+ #
76
+ # ## Parameters (optional):
77
+ # - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
78
+ # - after [Date or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
79
+ # - before [Date or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
80
+ # - is_delivered [bool, default nil]: bool to filter successfully delivered events. ex: true or false
81
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
82
+ #
83
+ # ## Return:
84
+ # - generator of Event objects with updated attributes
85
+ def self.query(limit: nil, after: nil, before: nil, is_delivered: nil, user: nil)
86
+ after = StarkInfra::Utils::Checks.check_date(after)
87
+ before = StarkInfra::Utils::Checks.check_date(before)
88
+ StarkInfra::Utils::Rest.get_stream(
89
+ user: user,
90
+ limit: limit,
91
+ after: after,
92
+ before: before,
93
+ is_delivered: is_delivered,
94
+ **resource
95
+ )
96
+ end
97
+
98
+ # # Retrieve paged Events
99
+ #
100
+ # Receive a list of up to 100 Event objects previously created in the Stark Infra API and the cursor to the next page.
101
+ # Use this function instead of query if you want to manually page your requests.
102
+ #
103
+ # ## Parameters (optional):
104
+ # - cursor [string, default nil]: cursor returned on the previous page function call
105
+ # - limit [integer, default 100]: maximum number of objects to be retrieved. Max = 100. ex: 35
106
+ # - after [Date or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
107
+ # - before [Date or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
108
+ # - is_delivered [bool, default nil]: bool to filter successfully delivered events. ex: true or false
109
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
110
+ #
111
+ # ## Return:
112
+ # - list of Event objects with updated attributes
113
+ # - cursor to retrieve the next page of Event objects
114
+ def self.page(cursor: nil, limit: nil, after: nil, before: nil, is_delivered: nil, user: nil)
115
+ after = StarkInfra::Utils::Checks.check_date(after)
116
+ before = StarkInfra::Utils::Checks.check_date(before)
117
+ StarkInfra::Utils::Rest.get_page(
118
+ cursor: cursor,
119
+ limit: limit,
120
+ after: after,
121
+ before: before,
122
+ is_delivered: is_delivered,
123
+ user: user,
124
+ **resource
125
+ )
126
+ end
127
+
128
+ # # Delete a notification Event
129
+ #
130
+ # Delete a notification Event entity previously created in the Stark Infra API by its ID
131
+ #
132
+ # ## Parameters (required):
133
+ # - id [string]: Event unique id. ex: '5656565656565656'
134
+ #
135
+ # ## Parameters (optional):
136
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
137
+ #
138
+ # ## Return:
139
+ # - deleted Event object
140
+ def self.delete(id, user: nil)
141
+ StarkInfra::Utils::Rest.delete_id(id: id, user: user, **resource)
142
+ end
143
+
144
+ # # Update notification Event entity
145
+ #
146
+ # Update a notification Event by its id.
147
+ # If is_delivered is true, the event will no longer be returned on queries with is_delivered=false.
148
+ #
149
+ # ## Parameters (required):
150
+ # - id [list of strings]: Event unique ids. ex: '5656565656565656'
151
+ # - is_delivered [bool]: If true and event hasn't been delivered already, event will be set as delivered. ex: true
152
+ #
153
+ # ## Parameters (optional):
154
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
155
+ #
156
+ # ## Return:
157
+ # - target Event with updated attributes
158
+ def self.update(id, is_delivered:, user: nil)
159
+ StarkInfra::Utils::Rest.patch_id(id: id, user: user, is_delivered: is_delivered, **resource)
160
+ end
161
+
49
162
  # # Create single notification Event from a content string
50
163
  #
51
164
  # Create a single Event object received from event listening at subscribed user endpoint.
52
165
  # If the provided digital signature does not check out with the StarkInfra public key, a
53
- # starkinfra.exception.InvalidSignatureException will be raised.
166
+ # StarkInfra.Error.InvalidSignatureError will be raised.
54
167
  #
55
168
  # ## Parameters (required):
56
169
  # - content [string]: response content from request received at user endpoint (not parsed)
57
170
  # - signature [string]: base-64 digital signature received at response header 'Digital-Signature'
58
171
  #
59
172
  # ## Parameters (optional):
60
- # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
173
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
61
174
  #
62
175
  # ## Return:
63
176
  # - Parsed Event object
64
177
  def self.parse(content:, signature:, user: nil)
65
- return StarkInfra::Utils::Parse.parse_and_verify(content: content, signature: signature, user: user, resource: resource, key: 'event')
178
+ StarkInfra::Utils::Parse.parse_and_verify(
179
+ content: content,
180
+ signature: signature,
181
+ user: user,
182
+ resource: resource,
183
+ key: 'event'
184
+ )
66
185
  end
67
-
186
+
68
187
  class << self
69
188
  private
70
189
 
@@ -86,4 +205,3 @@ module StarkInfra
86
205
  end
87
206
  end
88
207
  end
89
-
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative('../utils/resource')
4
+ require_relative('../utils/rest')
5
+ require_relative('../utils/checks')
6
+ require('json')
7
+
8
+ module StarkInfra
9
+ # # IssuingAuthorization object
10
+ #
11
+ # An IssuingAuthorization presents purchase data to be analysed and answered with an approval or a declination.
12
+ #
13
+ # ## Attributes (return-only):
14
+ # - id [string]: unique id returned when the IssuingAuthorization is created. ex: '5656565656565656'
15
+ # - end_to_end_id [string]: central bank's unique transaction ID. ex: "E79457883202101262140HHX553UPqeq"
16
+ # - amount [integer]: IssuingPurchase value in cents. Minimum = 0. ex: 1234 (= R$ 12.34)
17
+ # - tax [integer]: IOF amount taxed for international purchases. ex: 1234 (= R$ 12.34)
18
+ # - card_id [string]: unique id returned when IssuingCard is created. ex: "5656565656565656"
19
+ # - issuer_amount [integer]: issuer amount. ex: 1234 (= R$ 12.34)
20
+ # - issuer_currency_code [string]: issuer currency code. ex: "USD"
21
+ # - merchant_amount [integer]: merchant amount. ex: 1234 (= R$ 12.34)
22
+ # - merchant_currency_code [string]: merchant currency code. ex: "USD"
23
+ # - merchant_category_code [string]: merchant category code. ex: "fastFoodRestaurants"
24
+ # - merchant_country_code [string]: merchant country code. ex: "USA"
25
+ # - acquirer_id [string]: acquirer ID. ex: "5656565656565656"
26
+ # - merchant_id [string]: merchant ID. ex: "5656565656565656"
27
+ # - merchant_name [string]: merchant name. ex: "Google Cloud Platform"
28
+ # - merchant_fee [integer]: merchant fee charged. ex: 200 (= R$ 2.00)
29
+ # - wallet_id [string]: virtual wallet ID. ex: "googlePay"
30
+ # - method_code [string]: method code. ex: "chip", "token", "server", "manual", "magstripe" or "contactless"
31
+ # - score [float]: internal score calculated for the authenticity of the purchase. nil in case of insufficient data. ex: 7.6
32
+ # - is_partial_allowed [bool]: true if the the merchant allows partial purchases. ex: False
33
+ # - purpose [string]: purchase purpose. ex: "purchase"
34
+ # - card_tags [list of strings]: tags of the IssuingCard responsible for this purchase. ex: ["travel", "food"]
35
+ # - holder_tags [list of strings]: tags of the IssuingHolder responsible for this purchase. ex: ["technology", "john snow"]
36
+ class IssuingAuthorization < StarkInfra::Utils::Resource
37
+ attr_reader :id, :end_to_end_id, :amount, :tax, :card_id, :issuer_amount, :issuer_currency_code, :merchant_amount,
38
+ :merchant_currency_code, :merchant_category_code, :merchant_country_code, :acquirer_id, :merchant_id,
39
+ :merchant_name, :merchant_fee, :wallet_id, :method_code, :score, :is_partial_allowed, :purpose,
40
+ :card_tags, :holder_tags
41
+ def initialize(
42
+ id: nil, end_to_end_id: nil, amount: nil, tax: nil, card_id: nil, issuer_amount: nil,
43
+ issuer_currency_code: nil, merchant_amount: nil, merchant_currency_code: nil,
44
+ merchant_category_code: nil, merchant_country_code: nil, acquirer_id: nil, merchant_id: nil,
45
+ merchant_name: nil, merchant_fee: nil, wallet_id: nil, method_code: nil, score: nil,
46
+ is_partial_allowed: nil, purpose: nil, card_tags: nil, holder_tags: nil
47
+ )
48
+ super(id)
49
+ @end_to_end_id = end_to_end_id
50
+ @amount = amount
51
+ @tax = tax
52
+ @card_id = card_id
53
+ @issuer_amount = issuer_amount
54
+ @issuer_currency_code = issuer_currency_code
55
+ @merchant_amount = merchant_amount
56
+ @merchant_currency_code = merchant_currency_code
57
+ @merchant_category_code = merchant_category_code
58
+ @merchant_country_code = merchant_country_code
59
+ @acquirer_id = acquirer_id
60
+ @merchant_id = merchant_id
61
+ @merchant_name = merchant_name
62
+ @merchant_fee = merchant_fee
63
+ @wallet_id = wallet_id
64
+ @method_code = method_code
65
+ @score = score
66
+ @is_partial_allowed = is_partial_allowed
67
+ @purpose = purpose
68
+ @card_tags = card_tags
69
+ @holder_tags = holder_tags
70
+ end
71
+
72
+ # # Create single IssuingAuthorization from a content string
73
+ #
74
+ # Create a single IssuingAuthorization object received from IssuingAuthorization at the informed endpoint.
75
+ # If the provided digital signature does not check out with the StarkInfra public key, a
76
+ # StarkInfra.Error.InvalidSignatureError will be raised.
77
+ #
78
+ # ## Parameters (optional):
79
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
80
+ #
81
+ # ## Return:
82
+ # - IssuingAuthorization object with updated attributes
83
+ def self.parse(content:, signature:, user: nil)
84
+ StarkInfra::Utils::Parse.parse_and_verify(content: content, signature: signature, user: user, resource: resource)
85
+ end
86
+
87
+ # # Helps you respond IssuingAuthorization requests.
88
+ #
89
+ # ## Parameters (required):
90
+ # - status [string]: sub-issuer response to the authorization. ex: 'accepted' or 'denied'
91
+ #
92
+ # ## Parameters (optional):
93
+ # - amount [integer, default 0]: amount in cents that was authorized. ex: 1234 (= R$ 12.34)
94
+ # - reason [string, default '']: denial reason. ex: 'other'
95
+ # - tags [list of strings, default []]: tags to filter retrieved object. ex: ['tony', 'stark']
96
+ #
97
+ # ## Return:
98
+ # - Dumped JSON string that must be returned to us on the IssuingAuthorization request
99
+ def self.response(status:, amount: nil, reason: nil, tags: nil)
100
+ response_hash = {
101
+ "status": status,
102
+ "amount": amount || 0,
103
+ "reason": reason || '',
104
+ "tags": tags || []
105
+ }
106
+ response_hash.to_json
107
+ end
108
+
109
+ def self.resource
110
+ {
111
+ resource_name: 'IssuingAuthorization',
112
+ resource_maker: proc { |json|
113
+ IssuingAuthorization.new(
114
+ id: json['id'],
115
+ end_to_end_id: json['end_to_end_id'],
116
+ amount: json['amount'],
117
+ tax: json['tax'],
118
+ card_id: json['card_id'],
119
+ issuer_amount: json['issuer_amount'],
120
+ issuer_currency_code: json['issuer_currency_code'],
121
+ merchant_amount: json['merchant_amount'],
122
+ merchant_currency_code: json['merchant_currency_code'],
123
+ merchant_category_code: json['merchant_category_code'],
124
+ merchant_country_code: json['merchant_country_code'],
125
+ acquirer_id: json['acquirer_id'],
126
+ merchant_id: json['merchant_id'],
127
+ merchant_name: json['merchant_name'],
128
+ merchant_fee: json['merchant_fee'],
129
+ wallet_id: json['wallet_id'],
130
+ method_code: json['method_code'],
131
+ score: json['score'],
132
+ is_partial_allowed: json['is_partial_allowed'],
133
+ purpose: json['purpose'],
134
+ card_tags: json['card_tags'],
135
+ holder_tags: json['holder_tags']
136
+ )
137
+ }
138
+ }
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative('../utils/resource')
4
+ require_relative('../utils/rest')
5
+ require_relative('../utils/checks')
6
+
7
+ module StarkInfra
8
+ # # IssuingBalance object
9
+ #
10
+ # The IssuingBalance object displays the current issuing balance of the Workspace, which is the result of the sum of
11
+ # all transactions within this Workspace. The balance is never generated by the user, but it can be retrieved to see
12
+ # the available information.
13
+ #
14
+ # ## Attributes (return-only):
15
+ # - id [string]: unique id returned when IssuingBalance is created. ex: '5656565656565656'
16
+ # - amount [integer]: current balance amount of the Workspace in cents. ex: 200 (= R$ 2.00)
17
+ # - currency [string]: currency of the current Workspace. Expect others to be added eventually. ex: 'BRL'
18
+ # - updated [DateTime]: latest update datetime for the IssuingBalance. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
19
+ class IssuingBalance < StarkInfra::Utils::Resource
20
+ attr_reader :amount, :currency, :updated, :id
21
+ def initialize(amount: nil, currency: nil, updated: nil, id: nil)
22
+ super(id)
23
+ @amount = amount
24
+ @currency = currency
25
+ @updated = updated
26
+ end
27
+
28
+ # # Retrieve the IssuingBalance object
29
+ #
30
+ # Receive the IssuingBalance object linked to your Workspace in the Stark Infrq API
31
+ #
32
+ # ## Parameters (optional):
33
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkInfra.user was set before function call
34
+ #
35
+ # ## Return:
36
+ # - IssuingBalance object with updated attributes
37
+ def self.get(user: nil)
38
+ StarkInfra::Utils::Rest.get_stream(user: user, **resource).next
39
+ end
40
+
41
+ def self.resource
42
+ {
43
+ resource_name: 'IssuingBalance',
44
+ resource_maker: proc { |json|
45
+ IssuingBalance.new(
46
+ amount: json['amount'],
47
+ currency: json['currency'],
48
+ updated: json['updated'],
49
+ id: json['id']
50
+ )
51
+ }
52
+ }
53
+ end
54
+ end
55
+ end