From b94ebeae82e78f4bf5118bd7d9bbacb9c9d1d0d1 Mon Sep 17 00:00:00 2001 From: gaia-stark Date: Thu, 11 May 2023 15:10:13 -0300 Subject: [PATCH] Add Invoice::Rule sub-resource, CorporatePurchase::Log and CorporateRule attributes --- CHANGELOG.md | 5 ++++ lib/corporate_purchase/log.rb | 7 +++-- lib/corporate_rule/corporaterule.rb | 7 +++-- lib/darf_payment/darf_payment.rb | 2 +- lib/invoice/invoice.rb | 7 +++-- lib/invoice/rule.rb | 46 +++++++++++++++++++++++++++++ lib/starkbank.rb | 1 + 7 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 lib/invoice/rule.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 4435687..b20e1d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ Given a version number MAJOR.MINOR.PATCH, increment: ## [Unreleased] +### Added +- purpose and schedule attributes to CorporateRule resource +- description attribute to CorporatePurchase::Log resource +- rules attribute to Invoice resource +- Invoice::Rule sub-resource ## [2.9.0] - 2023-05-04 ### Added diff --git a/lib/corporate_purchase/log.rb b/lib/corporate_purchase/log.rb index a6bba0b..a5bce20 100644 --- a/lib/corporate_purchase/log.rb +++ b/lib/corporate_purchase/log.rb @@ -14,15 +14,17 @@ class CorporatePurchase # ## Attributes (return-only): # - id [string]: unique id returned when the log is created. ex: '5656565656565656' # - purchase [CorporatePurchase]: CorporatePurchase entity to which the log refers to. + # - description [string]: purchase descriptions. ex: 'my_description' # - corporate_transaction_id [string]: transaction ID related to the CorporateCard. # - errors [list of strings]: list of errors linked to this CorporatePurchase event # - type [string]: type of the CorporatePurchase event which triggered the log creation. ex: 'approved', 'canceled', 'confirmed', 'denied', 'reversed', 'voided'. # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) class Log < StarkCore::Utils::Resource - attr_reader :id, :purchase, :corporate_transaction_id, :errors, :type, :created - def initialize(id: nil, purchase: nil, corporate_transaction_id: nil, errors: nil, type: nil, created: nil) + attr_reader :id, :purchase, :description, :corporate_transaction_id, :errors, :type, :created + def initialize(id: nil, purchase: nil, description: nil, corporate_transaction_id: nil, errors: nil, type: nil, created: nil) super(id) @purchase = purchase + @description = description @corporate_transaction_id = corporate_transaction_id @errors = errors @type = type @@ -117,6 +119,7 @@ def self.resource Log.new( id: json['id'], purchase: StarkCore::Utils::API.from_api_json(request_maker, json['purchase']), + description: json['description'], corporate_transaction_id: json['corporate_transaction_id'], errors: json['errors'], type: json['type'], diff --git a/lib/corporate_rule/corporaterule.rb b/lib/corporate_rule/corporaterule.rb index a43a9e0..885c2cb 100644 --- a/lib/corporate_rule/corporaterule.rb +++ b/lib/corporate_rule/corporaterule.rb @@ -14,6 +14,7 @@ module StarkBank # ## Parameters (optional): # - interval [string, default 'lifetime']: interval after which the rule amount counter will be reset to 0. ex: 'instant', 'day', 'week', 'month', 'year' or 'lifetime' # - schedule [string, default nil]: schedule time for user to spend. ex: "every monday, wednesday from 00:00 to 23:59 in America/Sao_Paulo" + # - purposes [list of string, default []]: list of strings representing the allowed purposes for card purchases, you can use this to restrict ATM withdrawals. ex: ["purchase", "withdrawal"] # - currency_code [string, default 'BRL']: code of the currency that the rule amount refers to. ex: 'BRL' or 'USD' # - categories [list of MerchantCategories, default nil]: merchant categories accepted by the rule. ex: [MerchantCategory(code='fastFoodRestaurants')] # - countries [list of MerchantCountries, default nil]: countries accepted by the rule. ex: [MerchantCountry(code='BRA')] @@ -25,10 +26,10 @@ module StarkBank # - currency_symbol [string]: currency symbol. ex: 'R$' # - currency_name [string]: currency name. ex: 'Brazilian Real' class CorporateRule < StarkCore::Utils::Resource - attr_reader :name, :interval, :amount, :currency_code, :counter_amount, :currency_name, :currency_symbol, + attr_reader :name, :interval, :amount, :currency_code, :counter_amount, :schedule, :purposes, :currency_name, :currency_symbol, :categories, :countries, :methods def initialize( - name:, amount:, id: nil, interval: nil, schedule: nil, currency_code: nil, categories: nil, countries: nil, methods: nil, + name:, amount:, id: nil, interval: nil, schedule: nil, purposes: nil, currency_code: nil, categories: nil, countries: nil, methods: nil, counter_amount: nil, currency_symbol: nil, currency_name: nil ) super(id) @@ -36,6 +37,7 @@ def initialize( @amount = amount @interval = interval @schedule = schedule + @purposes = purposes @currency_code = currency_code @categories = CorporateRule.parse_categories(categories) @countries = CorporateRule.parse_categories(countries) @@ -110,6 +112,7 @@ def self.resource amount: json['amount'], interval: json['interval'], schedule: json['schedule'], + purposes: json['purposes'], currency_code: json['currency_code'], categories: json['categories'], countries: json['countries'], diff --git a/lib/darf_payment/darf_payment.rb b/lib/darf_payment/darf_payment.rb index 5f3213c..46e1f42 100644 --- a/lib/darf_payment/darf_payment.rb +++ b/lib/darf_payment/darf_payment.rb @@ -56,7 +56,7 @@ def initialize( @amount = amount @nominal_amount = nominal_amount @fee = fee - @transaction_ids = @transaction_ids + @transaction_ids = transaction_ids @updated = StarkCore::Utils::Checks.check_datetime(updated) @created = StarkCore::Utils::Checks.check_datetime(created) end diff --git a/lib/invoice/invoice.rb b/lib/invoice/invoice.rb index 0c5fac8..53263dc 100644 --- a/lib/invoice/invoice.rb +++ b/lib/invoice/invoice.rb @@ -24,6 +24,7 @@ module StarkBank # - fine [float, default 0.0]: Invoice fine for overdue payment in %. ex: 2.5 # - interest [float, default 0.0]: Invoice monthly interest for overdue payment in %. ex: 5.2 # - discounts [list of hashes, default nil]: list of hashes with 'percentage':float and 'due':DateTime or string pairs + # - rules [list of Invoice::Rule, default []]: list of Invoice::Rule objects for modifying invoice behavior. ex: [Invoice::Rule(key="allowedTaxIds", value=[ "012.345.678-90", "45.059.493/0001-73" ])] # - descriptions [list of hashes, default nil]: list of hashes with 'key':string and 'value':string pairs # - tags [list of strings, default nil]: list of strings for tagging # @@ -42,9 +43,9 @@ module StarkBank # - created [DateTime]: creation datetime for the Invoice. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) # - updated [DateTime]: latest update datetime for the Invoice. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) class Invoice < StarkCore::Utils::Resource - attr_reader :amount, :tax_id, :name, :due, :expiration, :fine, :interest, :discounts, :tags, :pdf, :link, :descriptions, :nominal_amount, :fine_amount, :interest_amount, :discount_amount, :id, :brcode, :fee, :status, :transaction_ids, :created, :updated + attr_reader :amount, :tax_id, :name, :due, :expiration, :fine, :interest, :discounts, :rules, :tags, :pdf, :link, :descriptions, :nominal_amount, :fine_amount, :interest_amount, :discount_amount, :id, :brcode, :fee, :status, :transaction_ids, :created, :updated def initialize( - amount:, tax_id:, name:, due: nil, expiration: nil, fine: nil, interest: nil, discounts: nil, + amount:, tax_id:, name:, due: nil, expiration: nil, fine: nil, interest: nil, discounts: nil, rules: nil, tags: nil, pdf: nil, link: nil, descriptions: nil, nominal_amount: nil, fine_amount: nil, interest_amount: nil, discount_amount: nil, id: nil, brcode: nil, fee: nil, status: nil, transaction_ids: nil, created: nil, updated: nil ) @@ -60,6 +61,7 @@ def initialize( @pdf = pdf @link = link @descriptions = descriptions + @rules = StarkBank::Invoice::Rule.parse_rules(rules) @nominal_amount = nominal_amount @fine_amount = fine_amount @interest_amount = interest_amount @@ -256,6 +258,7 @@ def self.resource fine: json['fine'], interest: json['interest'], discounts: json['discounts'], + rules: json['rules'], tags: json['tags'], pdf: json['pdf'], link: json['link'], diff --git a/lib/invoice/rule.rb b/lib/invoice/rule.rb new file mode 100644 index 0000000..1a4a1bf --- /dev/null +++ b/lib/invoice/rule.rb @@ -0,0 +1,46 @@ +require_relative('../utils/rest') + +module StarkBank + class Invoice + # # Invoice::Rule object + # + # The Invoice::Rule object modifies the behavior of Invoice objects when passed as an argument upon their creation. + # + # ## Parameters (required): + # - key [string]: Rule to be customized, describes what Invoice behavior will be altered. ex: "allowedTaxIds" + # - value [list of string]: Value of the rule. ex: ['012.345.678-90', '45.059.493/0001-73'] + class Rule < StarkCore::Utils::SubResource + attr_reader :key, :value + def initialize(key:, value:) + @key = key + @value = value + end + + def self.parse_rules(rules) + resource_maker = StarkBank::Invoice::Rule.resource[:resource_maker] + return rules if rules.nil? + + parsed_rules = [] + rules.each do |rule| + unless rule.is_a? Rule + rule = StarkCore::Utils::API.from_api_json(resource_maker, rule) + end + parsed_rules << rule + end + return parsed_rules + end + + def self.resource + { + resource_name: 'Rule', + resource_maker: proc { |json| + Rule.new( + key: json['key'], + value: json['value'] + ) + } + } + end + end + end +end diff --git a/lib/starkbank.rb b/lib/starkbank.rb index 595a08c..bb5c7af 100644 --- a/lib/starkbank.rb +++ b/lib/starkbank.rb @@ -5,6 +5,7 @@ require_relative('transaction/transaction') require_relative('invoice/invoice') require_relative('invoice/log') +require_relative('invoice/rule') require_relative('invoice/payment') require_relative('dict_key/dict_key') require_relative('dynamic_brcode/dynamic_brcode')