Skip to content

Commit

Permalink
Merge pull request #39 from starkbank/feature/darf-payment
Browse files Browse the repository at this point in the history
Add DarfPayment resource
  • Loading branch information
cdottori-stark authored Jul 30, 2021
2 parents e5c85f4 + f9cfbf0 commit 926a3d9
Show file tree
Hide file tree
Showing 34 changed files with 469 additions and 75 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Given a version number MAJOR.MINOR.PATCH, increment:
- page method as a manual-pagination alternative to queries
- Institution resource to allow query of institutions recognized by the Brazilian Central Bank for Pix and TED transactions
- TaxPayment resource
- DarfPayment resource

### Fixed
- special characters in brcodePreview query
Expand Down
218 changes: 218 additions & 0 deletions lib/darf_payment/darf_payment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
# frozen_string_literal: true

require_relative('../utils/resource')
require_relative('../utils/rest')
require_relative('../utils/checks')

module StarkBank
# # DarfPayment object
#
# When you initialize a DarfPayment, the entity will not be automatically
# created in the Stark Bank API. The 'create' function sends the objects
# to the Stark Bank API and returns the list of created objects.
#
# ## Parameters (required):
# - description [string]: Text to be displayed in your statement (min. 10 characters). ex: 'payment ABC'
# - description [string]: Text to be displayed in your statement (min. 10 characters). ex: 'payment ABC'
# - revenue_code [string]: 4-digit tax code assigned by Federal Revenue. ex: '5948'
# - tax_id [tax_id]: tax id (formatted or unformatted) of the payer. ex: '12.345.678/0001-95'
# - competence [Date, DateTime, Time or string, default today]: competence month of the service. ex: Date.new(2020, 3, 10)
# - nominal_amount [int]: amount due in cents without fee or interest. ex: 23456 (= R$ 234.56)
# - fine_amount [int]: fixed amount due in cents for fines. ex: 234 (= R$ 2.34)
# - interest_amount [int]: amount due in cents for interest. ex: 456 (= R$ 4.56)
# - due [Date, DateTime, Time or string, default today]: due date for payment. ex: Date.new(2020, 3, 10)
#
# ## Parameters (optional):
# - reference_number [string, default nil]: number assigned to the region of the tax. ex: '08.1.17.00-4'
# - scheduled [Date, DateTime, Time or string, default today]: payment scheduled date. ex: Date.new(2020, 3, 10)
# - tags [list of strings, default nil]: list of strings for tagging
#
# ## Attributes (return-only):
# - id [string, default nil]: unique id returned when payment is created. ex: '5656565656565656'
# - status [string, default nil]: current payment status. ex: 'success' or 'failed'
# - amount [int, default nil]: Total amount due calculated from other amounts. ex: 24146 (= R$ 241.46)
# - fee [integer, default nil]: fee charged when the DarfPayment is processed. ex: 0 (= R$ 0.00)
# - created [DateTime, default nil]: creation datetime for the Invoice. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
# - updated [DateTime, default nil]: latest update datetime for the Invoice. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
class DarfPayment < StarkBank::Utils::Resource
attr_reader :id, :revenue_code, :tax_id, :competence, :reference_number, :fine_amount, :interest_amount,
:due, :description, :tags, :scheduled, :status, :amount, :nominal_amount, :fee, :updated, :created
def initialize(
id: nil, revenue_code:, tax_id:, competence:, reference_number:, fine_amount:, interest_amount:, due:, description: nil,
tags: nil, scheduled: nil, status: nil, amount: nil, nominal_amount: nil, fee: nil, updated: nil, created: nil
)
super(id)
@revenue_code = revenue_code
@tax_id = tax_id
@competence = StarkBank::Utils::Checks.check_date(competence)
@reference_number = reference_number
@fine_amount = fine_amount
@interest_amount = interest_amount
@due = StarkBank::Utils::Checks.check_date(due)
@description = description
@tags = tags
@scheduled = StarkBank::Utils::Checks.check_date(scheduled)
@status = status
@amount = amount
@nominal_amount = nominal_amount
@fee = fee
@updated = StarkBank::Utils::Checks.check_datetime(updated)
@created = StarkBank::Utils::Checks.check_datetime(created)
end

