starkbank 2.7.0 → 2.9.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.
@@ -0,0 +1,265 @@
1
+ # frozen_string_literal: true
2
+
3
+ require('starkcore')
4
+ require_relative('../utils/rest')
5
+
6
+ module StarkBank
7
+ # # CorporatePurchase object
8
+ #
9
+ # Displays the CorporatePurchase objects created in your Workspace.
10
+ #
11
+ # ## Attributes (return-only):
12
+ # - id [string]: unique id returned when CorporatePurchase is created. ex: "5656565656565656"
13
+ # - holder_id [string]: card holder unique id. ex: "5656565656565656"
14
+ # - holder_name [string]: card holder name. ex: "Tony Stark"
15
+ # - center_id [string]: target cost center ID. ex: "5656565656565656"
16
+ # - card_id [string]: unique id returned when CorporateCard is created. ex: "5656565656565656"
17
+ # - card_ending [string]: last 4 digits of the card number. ex: "1234"
18
+ # - description [string]: purchase descriptions. ex: "my_description"
19
+ # - amount [integer]: CorporatePurchase value in cents. Minimum = 0. ex: 1234 (= R$ 12.34)
20
+ # - tax [integer]: IOF amount taxed for international purchases. ex: 1234 (= R$ 12.34)
21
+ # - corporate_amount [integer]: corporate amount. ex: 1234 (= R$ 12.34)
22
+ # - corporate_currency_code [string]: corporate currency code. ex: "USD"
23
+ # - corporate_currency_symbol [string]: corporate currency symbol. ex: "$"
24
+ # - merchant_amount [integer]: merchant amount. ex: 1234 (= R$ 12.34)
25
+ # - merchant_currency_code [string]: merchant currency code. ex: "USD"
26
+ # - merchant_currency_symbol [string]: merchant currency symbol. ex: "$"
27
+ # - merchant_category_code [string]: merchant category code. ex: "fastFoodRestaurants"
28
+ # - merchant_category_type [string]: merchant category type. ex: "health"
29
+ # - merchant_country_code [string]: merchant country code. ex: "USA"
30
+ # - merchant_name [string]: merchant name. ex: "Google Cloud Platform"
31
+ # - merchant_display_name [string]: merchant name. ex: "Google Cloud Platform"
32
+ # - merchant_display_url [string]: public merchant icon (png image). ex: "https://sandbox.api.starkbank.com/v2/corporate-icon/merchant/ifood.png"
33
+ # - merchant_fee [integer]: fee charged by the merchant to cover specific costs, such as ATM withdrawal logistics, etc. ex: 200 (= R$ 2.00)
34
+ # - method_code [string]: method code. Options: "chip", "token", "server", "manual", "magstripe" or "contactless"
35
+ # - tags [list of strings]: list of strings for tagging returned by the sub-issuer during the authorization. ex: ["travel", "food"]
36
+ # - corporate_transaction_ids [list of strings]: ledger transaction ids linked to this Purchase
37
+ # - status [string]: current CorporateCard status. Options: "approved", "canceled", "denied", "confirmed", "voided"
38
+ # - updated [DateTime]: latest update datetime for the CorporatePurchase. ex: DateTime(2020, 3, 10, 10, 30, 0, 0)
39
+ # - created [DateTime]: creation datetime for the CorporatePurchase. ex: DateTime(2020, 3, 10, 10, 30, 0, 0)
40
+ class CorporatePurchase < StarkCore::Utils::Resource
41
+ attr_reader :id, :holder_id, :holder_name, :center_id, :card_id, :card_ending, :description, :amount, :tax, :corporate_amount,
42
+ :corporate_currency_code, :corporate_currency_symbol, :merchant_amount, :merchant_currency_code, :merchant_currency_symbol,
43
+ :merchant_category_code, :merchant_category_type, :merchant_country_code, :merchant_name, :merchant_display_name,
44
+ :merchant_display_url, :merchant_fee, :method_code, :tags, :corporate_transaction_ids, :status, :updated, :created
45
+ def initialize(
46
+ id: nil, holder_id: nil, holder_name: nil, center_id: nil, card_id: nil, card_ending: nil, description: nil, amount: nil, tax: nil, corporate_amount: nil,
47
+ corporate_currency_code: nil, corporate_currency_symbol: nil, merchant_amount: nil, merchant_currency_code: nil, merchant_currency_symbol: nil,
48
+ merchant_category_code: nil, merchant_category_type: nil, merchant_country_code: nil, merchant_name: nil, merchant_display_name: nil,
49
+ merchant_display_url: nil, merchant_fee: nil, method_code: nil, tags: nil, corporate_transaction_ids: nil, status: nil, updated: nil, created: nil
50
+ )
51
+ super(id)
52
+ @holder_id = holder_id
53
+ @holder_name = holder_name
54
+ @center_id = center_id
55
+ @card_id = card_id
56
+ @card_ending = card_ending
57
+ @description = description
58
+ @amount = amount
59
+ @tax = tax
60
+ @corporate_amount = corporate_amount
61
+ @corporate_currency_code = corporate_currency_code
62
+ @corporate_currency_symbol = corporate_currency_symbol
63
+ @merchant_amount = merchant_amount
64
+ @merchant_currency_code = merchant_currency_code
65
+ @merchant_currency_symbol = merchant_currency_symbol
66
+ @merchant_category_code = merchant_category_code
67
+ @merchant_category_type = merchant_category_type
68
+ @merchant_country_code = merchant_country_code
69
+ @merchant_name = merchant_name
70
+ @merchant_display_name = merchant_display_name
71
+ @merchant_display_url = merchant_display_url
72
+ @merchant_fee = merchant_fee
73
+ @method_code = method_code
74
+ @tags = tags
75
+ @corporate_transaction_ids = corporate_transaction_ids
76
+ @status = status
77
+ @updated = updated
78
+ @created = created
79
+
80
+ end
81
+
82
+ # # Retrieve a specific CorporatePurchase
83
+ #
84
+ # Receive a single CorporatePurchase object previously created in the Stark Bank API by its id
85
+ #
86
+ # ## Parameters (required):
87
+ # - id [string]: object unique id. ex: '5656565656565656'
88
+ #
89
+ # ## Parameters (optional):
90
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkBank.user was set before function call
91
+ #
92
+ # ## Return:
93
+ # - CorporatePurchase object with updated attributes
94
+ def self.get(id, user: nil)
95
+ StarkBank::Utils::Rest.get_id(id: id, user: user, **resource)
96
+ end
97
+
98
+ # # Retrieve CorporatePurchases
99
+ #
100
+ # Receive a generator of CorporatePurchases objects previously created in the Stark Bank API
101
+ #
102
+ # ## Parameters (optional):
103
+ # - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
104
+ # - after [Date or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 09)
105
+ # - before [Date or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
106
+ # - merchant_category_types [list of strings, default nil]: merchant category type. ex: "health"
107
+ # - holder_ids [list of strings, default nil]: card holder IDs. ex: ["5656565656565656", "4545454545454545"]
108
+ # - card_ids [list of strings, default nil]: card IDs. ex: ["5656565656565656", "4545454545454545"]
109
+ # - status [list of strings, default nil]: filter for status of retrieved objects. ex: ['approved', 'canceled', 'denied', 'confirmed', 'voided']
110
+ # - ids [list of strings, default nil]: purchase IDs. ex: ['5656565656565656', '4545454545454545']
111
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if starkbank.user was set before function call
112
+ #
113
+ # ## Return:
114
+ # - generator of CorporatePurchases objects with updated attributes
115
+ def self.query(ids: nil, limit: nil, after: nil, before: nil, merchant_category_types: nil, holder_ids: nil, card_ids: nil,
116
+ status: nil, user: nil)
117
+ after = StarkCore::Utils::Checks.check_date(after)
118
+ before = StarkCore::Utils::Checks.check_date(before)
119
+ StarkBank::Utils::Rest.get_stream(
120
+ ids: ids,
121
+ limit: limit,
122
+ after: after,
123
+ before: before,
124
+ merchant_category_types: merchant_category_types,
125
+ holder_ids: holder_ids,
126
+ card_ids: card_ids,
127
+ status: status,
128
+ user: user,
129
+ **resource
130
+ )
131
+ end
132
+
133
+ # # Retrieve paged CorporatePurchases
134
+ #
135
+ # Receive a list of up to 100 CorporatePurchases objects previously created in the Stark Bank API and the cursor
136
+ # to the next page. Use this function instead of query if you want to manually page your invoices.
137
+ #
138
+ # ## Parameters (optional):
139
+ # - cursor [string, default nil]: cursor returned on the previous page function call.
140
+ # - limit [integer, default 100]: maximum number of objects to be retrieved. Max = 100. ex: 35
141
+ # - after [Date or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
142
+ # - before [Date or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
143
+ # - merchant_category_types [list of strings, default nil]: merchant category type. ex: "health"
144
+ # - holder_ids [list of strings, default nil]: card holder IDs. ex: ["5656565656565656", "4545454545454545"]
145
+ # - card_ids [list of strings, default nil]: card IDs. ex: ["5656565656565656", "4545454545454545"]
146
+ # - status [list of strings, default nil]: filter for status of retrieved objects. ex: ['approved', 'canceled', 'denied', 'confirmed', 'voided']
147
+ # - ids [list of strings, default nil]: purchase IDs. ex: ['5656565656565656', '4545454545454545']
148
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if starkbank.user was set before function call
149
+ #
150
+ # ## Return:
151
+ # - list of CorporatePurchases objects with updated attributes
152
+ # - cursor to retrieve the next page of CorporatePurchases objects
153
+ def self.page(cursor: nil, ids: nil, limit: nil, after: nil, before: nil, merchant_category_types: nil, holder_ids: nil,
154
+ card_ids: nil, status: nil, user: nil)
155
+ after = StarkCore::Utils::Checks.check_date(after)
156
+ before = StarkCore::Utils::Checks.check_date(before)
157
+ StarkBank::Utils::Rest.get_page(
158
+ cursor: cursor,
159
+ ids: ids,
160
+ limit: limit,
161
+ after: after,
162
+ before: before,
163
+ merchant_category_types: merchant_category_types,
164
+ holder_ids: holder_ids,
165
+ card_ids: card_ids,
166
+ status: status,
167
+ user: user,
168
+ **resource
169
+ )
170
+ end
171
+
172
+ # # Create a single verified CorporatePurchase authorization request from a content string
173
+ #
174
+ # Use this method to parse and verify the authenticity of the authorization request received at the informed endpoint.
175
+ # Authorization requests are posted to your registered endpoint whenever CorporatePurchases are received.
176
+ # They present CorporatePurchase data that must be analyzed and answered with approval or declination.
177
+ # If the provided digital signature does not check out with the StarkBank public key, a stark.exception.InvalidSignatureException will be raised.
178
+ # If the authorization request is not answered within 2 seconds or is not answered with an HTTP status code 200 the
179
+ # CorporatePurchase will go through the pre-configured stand-in validation.
180
+ #
181
+ # ## Parameters (required):
182
+ # - content [string]: response content from request received at user endpoint (not parsed)
183
+ # - signature [string]: base-64 digital signature received at response header 'Digital-Signature'
184
+ #
185
+ # # ## Parameters (optional):
186
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if starkbank.user was set before function call
187
+ #
188
+ # ## Return:
189
+ # - Parsed CorporatePurchase object
190
+ def self.parse(content:, signature:, user: nil)
191
+ StarkBank::Utils::Parse.parse_and_verify(
192
+ content: content,
193
+ signature: signature,
194
+ user: user,
195
+ key: nil,
196
+ resource: resource
197
+ )
198
+ end
199
+
200
+ # # Helps you respond CorporatePurchase requests
201
+ #
202
+ # ## Parameters (required):
203
+ # - status [string]: sub-issuer response to the authorization. ex: 'approved' or 'denied'
204
+ #
205
+ # ## Parameters (conditionally required):
206
+ # - reason [string]: denial reason. Options: 'other', 'blocked', 'lostCard', 'stolenCard', 'invalidPin', 'invalidCard', 'cardExpired', 'corporateError', 'concurrency', 'standInDenial', 'subIssuerError', 'invalidPurpose', 'invalidZipCode', 'invalidWalletId', 'inconsistentCard', 'settlementFailed', 'cardRuleMismatch', 'invalidExpiration', 'prepaidInstallment', 'holderRuleMismatch', 'insufficientBalance', 'tooManyTransactions', 'invalidSecurityCode', 'invalidPaymentMethod', 'confirmationDeadline', 'withdrawalAmountLimit', 'insufficientCardLimit', 'insufficientHolderLimit'
207
+ #
208
+ # # ## Parameters (optional):
209
+ # - amount [integer, default nil]: amount in cents that was authorized. ex: 1234 (= R$ 12.34)
210
+ # - tags [list of strings, default nil]: tags to filter retrieved object. ex: ['tony', 'stark']
211
+ #
212
+ # ## Return:
213
+ # - Dumped JSON string that must be returned to us on the CorporatePurchase request
214
+ def self.response(
215
+ status:, reason: nil, amount: nil, tags: nil
216
+ )
217
+ params = {
218
+ 'status': status,
219
+ 'reason': reason,
220
+ 'amount': amount,
221
+ 'tags': tags
222
+ }
223
+
224
+ params.to_json
225
+ end
226
+
227
+ def self.resource
228
+ {
229
+ resource_name: 'CorporatePurchase',
230
+ resource_maker: proc { |json|
231
+ CorporatePurchase.new(
232
+ id: json['id'],
233
+ holder_id: json['holder_id'],
234
+ holder_name: json['holder_name'],
235
+ center_id: json['center_id'],
236
+ card_id: json['card_id'],
237
+ card_ending: json['card_ending'],
238
+ description: json['description'],
239
+ amount: json['amount'],
240
+ tax: json['tax'],
241
+ corporate_amount: json['corporate_amount'],
242
+ corporate_currency_code: json['corporate_currency_code'],
243
+ corporate_currency_symbol: json['corporate_currency_symbol'],
244
+ merchant_amount: json['merchant_amount'],
245
+ merchant_currency_code: json['merchant_currency_code'],
246
+ merchant_currency_symbol: json['merchant_currency_symbol'],
247
+ merchant_category_code: json['merchant_category_code'],
248
+ merchant_category_type: json['merchant_category_type'],
249
+ merchant_country_code: json['merchant_country_code'],
250
+ merchant_name: json['merchant_name'],
251
+ merchant_display_name: json['merchant_display_name'],
252
+ merchant_display_url: json['merchant_display_url'],
253
+ merchant_fee: json['merchant_fee'],
254
+ method_code: json['method_code'],
255
+ tags: json['tags'],
256
+ corporate_transaction_ids: json['corporate_transaction_ids'],
257
+ status: json['status'],
258
+ updated: json['updated'],
259
+ created: json['created'],
260
+ )
261
+ }
262
+ }
263
+ end
264
+ end
265
+ end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ require('starkcore')
4
+ require_relative('corporatepurchase')
5
+ require_relative('../utils/rest')
6
+
7
+ module StarkBank
8
+ class CorporatePurchase
9
+ # # CorporatePurchase::Log object
10
+ #
11
+ # Every time a CorporatePurchase entity is updated, a corresponding CorporatePurchase::Log is generated for the entity.
12
+ # This Log is never generated by the user, but it can be retrieved to check additional information on the CorporatePurchase.
13
+ #
14
+ # ## Attributes (return-only):
15
+ # - id [string]: unique id returned when the log is created. ex: '5656565656565656'
16
+ # - purchase [CorporatePurchase]: CorporatePurchase entity to which the log refers to.
17
+ # - corporate_transaction_id [string]: transaction ID related to the CorporateCard.
18
+ # - errors [list of strings]: list of errors linked to this CorporatePurchase event
19
+ # - type [string]: type of the CorporatePurchase event which triggered the log creation. ex: 'approved', 'canceled', 'confirmed', 'denied', 'reversed', 'voided'.
20
+ # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
21
+ class Log < StarkCore::Utils::Resource
22
+ attr_reader :id, :purchase, :corporate_transaction_id, :errors, :type, :created
23
+ def initialize(id: nil, purchase: nil, corporate_transaction_id: nil, errors: nil, type: nil, created: nil)
24
+ super(id)
25
+ @purchase = purchase
26
+ @corporate_transaction_id = corporate_transaction_id
27
+ @errors = errors
28
+ @type = type
29
+ @created = StarkCore::Utils::Checks.check_datetime(created)
30
+ end
31
+
32
+ # # Retrieve a specific CorporatePurchase::Log
33
+ #
34
+ # Receive a single CorporatePurchase::Log object previously created by the Stark Bank 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 StarkBank.user was set before function call
41
+ #
42
+ # ## Return:
43
+ # - CorporatePurchase::Log object with updated attributes
44
+ def self.get(id, user: nil)
45
+ StarkBank::Utils::Rest.get_id(id: id, user: user, **resource)
46
+ end
47
+
48
+ # # Retrieve CorporatePurchase::Logs
49
+ #
50
+ # Receive a generator of CorporatePurchase::Log objects previously created in the Stark Bank API
51
+ #
52
+ # ## Parameters (optional):
53
+ # - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
54
+ # - after [Date or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
55
+ # - before [Date or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
56
+ # - types [list of strings, default nil]: filter for log event types. ex: ['approved', 'canceled', 'confirmed', 'denied', 'reversed', 'voided']
57
+ # - purchase_ids [list of strings, default nil]: list of Purchase ids to filter logs. ex: ['5656565656565656', '4545454545454545']
58
+ # - ids [list of strings, default nil]: list of CorporatePurchase ids to filter logs. ex: ['5656565656565656', '4545454545454545']
59
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if starkbank.user was set before function call
60
+ #
61
+ # ## Return:
62
+ # - generator of CorporatePurchase::Log objects with updated attributes
63
+ def self.query(ids: nil, limit: nil, after: nil, before: nil, types: nil, purchase_ids: nil, user: nil)
64
+ after = StarkCore::Utils::Checks.check_date(after)
65
+ before = StarkCore::Utils::Checks.check_date(before)
66
+ StarkBank::Utils::Rest.get_stream(
67
+ ids: ids,
68
+ limit: limit,
69
+ after: after,
70
+ before: before,
71
+ types: types,
72
+ purchase_ids: purchase_ids,
73
+ user: user,
74
+ **resource
75
+ )
76
+ end
77
+
78
+ # # Retrieve paged CorporatePurchase::Logs
79
+ #
80
+ # Receive a list of up to 100 Log objects previously created in the Stark Bank API and the cursor to the next page.
81
+ # Use this function instead of query if you want to manually page your purchases.
82
+ #
83
+ # ## Parameters (optional):
84
+ # - cursor [string, default nil]: cursor returned on the previous page function call
85
+ # - limit [integer, default 100]: maximum number of objects to be retrieved. Max = 100. ex: 35
86
+ # - after [Date or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
87
+ # - before [Date or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
88
+ # - types [list of strings, default nil]: filter for log event types. ex: ['approved', 'canceled', 'confirmed', 'denied', 'reversed', 'voided']
89
+ # - purchase_ids [list of strings, default nil]: list of Purchase ids to filter logs. ex: ['5656565656565656', '4545454545454545']
90
+ # - ids [list of strings, default nil]: list of CorporatePurchase ids to filter logs. ex: ['5656565656565656', '4545454545454545']
91
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if starkbank.user was set before function call
92
+ #
93
+ # ## Return:
94
+ # - list of CorporatePurchase::Log objects with updated attributes
95
+ # - cursor to retrieve the next page of Log objects
96
+ def self.page(cursor: nil, ids: nil, limit: nil, after: nil, before: nil, types: nil, purchase_ids: nil, user: nil)
97
+ after = StarkCore::Utils::Checks.check_date(after)
98
+ before = StarkCore::Utils::Checks.check_date(before)
99
+ StarkBank::Utils::Rest.get_page(
100
+ cursor: cursor,
101
+ ids: ids,
102
+ limit: limit,
103
+ after: after,
104
+ before: before,
105
+ types: types,
106
+ purchase_ids: purchase_ids,
107
+ user: user,
108
+ **resource
109
+ )
110
+ end
111
+
112
+ def self.resource
113
+ request_maker = StarkBank::CorporatePurchase.resource[:resource_maker]
114
+ {
115
+ resource_name: 'CorporatePurchaseLog',
116
+ resource_maker: proc { |json|
117
+ Log.new(
118
+ id: json['id'],
119
+ purchase: StarkCore::Utils::API.from_api_json(request_maker, json['purchase']),
120
+ corporate_transaction_id: json['corporate_transaction_id'],
121
+ errors: json['errors'],
122
+ type: json['type'],
123
+ created: json['created']
124
+ )
125
+ }
126
+ }
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative('../utils/rest')
4
+
5
+ module StarkBank
6
+ # # CorporateRule object
7
+ #
8
+ # The CorporateRule object displays the spending rules of CorporateCards and CorporateHolders created in your Workspace.
9
+ #
10
+ # ## Parameters (required):
11
+ # - name [string]: rule name. ex: 'Travel' or 'Food'
12
+ # - amount [integer]: maximum amount that can be spent in the informed interval. ex: 200000 (= R$ 2000.00)
13
+ #
14
+ # ## Parameters (optional):
15
+ # - interval [string, default 'lifetime']: interval after which the rule amount counter will be reset to 0. ex: 'instant', 'day', 'week', 'month', 'year' or 'lifetime'
16
+ # - schedule [string, default nil]: schedule time for user to spend. ex: "every monday, wednesday from 00:00 to 23:59 in America/Sao_Paulo"
17
+ # - currency_code [string, default 'BRL']: code of the currency that the rule amount refers to. ex: 'BRL' or 'USD'
18
+ # - categories [list of MerchantCategories, default nil]: merchant categories accepted by the rule. ex: [MerchantCategory(code='fastFoodRestaurants')]
19
+ # - countries [list of MerchantCountries, default nil]: countries accepted by the rule. ex: [MerchantCountry(code='BRA')]
20
+ # - methods [list of CardMethods, default nil]: card purchase methods accepted by the rule. ex: [CardMethod(code='magstripe')]
21
+ #
22
+ # ## Attributes (expanded return-only):
23
+ # - id [string]: unique id returned when a CorporateRule is created, used to update a specific CorporateRule. ex: '5656565656565656'
24
+ # - counter_amount [integer]: current rule spent amount. ex: 1000
25
+ # - currency_symbol [string]: currency symbol. ex: 'R$'
26
+ # - currency_name [string]: currency name. ex: 'Brazilian Real'
27
+ class CorporateRule < StarkCore::Utils::Resource
28
+ attr_reader :name, :interval, :amount, :currency_code, :counter_amount, :currency_name, :currency_symbol,
29
+ :categories, :countries, :methods
30
+ def initialize(
31
+ name:, amount:, id: nil, interval: nil, schedule: nil, currency_code: nil, categories: nil, countries: nil, methods: nil,
32
+ counter_amount: nil, currency_symbol: nil, currency_name: nil
33
+ )
34
+ super(id)
35
+ @name = name
36
+ @amount = amount
37
+ @interval = interval
38
+ @schedule = schedule
39
+ @currency_code = currency_code
40
+ @categories = CorporateRule.parse_categories(categories)
41
+ @countries = CorporateRule.parse_categories(countries)
42
+ @methods = CorporateRule.parse_categories(methods)
43
+ @counter_amount = counter_amount
44
+ @currency_symbol = currency_symbol
45
+ @currency_name = currency_name
46
+ end
47
+
48
+ def self.parse_categories(categories)
49
+ resource_maker = StarkBank::MerchantCategory.resource[:resource_maker]
50
+ return categories if categories.nil?
51
+
52
+ parsed_categories = []
53
+ categories.each do |category|
54
+ unless category.is_a? MerchantCategory
55
+ category = StarkCore::Utils::API.from_api_json(resource_maker, category)
56
+ end
57
+ parsed_categories << category
58
+ end
59
+ parsed_categories
60
+ end
61
+
62
+ def self.parse_countries(countries)
63
+ resource_maker = StarkBank::MerchantCountry.resource[:resource_maker]
64
+ return countries if countries.nil?
65
+
66
+ parsed_countries = []
67
+ countries.each do |country|
68
+ unless country.is_a? MerchantCountry
69
+ country = StarkCore::Utils::API.from_api_json(resource_maker, country)
70
+ end
71
+ parsed_countries << country
72
+ end
73
+ parsed_countries
74
+ end
75
+
76
+ def self.parse_methods(methods)
77
+ resource_maker = StarkBank::CardMethod.resource[:resource_maker]
78
+ return methods if methods.nil?
79
+
80
+ parsed_methods = []
81
+ methods.each do |method|
82
+ unless method.is_a? CardMethod
83
+ method = StarkCore::Utils::API.from_api_json(resource_maker, method)
84
+ end
85
+ parsed_methods << method
86
+ end
87
+ parsed_methods
88
+ end
89
+
90
+ def self.parse_rules(rules)
91
+ rule_maker = StarkBank::CorporateRule.resource[:resource_maker]
92
+ return rules if rules.nil?
93
+
94
+ parsed_rules = []
95
+ rules.each do |rule|
96
+ unless rule.is_a? CorporateRule
97
+ rule = StarkCore::Utils::API.from_api_json(rule_maker, rule)
98
+ end
99
+ parsed_rules << rule
100
+ end
101
+ parsed_rules
102
+ end
103
+
104
+ def self.resource
105
+ {
106
+ resource_name: 'CorporateRule',
107
+ resource_maker: proc { |json|
108
+ CorporateRule.new(
109
+ name: json['name'],
110
+ amount: json['amount'],
111
+ interval: json['interval'],
112
+ schedule: json['schedule'],
113
+ currency_code: json['currency_code'],
114
+ categories: json['categories'],
115
+ countries: json['countries'],
116
+ methods: json['methods'],
117
+ counter_amount: json['counter_amount'],
118
+ currency_symbol: json['currency_symbol'],
119
+ currency_name: json['currency_name']
120
+ )
121
+ }
122
+ }
123
+ end
124
+ end
125
+ end
126
+
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ require('starkcore')
4
+ require_relative('../utils/rest')
5
+
6
+ module StarkBank
7
+ # # CorporateTransaction object
8
+ #
9
+ # The CorporateTransaction objects created in your Workspace to represent each balance shift.
10
+ #
11
+ # ## Attributes (return-only):
12
+ # - id [string]: unique id returned when CorporateTransaction is created. ex: '5656565656565656'
13
+ # - amount [integer]: CorporateTransaction value in cents. ex: 1234 (= R$ 12.34)
14
+ # - balance [integer]: balance amount of the Workspace at the instant of the Transaction in cents. ex: 200 (= R$ 2.00)
15
+ # - description [string]: CorporateTransaction description. ex: 'Buying food'
16
+ # - source [string]: source of the transaction. ex: 'corporate-purchase/5656565656565656'
17
+ # - tags [string]: list of strings inherited from the source resource. ex: ['tony', 'stark']
18
+ # - created [DateTime]: creation datetime for the CorporateTransaction. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
19
+ class CorporateTransaction < StarkCore::Utils::Resource
20
+ attr_reader :id, :amount, :balance, :description, :source, :tags, :created
21
+
22
+ def initialize(id: nil, amount: nil, balance: nil, description: nil, source: nil, tags: nil, created: nil)
23
+ super(id)
24
+ @amount = amount
25
+ @balance = balance
26
+ @description = description
27
+ @source = source
28
+ @tags = tags
29
+ @created = StarkCore::Utils::Checks.check_datetime(created)
30
+ end
31
+
32
+ # # Retrieve a specific CorporateTransaction
33
+ #
34
+ # Receive a single CorporateTransaction object previously created in the Stark Bank API by 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 StarkBank.user was set before function call
41
+ #
42
+ # ## Return:
43
+ # - CorporateTransaction object with updated attributes
44
+ def self.get(id, user: nil)
45
+ StarkBank::Utils::Rest.get_id(id: id, user: user, **resource)
46
+ end
47
+
48
+ # # Retrieve CorporateTransactions
49
+ #
50
+ # Receive a generator of CorporateTransaction objects previously created in the Stark Bank API
51
+ #
52
+ # ## Parameters (optional):
53
+ # - tags [list of strings, default nil]: tags to filter retrieved objects. ex: ['tony', 'stark']
54
+ # - external_ids [list of strings, default nil]: external IDs. ex: ['5656565656565656', '4545454545454545']
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
+ # - status [string, default nil]: filter for status of retrieved objects. ex: 'approved', 'canceled', 'denied', 'confirmed' or 'voided'
58
+ # - ids [list of strings, default nil]: purchase IDs
59
+ # - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
60
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if starkbank.user was set before function call
61
+ #
62
+ # ## Return:
63
+ # - generator of CorporateTransaction objects with updated attributes
64
+ def self.query(tags: nil, external_ids: nil, after: nil, before: nil, status: nil, ids: nil, limit: nil, user: nil)
65
+ after = StarkCore::Utils::Checks.check_date(after)
66
+ before = StarkCore::Utils::Checks.check_date(before)
67
+ StarkBank::Utils::Rest.get_stream(
68
+ tags: tags,
69
+ external_ids: external_ids,
70
+ after: after,
71
+ before: before,
72
+ status: status,
73
+ ids: ids,
74
+ limit: limit,
75
+ user: user,
76
+ **resource
77
+ )
78
+ end
79
+
80
+ # # Retrieve paged CorporateTransactions
81
+ #
82
+ # Receive a list of up to 100 CorporateTransaction objects previously created in the Stark Bank API and the cursor to the next page.
83
+ # Use this function instead of query if you want to manually page your requests.
84
+ #
85
+ # ## Parameters (optional):
86
+ # - cursor [string, default nil]: cursor returned on the previous page function call.
87
+ # - limit [integer, default 100]: maximum number of objects to be retrieved. Max = 100. ex: 35
88
+ # - after [Date or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
89
+ # - before [Date or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
90
+ # - tags [list of strings, default nil]: tags to filter retrieved objects. ex: ['tony', 'stark']
91
+ # - external_ids [list of strings, default nil]: external IDs. ex: ['5656565656565656', '4545454545454545']
92
+ # - status [string, default nil]: filter for status of retrieved objects. ex: 'approved', 'canceled', 'denied', 'confirmed' or 'voided'
93
+ # - ids [list of strings, default nil]: purchase IDs
94
+ # - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if starkbank.user was set before function call
95
+ #
96
+ # ## Return:
97
+ # - list of CorporateTransactions objects with updated attributes
98
+ # - cursor to retrieve the next page of CorporateTransactions objects
99
+ def self.page(cursor: nil, tags: nil, external_ids: nil, after: nil, before: nil, status: nil, ids: nil, limit: nil,
100
+ user: nil)
101
+ after = StarkCore::Utils::Checks.check_date(after)
102
+ before = StarkCore::Utils::Checks.check_date(before)
103
+ StarkBank::Utils::Rest.get_page(
104
+ cursor: cursor,
105
+ tags: tags,
106
+ external_ids: external_ids,
107
+ after: after,
108
+ before: before,
109
+ status: status,
110
+ ids: ids,
111
+ limit: limit,
112
+ user: user,
113
+ **resource
114
+ )
115
+ end
116
+
117
+ def self.resource
118
+ {
119
+ resource_name: 'CorporateTransaction',
120
+ resource_maker: proc { |json|
121
+ CorporateTransaction.new(
122
+ id: json['id'],
123
+ amount: json['amount'],
124
+ balance: json['balance'],
125
+ description: json['description'],
126
+ source: json['source'],
127
+ tags: json['tags'],
128
+ created: json['created']
129
+ )
130
+ }
131
+ }
132
+ end
133
+ end
134
+ end