Skip to content

Commit

Permalink
Merge pull request #54 from starkbank/add/invoice-rules
Browse files Browse the repository at this point in the history
Add/invoice rules
  • Loading branch information
rgaia-stark authored May 12, 2023
2 parents 23b6203 + b94ebea commit 2496859
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions lib/corporate_purchase/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'],
Expand Down
7 changes: 5 additions & 2 deletions lib/corporate_rule/corporaterule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
Expand All @@ -25,17 +26,18 @@ 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)
@name = name
@amount = amount
@interval = interval
@schedule = schedule
@purposes = purposes
@currency_code = currency_code
@categories = CorporateRule.parse_categories(categories)
@countries = CorporateRule.parse_categories(countries)
Expand Down Expand Up @@ -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'],
Expand Down
2 changes: 1 addition & 1 deletion lib/darf_payment/darf_payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions lib/invoice/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand All @@ -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
)
Expand All @@ -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
Expand Down Expand Up @@ -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'],
Expand Down
46 changes: 46 additions & 0 deletions lib/invoice/rule.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/starkbank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 2496859

Please sign in to comment.