# # Create DarfPayments
#
# Send a list of DarfPayment objects for creation in the Stark Bank API
#
# ## Parameters (required):
# - payments [list of DarfPayment objects]: list of DarfPayment objects to be created in the API
#
# ## Parameters (optional):
# - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkBank.user was set before function call
#
# ## Return:
# - list of DarfPayment objects with updated attributes
def self.create(payments, user: nil)
StarkBank::Utils::Rest.post(entities: payments, user: user, **resource)
end

# # Retrieve a specific DarfPayment
#
# Receive a single DarfPayment object previously created by the Stark Bank API by passing its id
#
# ## Parameters (required):
# - id [string]: object unique id. ex: '5656565656565656'
#
# ## Parameters (optional):
# - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkBank.user was set before function call
#
# ## Return:
# - DarfPayment object with updated attributes
def self.get(id, user: nil)
StarkBank::Utils::Rest.get_id(id: id, user: user, **resource)
end

# # Retrieve a specific DarfPayment pdf file
#
# Receive a single DarfPayment pdf file generated in the Stark Bank API by passing its id.
# Only valid for darf payments with 'success' status.
#
# ## Parameters (required):
# - id [string]: object unique id. ex: '5656565656565656'
#
# ## Parameters (optional):
# - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkBank.user was set before function call
#
# ## Return:
# - DarfPayment pdf file
def self.pdf(id, user: nil)
StarkBank::Utils::Rest.get_content(id: id, user: user, sub_resource_name: 'pdf', **resource)
end

# # Retrieve DarfPayments
#
# Receive a generator of DarfPayment objects previously created in the Stark Bank API
#
# ## Parameters (optional):
# - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
# - after [Date , DateTime, Time or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
# - before [Date, DateTime, Time or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
# - tags [list of strings, default nil]: tags to filter retrieved objects. ex: ['tony', 'stark']
# - ids [list of strings, default nil]: list of ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545']
# - status [string, default nil]: filter for status of retrieved objects. ex: 'paid' or 'registered'
# - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkBank.user was set before function call
#
# ## Return:
# - generator of DarfPayment objects with updated attributes
def self.query(limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, user: nil)
after = StarkBank::Utils::Checks.check_date(after)
before = StarkBank::Utils::Checks.check_date(before)
StarkBank::Utils::Rest.get_stream(
limit: limit,
after: after,
before: before,
tags: tags,
ids: ids,
status: status,
user: user,
**resource
)
end

# # Retrieve paged Darf Payments
#
# Receive a list of up to 100 Darf Payment objects previously created in the Stark Bank API and the cursor to the next page.
# Use this function instead of query if you want to manually page your requests.
#
# ## Parameters (optional):
# - cursor [string, default nil]: cursor returned on the previous page function call
# - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
# - after [Date , DateTime, Time or string, default nil]: date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
# - before [Date, DateTime, Time or string, default nil]: date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
# - tags [list of strings, default nil]: tags to filter retrieved objects. ex: ['tony', 'stark']
# - ids [list of strings, default nil]: list of ids to filter retrieved objects. ex: ['5656565656565656', '4545454545454545']
# - status [string, default nil]: filter for status of retrieved objects. ex: 'paid' or 'registered'
# - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkBank.user was set before function call
#
# ## Return:
# - list of Darf Payment objects with updated attributes and cursor to retrieve the next page of Darf Payment objects
def self.page(cursor: nil, limit: nil, after: nil, before: nil, status: nil, tags: nil, ids: nil, user: nil)
after = StarkBank::Utils::Checks.check_date(after)
before = StarkBank::Utils::Checks.check_date(before)
return StarkBank::Utils::Rest.get_page(
cursor: cursor,
limit: limit,
after: after,
before: before,
tags: tags,
ids: ids,
status: status,
user: user,
**resource
)
end

# # Delete a DarfPayment entity
#
# Delete a DarfPayment entity previously created in the Stark Bank API
#
# ## Parameters (required):
# - id [string]: UtilityPayment unique id. ex:'5656565656565656'
#
# ## Parameters (optional):
# - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkBank.user was set before function call
#
# ## Return:
# - deleted TaxPayment object
def self.delete(id, user: nil)
StarkBank::Utils::Rest.delete_id(id: id, user: user, **resource)
end

def self.resource
{
resource_name: 'DarfPayment',
resource_maker: proc { |json|
DarfPayment.new(
id: json['id'],
revenue_code: json['revenue_code'],
tax_id: json['tax_id'],
competence: json['competence'],
reference_number: json['reference_number'],
fine_amount: json['fine_amount'],
interest_amount: json['interest_amount'],
due: json['due'],
description: json['description'],
tags: json['tags'],
scheduled: json['scheduled'],
status: json['status'],
amount: json['amount'],
nominal_amount: json['nominal_amount'],
fee: json['fee'],
updated: json['updated'],
created: json['created'],
)
}
}
end
end
end
125 changes: 125 additions & 0 deletions lib/darf_payment/log.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# frozen_string_literal: true

require_relative('../utils/resource')
require_relative('../utils/rest')
require_relative('../utils/checks')
require_relative('darf_payment')

module StarkBank
class DarfPayment
# # DarfPayment::Log object
#
# Every time a DarfPayment entity is updated, a corresponding DarfPayment::Log
# is generated for the entity. This log is never generated by the
# user, but it can be retrieved to check additional information
# on the DarfPayment.
#
# ## Attributes:
# - id [string]: unique id returned when the log is created. ex: '5656565656565656'
# - payment [DarfPayment]: DarfPayment entity to which the log refers to.
# - errors [list of strings]: list of errors linked to this DarfPayment event
# - type [string]: type of the DarfPayment event which triggered the log creation. ex: 'processing' or 'success'
# - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0)
class Log < StarkBank::Utils::Resource
attr_reader :id, :created, :type, :errors, :payment
def initialize(id:, created:, type:, errors:, payment:)
super(id)
@type = type
@errors = errors
@payment = payment
@created = StarkBank::Utils::Checks.check_datetime(created)
end

# # Retrieve a specific Log
#
# Receive a single Log object previously created by the Stark Bank API by passing its id
#
# ## Parameters (required):
# - id [string]: object unique id. ex: '5656565656565656'
#
# ## Parameters (optional):
# - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if StarkBank.user was set before function call
#
# ## Return:
# - Log object with updated attributes
def self.get(id, user: nil)
StarkBank::Utils::Rest.get_id(id: id, user: user, **resource)
end

# # Retrieve Logs
#
# Receive a generator of Log objects previously created in the Stark Bank API
#
# ## Parameters (optional):
# - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
# - after [Date, DateTime, Time or string, default nil] date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
# - before [Date, DateTime, Time or string, default nil] date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
# - types [list of strings, default nil]: filter for log event types. ex: 'paid' or 'canceled'
# - payment_ids [list of strings, default nil]: list of TaxPayment ids to filter logs. ex: ['5656565656565656', '4545454545454545']
# - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if Starkbank.user was set before function call
#
# ## Return:
# - list of Log objects with updated attributes
def self.query(limit: nil, after: nil, before: nil, types: nil, payment_ids: nil, user: nil)
after = StarkBank::Utils::Checks.check_date(after)
before = StarkBank::Utils::Checks.check_date(before)
StarkBank::Utils::Rest.get_stream(
limit: limit,
after: after,
before: before,
types: types,
payment_ids: payment_ids,
user: user,
**resource
)
end

# # Retrieve paged Logs
#
# Receive a list of up to 100 Log objects previously created in the Stark Bank API and the cursor to the next page.
# Use this function instead of query if you want to manually page your requests.
#
# ## Parameters (optional):
# - cursor [string, default nil]: cursor returned on the previous page function call
# - limit [integer, default nil]: maximum number of objects to be retrieved. Unlimited if nil. ex: 35
# - after [Date, DateTime, Time or string, default nil] date filter for objects created only after specified date. ex: Date.new(2020, 3, 10)
# - before [Date, DateTime, Time or string, default nil] date filter for objects created only before specified date. ex: Date.new(2020, 3, 10)
# - types [list of strings, default nil]: filter for log event types. ex: 'paid' or 'canceled'
# - payment_ids [list of strings, default nil]: list of TaxPayment ids to filter logs. ex: ['5656565656565656', '4545454545454545']
# - user [Organization/Project object, default nil]: Organization or Project object. Not necessary if Starkbank.user was set before function call
#
# ## Return:
# - list of Log objects with updated attributes and cursor to retrieve the next page of Log objects
def self.page(cursor: nil, limit: nil, after: nil, before: nil, types: nil, payment_ids: nil, user: nil)
after = StarkBank::Utils::Checks.check_date(after)
before = StarkBank::Utils::Checks.check_date(before)
return StarkBank::Utils::Rest.get_page(
cursor: cursor,
limit: limit,
after: after,
before: before,
types: types,
payment_ids: payment_ids,
user: user,
**resource
)
end

def self.resource
darf_payment_maker = StarkBank::DarfPayment.resource[:resource_maker]
{
resource_name: 'DarfPaymentLog',
resource_maker: proc { |json|
Log.new(
id: json['id'],
created: json['created'],
type: json['type'],
errors: json['errors'],
payment: StarkBank::Utils::API.from_api_json(darf_payment_maker, json['payment'])
)
}
}
end
end
end
end
2 changes: 2 additions & 0 deletions lib/event/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require_relative('../boleto_payment/log')
require_relative('../utility_payment/log')
require_relative('../tax_payment/log')
require_relative('../darf_payment/log')

module StarkBank
# # Webhook Event object
Expand Down Expand Up @@ -49,6 +50,7 @@ def initialize(id:, log:, created:, is_delivered:, workspace_id:, subscription:)
'boleto-payment': StarkBank::BoletoPayment::Log.resource,
'utility-payment': StarkBank::UtilityPayment::Log.resource,
'tax-payment': StarkBank::TaxPayment::Log.resource,
'darf-payment': StarkBank::DarfPayment::Log.resource,
'boleto-holmes': StarkBank::BoletoHolmes::Log.resource
}[subscription.to_sym]

Expand Down
6 changes: 4 additions & 2 deletions lib/payment_request/payment_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,18 @@ def parse_payment(payment:, type:)
return [payment, 'boleto-payment'] if payment.is_a?(StarkBank::BoletoPayment)
return [payment, 'utility-payment'] if payment.is_a?(StarkBank::UtilityPayment)
return [payment, 'tax-payment'] if payment.is_a?(StarkBank::TaxPayment)
return [payment, 'darf-payment'] if payment.is_a?(StarkBank::DarfPayment)

raise(Exception('Payment must either be a Transfer, a Transaction, a BrcodePayment, BoletoPayment, a UtilityPayment, a TaxPayment or a hash.')) unless payment.is_a?(Hash)
raise(Exception('Payment must either be a Transfer, a Transaction, a BrcodePayment, BoletoPayment, a UtilityPayment, a TaxPayment, a DarfPayment or a hash.')) unless payment.is_a?(Hash)

resource = {
'transfer': StarkBank::Transfer.resource,
'transaction': StarkBank::Transaction.resource,
'brcode-payment': StarkBank::BrcodePayment.resource,
'boleto-payment': StarkBank::BoletoPayment.resource,
'utility-payment': StarkBank::UtilityPayment.resource,
'tax-payment': StarkBank::TaxPayment.resource
'tax-payment': StarkBank::TaxPayment.resource,
'darf-payment': StarkBank::DarfPayment.resource
}[type.to_sym]

payment = StarkBank::Utils::API.from_api_json(resource[:resource_maker], payment) unless resource.nil?
Expand Down
2 changes: 2 additions & 0 deletions lib/starkbank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
require_relative('utility_payment/log')
require_relative('tax_payment/tax_payment')
require_relative('tax_payment/log')
require_relative('darf_payment/darf_payment')
require_relative('darf_payment/log')
require_relative('webhook/webhook')
require_relative('event/event')
require_relative('event/attempt')
Expand Down
Loading

0 comments on commit 926a3d9

Please sign in to comment.