diff --git a/CHANGELOG.md b/CHANGELOG.md index 3124a91..561ed99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,8 +13,25 @@ Given a version number MAJOR.MINOR.PATCH, increment: ## [Unreleased] +### Added +- updated, type and transaction_ids attribute to UtilityPayment resource +- workspace_id attribute to Boleto resource +- updated attribute to BoletoHolmes::Log resource +- description attribute to PaymentRequest resource +- amount and transaction_ids attribute to BoletoPayment resource +- workspace_id attribute to Event resource +- transaction_ids attribute to DarfPayment and TaxPayment resources +- picture and picture_type parameters to update method on Workspace resource +- rules attribute to BrcodePayment resource +- BrcodePayment::Rule sub-resource +- rules attribute to Transfer resource +- Transfer::Rule sub-resource +- DynamicBrcode resource +- status, organization_id, picture_url and created attributes to Workspace resource ### Changed - internal structure to use starkcore as a dependency. +### Removed +- deprecated BrcodePreview resource ## [2.6.0] - 2021-09-04 ### Added @@ -54,10 +71,10 @@ Given a version number MAJOR.MINOR.PATCH, increment: ## [2.2.0] - 2020-11-16 ### Added - Invoice resource to load your account with dynamic QR Codes -- DictKey resource to get PIX key's parameters +- DictKey resource to get Pix key's parameters - Deposit resource to receive transfers passively -- PIX support in Transfer resource -- BrcodePayment support to pay static and dynamic PIX QR Codes +- Pix support in Transfer resource +- BrcodePayment support to pay static and dynamic Pix QR Codes ## [2.1.0] - 2020-10-28 ### Added diff --git a/README.md b/README.md index ece65f0..5370e51 100644 --- a/README.md +++ b/README.md @@ -30,15 +30,18 @@ is as easy as sending a text message to your client! - [Transfers](#create-transfers): Wire transfers (TED and manual Pix) - [DictKeys](#get-dict-key): Pix Key queries to use with Transfers - [Institutions](#query-bacen-institutions): Instutitions recognized by the Central Bank - - [Invoices](#create-invoices): Reconciled receivables (dynamic PIX QR Codes) - - [Deposits](#query-deposits): Other cash-ins (static PIX QR Codes, manual PIX, etc) + - [Invoices](#create-invoices): Reconciled receivables (dynamic Pix QR Codes) + - [DynamicBrcode](#create-dynamicbrcodes): Simplified reconciled receivables (dynamic Pix QR Codes) + - [Deposits](#query-deposits): Other cash-ins (static Pix QR Codes, DynamicBrcodes, manual Pix, etc) - [Boletos](#create-boletos): Boleto receivables - [BoletoHolmes](#investigate-a-boleto): Boleto receivables investigator - [BrcodePayments](#pay-a-br-code): Pay Pix QR Codes - [BoletoPayments](#pay-a-boleto): Pay Boletos - [UtilityPayments](#create-utility-payments): Pay Utility bills (water, light, etc.) - [TaxPayments](#create-tax-payments): Pay taxes + - [DarfPayments](#create-darf-payment): Pay DARFs - [PaymentPreviews](#preview-payment-information-before-executing-the-payment): Preview all sorts of payments + - [PaymentRequest](#create-payment-requests-to-be-approved-by-authorized-people-in-a-cost-center): Request a payment approval to a cost center - [Webhooks](#create-a-webhook-subscription): Configure your webhook endpoints and subscriptions - [WebhookEvents](#process-webhook-events): Manage webhook events - [WebhookEventAttempts](#query-failed-webhook-event-delivery-attempts-information): Query failed webhook event deliveries @@ -388,7 +391,7 @@ puts balance ## Create transfers -You can also create transfers in the SDK (TED/Pix). +You can also create transfers in the SDK (TED/Pix) and configure transfer behavior according to its rules. ```ruby require('starkbank') @@ -414,7 +417,12 @@ transfers = StarkBank::Transfer.create( tax_id: '012.345.678-90', name: 'Jon Snow', scheduled: Time.now + 24 * 3600, - tags: [] + tags: [], + rules: [ + StarkBank::Transfer::Rule.new( + key: 'resendingLimit', # Set maximum number of retries if Transfer fails due to systemic issues at the receiver bank + value: 5 # Our resending limit is 10 by default + ] ) ] ) @@ -735,6 +743,75 @@ payment = StarkBank::Invoice.payment('5155165527080960'); puts payment ``` +## Create DynamicBrcodes + +You can create simplified dynamic QR Codes to receive money using Pix transactions. +When a DynamicBrcode is paid, a Deposit is created with the tags parameter containing the character “dynamic-brcode/” followed by the DynamicBrcode’s uuid "dynamic-brcode/{uuid}" for conciliation. + +The differences between an Invoice and the DynamicBrcode are the following: + +| | Invoice | DynamicBrcode | +|-----------------------|:-------:|:-------------:| +| Expiration | ✓ | ✓ | +| Can only be paid once | ✓ | ✓ | +| Due, fine and fee | ✓ | X | +| Discount | ✓ | X | +| Description | ✓ | X | +| Can be updated | ✓ | X | + +**Note:** In order to check if a BR code has expired, you must first calculate its expiration date (add the expiration to the creation date). +**Note:** To know if the BR code has been paid, you need to query your Deposits by the tag "dynamic-brcode/{uuid}" to check if it has been paid. + +```ruby +require('starkbank') + +brcodes = StarkBank::DynamicBrcode.create([ + StarkBank::DynamicBrcode.new( + amount: 23571, # R$ 235,71 + expiration: 3600 * 3 + ), + StarkBank::DynamicBrcode( + amount: 23571, # R$ 235,71 + expiration: 3600 * 3 + ) +]) + +brcodes.each do |brcode| + puts brcode +end +``` + +**Note**: Instead of using DynamicBrcode objects, you can also pass each brcode element in dictionary format + +## Get a DynamicBrcode + +After its creation, information on a DynamicBrcode may be retrieved by its uuid. + +```ruby +require('starkbank') + +brcode = StarkBank::DynamicBrcode.get("bb9cd43ea6f4403391bf7ef6aa876600") + +puts brcode +``` + +## Query DynamicBrcodes + +You can get a list of created DynamicBrcodes given some filters. + +```ruby +require('starkbank') + +brcodes = StarkBank::DynamicBrcode.query( + after: "2023-01-01", + before: "2023-03-01" +) + +brcodes.each do |brcode| + puts brcode +end +``` + ## Query deposits You can get a list of created deposits given some filters. @@ -996,10 +1073,15 @@ payments = StarkBank::BrcodePayment.create( [ StarkBank::BrcodePayment.new( line: '00020126580014br.gov.bcb.pix0136a629532e-7693-4846-852d-1bbff817b5a8520400005303986540510.005802BR5908T'Challa6009Sao Paulo62090505123456304B14A', - tax_id: '012.345.678-90', + tax_id:"012.345.678-90", scheduled: Time.now, - description: 'take my money', - tags: %w[take my money] + description: "take my money", + tags: %w[take my money], + rules: [ + StarkBank::BrcodePayment::Rule.new( + key: "resendingLimit", # Set maximum number of retries if BrcodePayment fails due to systemic issues at the receiver bank + value: 5 # Our resending limit is 10 by default + ] ) ] ) @@ -1009,6 +1091,7 @@ payments.each do |payment| end ``` +**Note**: You can also configure payment behavior according to its rules **Note**: Instead of using BrcodePayment objects, you can also pass each payment element in hash format ## Get a BR Code payment @@ -1387,9 +1470,9 @@ You can get a specific tax payment by its id: ```ruby require('starkbank') -tax_payment = StarkBank::TaxPayment.get('5155165527080960') +payment = StarkBank::TaxPayment.get('5155165527080960') -puts tax_payment +puts payment ``` ## Get tax payment PDF @@ -1415,9 +1498,9 @@ Note that this is not possible if it has been processed already. ```ruby require('starkbank') -tax_payment = StarkBank::TaxPayment.delete('5155165527080960') +payment = StarkBank::TaxPayment.delete('5155165527080960') -puts tax_payment +puts payment ``` ## Query tax payment logs @@ -1450,6 +1533,120 @@ puts log resource and routes, which are all analogous to the TaxPayment resource. The ones we currently support are: - DarfPayment, for DARFs +## Create DARF payment + +If you want to manually pay DARFs without barcodes, you may create DarfPayments: + +```ruby +require('starkbank') + +payments = StarkBank::DarfPayment.create( + [ + StarkBank::DarfPayment.new( + revenue_code: "1240", + tax_id: "012.345.678-90", + competence: "2023-09-01", + reference_number: "2340978970", + nominal_amount: 1234, + fine_amount: 12, + interest_amount: 34, + due: "2023-03-05", + scheduled: "2023-03-05", + tags: ["DARF", "making money"], + description: "take my money", + ) + ] +) + +payments.each do |payment| + puts payment +end +``` + +**Note**: Instead of using DarfPayment objects, you can also pass each payment element in dictionary format + +## Query DARF payments + +To search for DARF payments using filters, run: + +```ruby +require('starkbank') + +payments = StarkBank::DarfPayment.query( + tags: ["darf", "july"] +).to_a + +payments.each do |payment| + puts payment +end +``` + +## Get DARF payment + +You can get a specific DARF payment by its id: + +```ruby +require('starkbank') + +payment = StarkBank::DarfPayment.get('5155165527080960') + +puts payment +``` + +## Get DARF payment PDF + +After its creation, a DARF payment PDF may also be retrieved by its id. + +```ruby +require('starkbank') + +pdf = StarkBank::DarfPayment.pdf('5155165527080960') +File.binwrite('tax_payment.pdf', pdf) +``` + +Be careful not to accidentally enforce any encoding on the raw pdf content, +as it may yield abnormal results in the final file, such as missing images +and strange characters. + +## Delete DARF payment + +You can also cancel a DARF payment by its id. +Note that this is not possible if it has been processed already. + +```ruby +require('starkbank') + +payment = StarkBank::DarfPayment.delete('5155165527080960') + +puts payment +``` + +## Query DARF payment logs + +You can search for payment logs by specifying filters. Use this to understand each payment life cycle. + +```ruby +require('starkbank') + +logs = StarkBank::DarfPayment::Log.query(limit: 5).to_a + +logs.each do |log| + puts log +end +``` + +## Get DARF payment log + +If you want to get a specific payment log by its id, just run: + +```ruby +require('starkbank') + +log = StarkBank::DarfPayment::Log.get('1902837198237992') + +puts log +``` + ## Preview payment information before executing the payment You can preview multiple types of payment to confirm any information before actually paying. @@ -1754,11 +1951,29 @@ You can update a specific Workspace by its id. ```ruby require('starkbank') +picture = open("path/to/picture.png", "rb").read() + updatedWorkspace = StarkBank::Workspace.update( workspace.ID, username: 'new-username-test', name: 'Updated workspace test', - allowed_tax_ids: ['20.018.183/0001-80'] + allowed_tax_ids: ['20.018.183/0001-80'], + picture: picture, + picture_type: "image/png" +) + +puts updatedWorkspace +``` + +You can also block a specific Workspace by its id. + +```ruby +require('starkbank') + +updatedWorkspace = StarkBank::Workspace.update( + workspace.ID, + status: "blocked", + user:starkbank.Organization.replace(organization, workspace.ID) ) puts updatedWorkspace diff --git a/lib/balance/balance.rb b/lib/balance/balance.rb index 346a6e0..e21f69a 100644 --- a/lib/balance/balance.rb +++ b/lib/balance/balance.rb @@ -13,10 +13,10 @@ module StarkBank # can be retrieved to see the available information. # # ## Attributes (return-only): - # - id [string, default nil]: unique id returned when Balance is created. ex: '5656565656565656' - # - amount [integer, default nil]: current balance amount of the workspace in cents. ex: 200 (= R$ 2.00) - # - currency [string, default nil]: currency of the current workspace. Expect others to be added eventually. ex:'BRL' - # - updated [DateTime, default nil]: update datetime for the balance. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) + # - id [string]: unique id returned when Balance is created. ex: '5656565656565656' + # - amount [integer]: current balance amount of the workspace in cents. ex: 200 (= R$ 2.00) + # - currency [string]: currency of the current workspace. Expect others to be added eventually. ex:'BRL' + # - updated [DateTime]: update datetime for the balance. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) class Balance < StarkCore::Utils::Resource attr_reader :amount, :currency, :updated def initialize(amount:, currency:, updated:, id:) diff --git a/lib/boleto/boleto.rb b/lib/boleto/boleto.rb index 671db68..18adc08 100644 --- a/lib/boleto/boleto.rb +++ b/lib/boleto/boleto.rb @@ -34,21 +34,22 @@ module StarkBank # - tags [list of strings]: list of strings for tagging # # ## Attributes (return-only): - # - id [string, default nil]: unique id returned when Boleto is created. ex: '5656565656565656' - # - fee [integer, default nil]: fee charged when Boleto is paid. ex: 200 (= R$ 2.00) - # - line [string, default nil]: generated Boleto line for payment. ex: '34191.09008 63571.277308 71444.640008 5 81960000000062' - # - bar_code [string, default nil]: generated Boleto bar-code for payment. ex: '34195819600000000621090063571277307144464000' - # - transaction_ids [list of strings, default nil]: ledger transaction ids linked to this boleto. ex: ['19827356981273'] - # - status [string, default nil]: current Boleto status. ex: 'registered' or 'paid' - # - created [DateTime, default nil]: creation datetime for the Boleto. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - # - our_number [string, default nil]: Reference number registered at the settlement bank. ex:'10131474' + # - id [string]: unique id returned when Boleto is created. ex: '5656565656565656' + # - fee [integer]: fee charged when Boleto is paid. ex: 200 (= R$ 2.00) + # - line [string]: generated Boleto line for payment. ex: '34191.09008 63571.277308 71444.640008 5 81960000000062' + # - bar_code [string]: generated Boleto bar-code for payment. ex: '34195819600000000621090063571277307144464000' + # - transaction_ids [list of strings]: ledger transaction ids linked to this boleto. ex: ['19827356981273'] + # - workspace_id [string]: ID of the Workspace where this Boleto was generated. ex: "4545454545454545" + # - status [string]: current Boleto status. ex: 'registered' or 'paid' + # - created [DateTime]: creation datetime for the Boleto. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) + # - our_number [string]: Reference number registered at the settlement bank. ex:'10131474' class Boleto < StarkCore::Utils::Resource - attr_reader :amount, :name, :tax_id, :street_line_1, :street_line_2, :district, :city, :state_code, :zip_code, :due, :fine, :interest, :overdue_limit, :receiver_name, :receiver_tax_id, :tags, :descriptions, :discounts, :id, :fee, :line, :bar_code, :status, :transaction_ids, :created, :our_number + attr_reader :amount, :name, :tax_id, :street_line_1, :street_line_2, :district, :city, :state_code, :zip_code, :due, :fine, :interest, :overdue_limit, :receiver_name, :receiver_tax_id, :tags, :descriptions, :discounts, :id, :fee, :line, :bar_code, :status, :transaction_ids, :workspace_id, :created, :our_number def initialize( amount:, name:, tax_id:, street_line_1:, street_line_2:, district:, city:, state_code:, zip_code:, due: nil, fine: nil, interest: nil, overdue_limit: nil, receiver_name: nil, receiver_tax_id: nil, tags: nil, descriptions: nil, discounts: nil, id: nil, fee: nil, line: nil, bar_code: nil, - status: nil, transaction_ids: nil, created: nil, our_number: nil + status: nil, transaction_ids: nil, workspace_id: nil, created: nil, our_number: nil ) super(id) @amount = amount @@ -74,6 +75,7 @@ def initialize( @bar_code = bar_code @status = status @transaction_ids = transaction_ids + @workspace_id = workspace_id @created = StarkCore::Utils::Checks.check_datetime(created) @our_number = our_number end @@ -236,6 +238,7 @@ def self.resource bar_code: json['bar_code'], status: json['status'], transaction_ids: json['transaction_ids'], + workspace_id: json['workspace_id'], created: json['created'], our_number: json['our_number'] ) diff --git a/lib/boleto/log.rb b/lib/boleto/log.rb index d5d56b6..15e454b 100644 --- a/lib/boleto/log.rb +++ b/lib/boleto/log.rb @@ -14,7 +14,7 @@ class Boleto # user, but it can be retrieved to check additional information # on the Boleto. # - # ## Attributes: + # ## Attributes (return-only): # - id [string]: unique id returned when the log is created. ex: '5656565656565656' # - boleto [Boleto]: Boleto entity to which the log refers to. # - errors [list of strings]: list of errors linked to this Boleto event diff --git a/lib/boleto_holmes/boleto_holmes.rb b/lib/boleto_holmes/boleto_holmes.rb index 27abffb..c99e58d 100644 --- a/lib/boleto_holmes/boleto_holmes.rb +++ b/lib/boleto_holmes/boleto_holmes.rb @@ -18,11 +18,11 @@ module StarkBank # - tags [list of strings]: list of strings for tagging # # ## Attributes (return-only): - # - id [string, default nil]: unique id returned when holmes is created. ex: '5656565656565656' - # - status [string, default nil]: current holmes status. ex: 'solving' or 'solved' - # - result [string, default nil]: result of boleto status investigation. ex: 'paid' or 'cancelled' - # - created [DateTime, default nil]: creation datetime for the Boleto. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - # - updated [DateTime, default nil]: latest update datetime for the holmes. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) + # - id [string]: unique id returned when holmes is created. ex: '5656565656565656' + # - status [string]: current holmes status. ex: 'solving' or 'solved' + # - result [string]: result of boleto status investigation. ex: 'paid' or 'cancelled' + # - created [DateTime]: creation datetime for the Boleto. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) + # - updated [DateTime]: latest update datetime for the holmes. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) class BoletoHolmes < StarkCore::Utils::Resource attr_reader :boleto_id, :tags, :id, :status, :result, :created, :updated def initialize( diff --git a/lib/boleto_holmes/log.rb b/lib/boleto_holmes/log.rb index 84036fa..02dd8dd 100644 --- a/lib/boleto_holmes/log.rb +++ b/lib/boleto_holmes/log.rb @@ -14,18 +14,20 @@ class BoletoHolmes # user, but it can be retrieved to check additional information # on the BoletoHolmes. # - # ## Attributes: + # ## Attributes (return-only): # - id [string]: unique id returned when the log is created. ex: '5656565656565656' # - holmes [BoletoHolmes]: BoletoHolmes entity to which the log refers to. # - type [string]: type of the Boleto event which triggered the log creation. ex: 'registered' or 'paid' # - created [DateTime]: creation datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) + # - updated [DateTime]: latest update datetime for the log. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) class Log < StarkCore::Utils::Resource - attr_reader :id, :holmes, :type, :created - def initialize(id:, holmes:, type:, created:) + attr_reader :id, :holmes, :type, :created, :updated + def initialize(id:, holmes:, type:, created:, updated:) super(id) @holmes = holmes @type = type @created = StarkCore::Utils::Checks.check_datetime(created) + @updated = StarkCore::Utils::Checks.check_datetime(updated) end # # Retrieve a specific Log @@ -112,7 +114,8 @@ def self.resource id: json['id'], holmes: StarkCore::Utils::API.from_api_json(holmes_maker, json['holmes']), type: json['type'], - created: json['created'] + created: json['created'], + updated: json['updated'] ) } } diff --git a/lib/boleto_payment/boleto_payment.rb b/lib/boleto_payment/boleto_payment.rb index 9b8d77f..ae4bbfa 100644 --- a/lib/boleto_payment/boleto_payment.rb +++ b/lib/boleto_payment/boleto_payment.rb @@ -20,28 +20,30 @@ module StarkBank # - description [string]: Text to be displayed in your statement (min. 10 characters). ex: 'payment ABC' # # ## Parameters (optional): + # - amount [int, default nil]: amount automatically calculated from line or bar_code. ex: 23456 (= R$ 234.56) # - scheduled [Date, DateTime, Time or string, default today]: payment scheduled date. ex: Date.new(2020, 3, 10) # - tags [list of strings]: 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]: amount automatically calculated from line or bar_code. ex: 23456 (= R$ 234.56) - # - fee [integer, default nil]: fee charged when the boleto payment is created. ex: 200 (= R$ 2.00) - # - created [DateTime, default nil]: creation datetime for the payment. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) + # - id [string]: unique id returned when payment is created. ex: '5656565656565656' + # - status [string]: current payment status. ex: 'success' or 'failed' + # - fee [integer]: fee charged when the boleto payment is created. ex: 200 (= R$ 2.00) + # - transaction_ids [list of strings]: ledger transaction ids linked to this BoletoPayment. ex: ["19827356981273"] + # - created [DateTime]: creation datetime for the payment. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) class BoletoPayment < StarkCore::Utils::Resource - attr_reader :tax_id, :description, :line, :bar_code, :scheduled, :tags, :id, :status, :amount, :fee, :created - def initialize(tax_id:, description:, line: nil, bar_code: nil, scheduled: nil, tags: nil, id: nil, status: nil, amount: nil, fee: nil, created: nil) + attr_reader :tax_id, :description, :line, :bar_code, :scheduled, :tags, :id, :status, :amount, :fee, :transaction_ids, :created + def initialize(tax_id:, description:, line: nil, bar_code: nil, scheduled: nil, tags: nil, id: nil, status: nil, amount: nil, fee: nil, transaction_ids: nil, created: nil) super(id) @tax_id = tax_id @description = description @line = line @bar_code = bar_code + @amount = amount @scheduled = StarkCore::Utils::Checks.check_date(scheduled) @tags = tags @status = status - @amount = amount @fee = fee + @transaction_ids = transaction_ids @created = StarkCore::Utils::Checks.check_datetime(created) end @@ -181,11 +183,12 @@ def self.resource description: json['description'], line: json['line'], bar_code: json['bar_code'], + amount: json['amount'], scheduled: json['scheduled'], tags: json['tags'], status: json['status'], - amount: json['amount'], fee: json['fee'], + transaction_ids: json['transaction_ids'], created: json['created'] ) } diff --git a/lib/boleto_payment/log.rb b/lib/boleto_payment/log.rb index 11656a0..0d177be 100644 --- a/lib/boleto_payment/log.rb +++ b/lib/boleto_payment/log.rb @@ -14,7 +14,7 @@ class BoletoPayment # user, but it can be retrieved to check additional information # on the BoletoPayment. # - # ## Attributes: + # ## Attributes (return-only): # - id [string]: unique id returned when the log is created. ex: '5656565656565656' # - payment [BoletoPayment]: BoletoPayment entity to which the log refers to. # - errors [list of strings]: list of errors linked to this BoletoPayment event. diff --git a/lib/brcode_payment/brcode_payment.rb b/lib/brcode_payment/brcode_payment.rb index 156620c..f6a85e2 100644 --- a/lib/brcode_payment/brcode_payment.rb +++ b/lib/brcode_payment/brcode_payment.rb @@ -22,19 +22,20 @@ module StarkBank # ## Parameters (optional): # - scheduled [datetime.date, datetime.datetime or string, default now]: payment scheduled date or datetime. ex: datetime.datetime(2020, 3, 10, 15, 17, 3) # - tags [list of strings, default nil]: list of strings for tagging + # - rules [list of BrcodePayment::Rules, default []]: list of BrcodePayment::Rule objects for modifying transfer behavior. ex: [BrcodePayment::Rule(key: "resendingLimit", value: 5)] # # ## Attributes (return-only): - # - id [string, default nil]: unique id returned when payment is created. ex: '5656565656565656' - # - name [string, nil]: receiver name. ex: 'Jon Snow' - # - status [string, default nil]: current payment status. ex: 'success' or 'failed' - # - type [string, default nil]: brcode type. ex: 'static' or 'dynamic' - # - transaction_ids [list of strings, default nil]: ledger transaction ids linked to this payment. ex: ['19827356981273'] - # - fee [integer, default nil]: fee charged when the brcode payment is created. ex: 200 (= R$ 2.00) - # - updated [datetime.datetime, default nil]: latest update datetime for the payment. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0) - # - created [datetime.datetime, default nil]: creation datetime for the payment. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0) + # - id [string]: unique id returned when payment is created. ex: '5656565656565656' + # - name [string]: receiver name. ex: 'Jon Snow' + # - status [string]: current payment status. ex: 'success' or 'failed' + # - type [string]: brcode type. ex: 'static' or 'dynamic' + # - transaction_ids [list of strings]: ledger transaction ids linked to this payment. ex: ['19827356981273'] + # - fee [integer]: fee charged when the brcode payment is created. ex: 200 (= R$ 2.00) + # - updated [datetime.datetime]: latest update datetime for the payment. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0) + # - created [datetime.datetime]: creation datetime for the payment. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0) class BrcodePayment < StarkCore::Utils::Resource - attr_reader :brcode, :tax_id, :description, :amount, :scheduled, :tags, :id, :name, :status, :type, :transaction_ids, :fee, :updated, :created - def initialize(brcode:, tax_id:, description:, amount: nil, scheduled: nil, tags: nil, id: nil, name: nil, status: nil, type: nil, transaction_ids: nil, fee: nil, updated: nil, created: nil) + attr_reader :brcode, :tax_id, :description, :amount, :scheduled, :tags, :rules, :id, :name, :status, :type, :transaction_ids, :fee, :updated, :created + def initialize(brcode:, tax_id:, description:, amount: nil, scheduled: nil, tags: nil, rules: nil, id: nil, name: nil, status: nil, type: nil, transaction_ids: nil, fee: nil, updated: nil, created: nil) super(id) @brcode = brcode @tax_id = tax_id @@ -42,6 +43,7 @@ def initialize(brcode:, tax_id:, description:, amount: nil, scheduled: nil, tags @amount = amount @scheduled = StarkCore::Utils::Checks.check_date_or_datetime(scheduled) @tags = tags + @rules = StarkBank::BrcodePayment::Rule.parse_rules(rules) @name = name @status = status @type = type @@ -190,6 +192,7 @@ def self.resource amount: json['amount'], scheduled: json['scheduled'], tags: json['tags'], + rules: json['rules'], id: json['id'], name: json['name'], status: json['status'], diff --git a/lib/brcode_payment/log.rb b/lib/brcode_payment/log.rb index 8b37ba4..24f04ac 100644 --- a/lib/brcode_payment/log.rb +++ b/lib/brcode_payment/log.rb @@ -14,7 +14,7 @@ class BrcodePayment # user, but it can be retrieved to check additional information # on the BrcodePayment. # - # ## Attributes: + # ## Attributes (return-only): # - id [string]: unique id returned when the log is created. ex: '5656565656565656' # - payment [BrcodePayment]: BrcodePayment entity to which the log refers to. # - errors [list of strings]: list of errors linked to this BrcodePayment event. diff --git a/lib/brcode_payment/rule.rb b/lib/brcode_payment/rule.rb new file mode 100644 index 0000000..7579fd5 --- /dev/null +++ b/lib/brcode_payment/rule.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require_relative('../utils/rest') + + +module StarkBank + class BrcodePayment + # # BrcodePayment::Rule object + # + # The BrcodePayment::Rule object modifies the behavior of BrcodePayment objects when passed as an argument upon their creation. + # + # ## Parameters (required): + # - key [string]: Rule to be customized, describes what BrcodePayment behavior will be altered. ex: "resendingLimit" + # - value [integer]: Value of the rule. ex: 5 + 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::BrcodePayment::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/brcode_preview/brcode_preview.rb b/lib/brcode_preview/brcode_preview.rb deleted file mode 100644 index 254fd3c..0000000 --- a/lib/brcode_preview/brcode_preview.rb +++ /dev/null @@ -1,79 +0,0 @@ -# frozen_string_literal: true - -require('starkcore') -require_relative('../utils/rest') - - -module StarkBank - # # BrcodePreview object - # - # A BrcodePreview is used to get information from a BR Code you received to check the informations before paying it. - # - # ## Attributes (return-only): - # - status [string]: Payment status. ex: 'active', 'paid', 'canceled' or 'unknown' - # - name [string]: Payment receiver name. ex: 'Tony Stark' - # - tax_id [string]: Payment receiver tax ID. ex: '012.345.678-90' - # - bank_code [string]: Payment receiver bank code. ex: '20018183' - # - branch_code [string]: Payment receiver branch code. ex: '0001' - # - account_number [string]: Payment receiver account number. ex: '1234567' - # - account_type [string]: Payment receiver account type. ex: 'checking' - # - allow_change [bool]: If True, the payment is able to receive amounts that are diferent from the nominal one. ex: True or False - # - amount [integer]: Value in cents that this payment is expecting to receive. If 0, any value is accepted. ex: 123 (= R$1,23) - # - reconciliation_id [string]: Reconciliation ID linked to this payment. ex: 'txId', 'payment-123' - class BrcodePreview < StarkCore::Utils::Resource - attr_reader :status, :name, :tax_id, :bank_code, :branch_code, :account_number, :account_type, :allow_change, :amount, :reconciliation_id - def initialize(status:, name:, tax_id:, bank_code:, branch_code:, account_number:, account_type:, allow_change:, amount:, reconciliation_id:) - @status = status - @name = name - @tax_id = tax_id - @bank_code = bank_code - @branch_code = branch_code - @account_number = account_number - @account_type = account_type - @allow_change = allow_change - @amount = amount - @reconciliation_id = reconciliation_id - end - - # # BrcodePreview is DEPRECATED: Please use PaymentPreview instead. - # Retrieve BrcodePreviews - # - # Receive a generator of BrcodePreview objects previously created in the Stark Bank API - # - # ## Parameters (optional): - # - brcodes [list of strings]: List of brcodes to preview. ex: %w[00020126580014br.gov.bcb.pix0136a629532e-7693-4846-852d-1bbff817b5a8520400005303986540510.005802BR5908T'Challa6009Sao Paulo62090505123456304B14A] - # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call - # - # ## Return: - # - generator of BrcodePreview objects with updated attributes - def self.query(limit: nil, brcodes: nil, user: nil) - warn "[DEPRECATION] `BrcodePreview` is deprecated. Please use `PaymentPreview` instead." - StarkBank::Utils::Rest.get_stream( - user: user, - limit: nil, - brcodes: brcodes, - **resource - ) - end - - def self.resource - { - resource_name: 'BrcodePreview', - resource_maker: proc { |json| - BrcodePreview.new( - status: json['status'], - name: json['name'], - tax_id: json['tax_id'], - bank_code: json['bank_code'], - branch_code: json['branch_code'], - account_number: json['account_number'], - account_type: json['account_type'], - allow_change: json['allow_change'], - amount: json['amount'], - reconciliation_id: json['reconciliation_id'] - ) - } - } - end - end -end diff --git a/lib/darf_payment/darf_payment.rb b/lib/darf_payment/darf_payment.rb index 100541f..5f3213c 100644 --- a/lib/darf_payment/darf_payment.rb +++ b/lib/darf_payment/darf_payment.rb @@ -13,7 +13,6 @@ module StarkBank # # ## 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) @@ -28,18 +27,19 @@ module StarkBank # - 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) + # - id [string]: unique id returned when payment is created. ex: '5656565656565656' + # - status [string]: current payment status. ex: 'success' or 'failed' + # - amount [int]: Total amount due calculated from other amounts. ex: 24146 (= R$ 241.46) + # - fee [integer]: fee charged when the DarfPayment is processed. ex: 0 (= R$ 0.00) + # - transaction_ids [list of strings]: ledger transaction ids linked to this DarfPayment. ex: ["19827356981273"] + # - 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 DarfPayment < StarkCore::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 + :due, :description, :tags, :scheduled, :status, :amount, :nominal_amount, :fee, :transaction_ids, :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 + tags: nil, scheduled: nil, status: nil, amount: nil, nominal_amount: nil, fee: nil, transaction_ids: nil, updated: nil, created: nil ) super(id) @revenue_code = revenue_code @@ -56,6 +56,7 @@ def initialize( @amount = amount @nominal_amount = nominal_amount @fee = fee + @transaction_ids = @transaction_ids @updated = StarkCore::Utils::Checks.check_datetime(updated) @created = StarkCore::Utils::Checks.check_datetime(created) end @@ -209,6 +210,7 @@ def self.resource nominal_amount: json['nominal_amount'], fee: json['fee'], updated: json['updated'], + transaction_ids: json['transaction_ids'], created: json['created'], ) } diff --git a/lib/darf_payment/log.rb b/lib/darf_payment/log.rb index 5ccef97..032840e 100644 --- a/lib/darf_payment/log.rb +++ b/lib/darf_payment/log.rb @@ -14,7 +14,7 @@ class DarfPayment # user, but it can be retrieved to check additional information # on the DarfPayment. # - # ## Attributes: + # ## Attributes (return-only): # - 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 diff --git a/lib/deposit/log.rb b/lib/deposit/log.rb index d72cd3f..d7c720f 100644 --- a/lib/deposit/log.rb +++ b/lib/deposit/log.rb @@ -14,7 +14,7 @@ class Deposit # user, but it can be retrieved to check additional information # on the Deposit. # - # ## Attributes: + # ## Attributes (return-only): # - id [string]: unique id returned when the log is created. ex: '5656565656565656' # - deposit [Deposit]: Deposit entity to which the log refers to. # - errors [list of strings]: list of errors linked to this Deposit event diff --git a/lib/dict_key/dict_key.rb b/lib/dict_key/dict_key.rb index e23d740..8aaed62 100644 --- a/lib/dict_key/dict_key.rb +++ b/lib/dict_key/dict_key.rb @@ -6,25 +6,25 @@ module StarkBank # # DictKey object # - # DictKey represents a PIX key registered in Bacen's DICT system. + # DictKey represents a Pix key registered in Bacen's DICT system. # - # ## Parameters (required): - # - id [string]: DictKey object unique id and PIX key itself. ex: 'tony@starkbank.com', '722.461.430-04', '20.018.183/0001-80', '+5511988887777', 'b6295ee1-f054-47d1-9e90-ee57b74f60d9' + # ## Parameters (optional): + # - id [string]: DictKey object unique id and Pix key itself. ex: 'tony@starkbank.com', '722.461.430-04', '20.018.183/0001-80', '+5511988887777', 'b6295ee1-f054-47d1-9e90-ee57b74f60d9' # # ## Attributes (return-only): - # - type [string, default nil]: DICT key type. ex: 'email', 'cpf', 'cnpj', 'phone' or 'evp' - # - name [string, default nil]: account owner full name. ex: 'Tony Stark' - # - tax_id [string, default nil]: key owner tax ID (CNPJ or masked CPF). ex: '***.345.678-**' or '20.018.183/0001-80' - # - owner_type [string, default nil]: DICT key owner type. ex 'naturalPerson' or 'legalPerson' - # - bank_name [string, default nil]: bank name associated with the DICT key. ex: 'Stark Bank' - # - ispb [string, default nil]: bank ISPB associated with the DICT key. ex: '20018183' - # - branch_code [string, default nil]: bank account branch code associated with the DICT key. ex: '9585' - # - account_number [string, default nil]: bank account number associated with the DICT key. ex: '9828282578010513' - # - account_type [string, default nil]: bank account type associated with the DICT key. ex: 'checking', 'saving', 'salary' or 'payment' - # - status [string, default nil]: current DICT key status. ex: 'created', 'registered', 'canceled' or 'failed' - # - account_created [DateTime or string, default nil]: creation datetime of the bank account associated with the DICT key. ex: '2020-11-05T14:55:08.812665+00:00' - # - owned [DateTime or string, default nil]: datetime since when the current owner hold this DICT key. ex : '2020-11-05T14:55:08.812665+00:00' - # - created [DateTime or string, default nil]: creation datetime for the DICT key. ex: '2020-03-10 10:30:00.000' + # - type [string]: DICT key type. ex: 'email', 'cpf', 'cnpj', 'phone' or 'evp' + # - name [string]: account owner full name. ex: 'Tony Stark' + # - tax_id [string]: key owner tax ID (CNPJ or masked CPF). ex: '***.345.678-**' or '20.018.183/0001-80' + # - owner_type [string]: DICT key owner type. ex 'naturalPerson' or 'legalPerson' + # - bank_name [string]: bank name associated with the DICT key. ex: 'Stark Bank' + # - ispb [string]: bank ISPB associated with the DICT key. ex: '20018183' + # - branch_code [string]: bank account branch code associated with the DICT key. ex: '9585' + # - account_number [string]: bank account number associated with the DICT key. ex: '9828282578010513' + # - account_type [string]: bank account type associated with the DICT key. ex: 'checking', 'saving', 'salary' or 'payment' + # - status [string]: current DICT key status. ex: 'created', 'registered', 'canceled' or 'failed' + # - account_created [DateTime or string]: creation datetime of the bank account associated with the DICT key. ex: '2020-11-05T14:55:08.812665+00:00' + # - owned [DateTime or string]: datetime since when the current owner hold this DICT key. ex : '2020-11-05T14:55:08.812665+00:00' + # - created [DateTime or string]: creation datetime for the DICT key. ex: '2020-03-10 10:30:00.000' class DictKey < StarkCore::Utils::Resource attr_reader :id, :type, :name, :tax_id, :owner_type, :bank_name, :ispb, :branch_code, :account_number, :account_type, :status, :account_created, :owned, :created def initialize( @@ -52,7 +52,7 @@ def initialize( # Receive a single DictKey object by passing its id # # ## Parameters (required): - # - id [string]: DictKey object unique id and PIX key itself. ex: 'tony@starkbank.com', '722.461.430-04', '20.018.183/0001-80', '+5511988887777', 'b6295ee1-f054-47d1-9e90-ee57b74f60d9' + # - id [string]: DictKey object unique id and Pix key itself. ex: 'tony@starkbank.com', '722.461.430-04', '20.018.183/0001-80', '+5511988887777', 'b6295ee1-f054-47d1-9e90-ee57b74f60d9' # # ## Parameters (optional): # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call diff --git a/lib/dynamic_brcode/dynamic_brcode.rb b/lib/dynamic_brcode/dynamic_brcode.rb new file mode 100644 index 0000000..3afb4ab --- /dev/null +++ b/lib/dynamic_brcode/dynamic_brcode.rb @@ -0,0 +1,155 @@ +# frozen_string_literal: true + +require('starkcore') +require_relative('../utils/rest') + + +module StarkBank + # # DynamicBrcode object + # + # When you initialize a DynamicBrcode, the entity will not be automatically + # sent to the Stark Bank API. The 'create' function sends the objects + # to the Stark Bank API and returns the list of created objects. + # + # DynamicBrcodes are conciliated BR Codes that can be used to receive Pix transactions in a convenient way. + # When a DynamicBrcode is paid, a Deposit is created with the tags parameter containing the character “dynamic-brcode/” followed by the DynamicBrcode’s uuid "dynamic-brcode/{uuid}" for conciliation. + # Additionally, all tags passed on the DynamicBrcode will be transferred to the respective Deposit resource. + # + # ## Parameters (required): + # - amount [integer]: DynamicBrcode value in cents. Minimum = 0 (any value will be accepted). ex: 1234 (= R$ 12.34) + # + # ## Parameters (optional): + # - expiration [integer, default 3600 (1 hour)]: time interval in seconds between due date and expiration date. ex 123456789 + # - tags [list of strings, default []]: list of strings for tagging, these will be passed to the respective DynamicBrcode resource when paid + # + # ## Attributes (return-only): + # - id [string]: id returned on creation, this is the BR code. ex: "00020126360014br.gov.bcb.pix0114+552840092118152040000530398654040.095802BR5915Jamie Lannister6009Sao Paulo620705038566304FC6C" + # - uuid [string]: unique uuid returned when the DynamicBrcode is created. ex: "4e2eab725ddd495f9c98ffd97440702d" + # - picture_url [string]: public QR Code (png image) URL. "https://sandbox.api.starkbank.com/v2/dynamic-brcode/d3ebb1bd92024df1ab6e5a353ee799a4.png" + # - updated [DateTime]: latest update datetime for the DynamicBrcode. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) + # - created [DateTime]: creation datetime for the DynamicBrcode. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) + class DynamicBrcode < StarkCore::Utils::Resource + attr_reader :amount, :expiration, :tags, :id, :uuid, :picture_url, :updated, :created + def initialize( + amount:, expiration: nil, tags: nil, id: nil, uuid: nil, picture_url: nil, updated: nil, created: nil + ) + super(id) + @amount = amount + @expiration = expiration + @tags = tags + @uuid = uuid + @picture_url = picture_url + @created = StarkCore::Utils::Checks.check_datetime(created) + @updated = StarkCore::Utils::Checks.check_datetime(updated) + end + + # # Create DynamicBrcode + # + # Send a list of DynamicBrcode objects for creation in the Stark Bank API + # + # ## Parameters (required): + # - brcodes [list of DynamicBrcode objects]: list of DynamicBrcode 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 DynamicBrcode objects with updated attributes + def self.create(brcodes, user: nil) + StarkBank::Utils::Rest.post(entities: brcodes, user: user, **resource) + end + + # # Retrieve a specific DynamicBrcode + # + # Receive a single DynamicBrcode object previously created in the Stark Bank API by its uuid + # + # ## Parameters (required): + # - uuid [string]: object unique uuid. ex: "901e71f2447c43c886f58366a5432c4b" + # + # ## Parameters (optional): + # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call + # + # ## Return: + # - DynamicBrcode object with updated attributes + def self.get(uuid, user: nil) + StarkBank::Utils::Rest.get_id(id: uuid, user: user, **resource) + end + + # # Retrieve DynamicBrcodes + # + # Receive a generator of DynamicBrcode 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'] + # - uuids [list of strings, default nil]: list of uuids to filter retrieved objects. ex: ["901e71f2447c43c886f58366a5432c4b", "4e2eab725ddd495f9c98ffd97440702d"] + # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call + # + # ## Return: + # - generator of DynamicBrcode objects with updated attributes + def self.query(limit: nil, after: nil, before: nil, tags: nil, uuids: nil, user: nil) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) + StarkBank::Utils::Rest.get_stream( + limit: limit, + after: after, + before: before, + tags: tags, + uuids: uuids, + user: user, + **resource + ) + end + + # # Retrieve paged DynamicBrcodes + # + # Receive a list of up to 100 DynamicBrcode 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'] + # - uuids [list of strings, default nil]: list of uuids to filter retrieved objects. ex: ["901e71f2447c43c886f58366a5432c4b", "4e2eab725ddd495f9c98ffd97440702d"] + # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call + # + # ## Return: + # - list of DynamicBrcode objects with updated attributes and cursor to retrieve the next page of DynamicBrcode objects + def self.page(cursor: nil, limit: nil, after: nil, before: nil, tags: nil, uuids: nil, user: nil) + after = StarkCore::Utils::Checks.check_date(after) + before = StarkCore::Utils::Checks.check_date(before) + return StarkBank::Utils::Rest.get_page( + cursor: cursor, + limit: limit, + after: after, + before: before, + tags: tags, + uuids: uuids, + user: user, + **resource + ) + end + + def self.resource + { + resource_name: 'DynamicBrcode', + resource_maker: proc { |json| + DynamicBrcode.new( + id: json['id'], + amount: json['amount'], + expiration: json['expiration'], + tags: json['tags'], + uuid: json['uuid'], + picture_url: json['picture_url'], + created: json['created'], + updated: json['updated'] + ) + } + } + end + end +end diff --git a/lib/event/attempt.rb b/lib/event/attempt.rb index 6fa614e..6f010b5 100644 --- a/lib/event/attempt.rb +++ b/lib/event/attempt.rb @@ -12,7 +12,7 @@ class Event # When an Event delivery fails, an event attempt will be registered. # It carries information meant to help you debug event reception issues. # - # ## Attributes: + # ## Attributes (return-only): # - id [string]: unique id that identifies the delivery attempt. ex: "5656565656565656" # - code [string]: delivery error code. ex: badHttpStatus, badConnection, timeout # - message [string]: delivery error full description. ex: "HTTP POST request returned status 404" diff --git a/lib/event/event.rb b/lib/event/event.rb index 26bcbe5..f057a64 100644 --- a/lib/event/event.rb +++ b/lib/event/event.rb @@ -23,7 +23,7 @@ module StarkBank # Events cannot be created, but may be retrieved from the Stark Bank API to # list all generated updates on entities. # - # ## Attributes: + # ## Attributes (return-only): # - id [string]: unique id returned when the event is created. ex: '5656565656565656' # - log [Log]: a Log object from one the subscription services (TransferLog, InvoiceLog, BoletoLog, BoletoPaymentlog or UtilityPaymentLog) # - created [DateTime]: creation datetime for the notification event. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) diff --git a/lib/invoice/invoice.rb b/lib/invoice/invoice.rb index 4f9270f..0c5fac8 100644 --- a/lib/invoice/invoice.rb +++ b/lib/invoice/invoice.rb @@ -28,18 +28,19 @@ module StarkBank # - tags [list of strings, default nil]: list of strings for tagging # # ## Attributes (return-only): - # - pdf [string, default nil]: public Invoice PDF URL. ex: 'https://invoice.starkbank.com/pdf/d454fa4e524441c1b0c1a729457ed9d8' - # - link [string, default nil]: public Invoice webpage URL. ex: 'https://my-workspace.sandbox.starkbank.com/invoicelink/d454fa4e524441c1b0c1a729457ed9d8' - # - id [string, default nil]: unique id returned when Invoice is created. ex: '5656565656565656' - # - nominal_amount [integer, default nil]: Invoice emission value in cents (will change if invoice is updated, but not if it's paid). ex: 400000 - # - fine_amount [integer, default nil]: Invoice fine value calculated over nominal_amount. ex: 20000 - # - interest_amount [integer, default nil]: Invoice interest value calculated over nominal_amount. ex: 10000 - # - discount_amount [integer, default nil]: Invoice discount value calculated over nominal_amount. ex: 3000 - # - brcode [string, default nil]: BR Code for the Invoice payment. ex: '00020101021226800014br.gov.bcb.pix2558invoice.starkbank.com/f5333103-3279-4db2-8389-5efe335ba93d5204000053039865802BR5913Arya Stark6009Sao Paulo6220051656565656565656566304A9A0' - # - fee [integer, default nil]: fee charged by the Invoice. ex: 65 (= R$ 0.65) - # - status [string, default nil]: current Invoice status. ex: 'registered' or 'paid' - # - 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) + # - pdf [string]: public Invoice PDF URL. ex: 'https://invoice.starkbank.com/pdf/d454fa4e524441c1b0c1a729457ed9d8' + # - link [string]: public Invoice webpage URL. ex: 'https://my-workspace.sandbox.starkbank.com/invoicelink/d454fa4e524441c1b0c1a729457ed9d8' + # - id [string]: unique id returned when Invoice is created. ex: '5656565656565656' + # - nominal_amount [integer]: Invoice emission value in cents (will change if invoice is updated, but not if it's paid). ex: 400000 + # - fine_amount [integer]: Invoice fine value calculated over nominal_amount. ex: 20000 + # - interest_amount [integer]: Invoice interest value calculated over nominal_amount. ex: 10000 + # - discount_amount [integer]: Invoice discount value calculated over nominal_amount. ex: 3000 + # - brcode [string]: BR Code for the Invoice payment. ex: '00020101021226800014br.gov.bcb.pix2558invoice.starkbank.com/f5333103-3279-4db2-8389-5efe335ba93d5204000053039865802BR5913Arya Stark6009Sao Paulo6220051656565656565656566304A9A0' + # - fee [integer]: fee charged by the Invoice. ex: 65 (= R$ 0.65) + # - transaction_ids [list of strings]: ledger transaction ids linked to this Invoice (if there are more than one, all but the first are reversals or failed reversal chargebacks). ex: ["19827356981273"] + # - status [string]: current Invoice status. ex: 'registered' or 'paid' + # - 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 def initialize( diff --git a/lib/invoice/log.rb b/lib/invoice/log.rb index 697d3ab..dd4bed4 100644 --- a/lib/invoice/log.rb +++ b/lib/invoice/log.rb @@ -14,7 +14,7 @@ class Invoice # user, but it can be retrieved to check additional information # on the Invoice. # - # ## Attributes: + # ## Attributes (return-only): # - id [string]: unique id returned when the log is created. ex: '5656565656565656' # - invoice [Invoice]: Invoice entity to which the log refers to. # - errors [list of strings]: list of errors linked to this Invoice event diff --git a/lib/payment_preview/payment_preview.rb b/lib/payment_preview/payment_preview.rb index b17fd5b..b15d7c2 100644 --- a/lib/payment_preview/payment_preview.rb +++ b/lib/payment_preview/payment_preview.rb @@ -10,9 +10,13 @@ module StarkBank # A PaymentPreview is used to get information from a payment code before confirming the payment. # This resource can be used to preview BR Codes and bar codes of boleto, tax and utility payments # - # ## Attributes (return-only): + # ## Parameters (required): # - id [string]: Main identification of the payment. This should be the BR Code for Pix payments and lines or bar codes for payment slips. ex: '34191.09008 63571.277308 71444.640008 5 81960000000062', '00020126580014br.gov.bcb.pix0136a629532e-7693-4846-852d-1bbff817b5a8520400005303986540510.005802BR5908T'Challa6009Sao Paulo62090505123456304B14A' + # + # ## Parameters (optional): # - scheduled [DateTime or string]: intended payment date. Right now, this parameter only has effect on BrcodePreviews. ex: '2020-04-30' + # + # ## Attributes (return-only): # - type [string]: Payment type. ex: 'brcode-payment', 'boleto-payment', 'utility-payment' or 'tax-payment' # - payment [BrcodePreview, BoletoPreview, UtilityPreview or TaxPreview]: Information preview of the informed payment. class PaymentPreview < StarkCore::Utils::Resource diff --git a/lib/payment_request/payment_request.rb b/lib/payment_request/payment_request.rb index da35c28..a2699da 100644 --- a/lib/payment_request/payment_request.rb +++ b/lib/payment_request/payment_request.rb @@ -6,6 +6,7 @@ module StarkBank # # PaymentRequest object + # # A PaymentRequest is an indirect request to access a specific cash-out service # (such as Transfer, BrcodePayments, etc.) which goes through the cost center # approval flow on our website. To emit a PaymentRequest, you must direct it to @@ -16,24 +17,27 @@ module StarkBank # - center_id [String]: target cost center ID. ex: '5656565656565656' # - payment [Transfer, BrcodePayment, BoletoPayment, UtilityPayment, Transaction or dictionary]: payment entity that should be approved and executed. # - # ## Parameters (optional): + # ## Parameters (conditionally required): # - type [String]: payment type, inferred from the payment parameter if it is not a dictionary. ex: 'transfer', 'brcode-payment' + # + # ## Parameters (optional): # - due [Date, DateTime, Time or string]: Payment target date in ISO format. ex: 2020-12-31 # - tags [list of strings]: list of strings for tagging # # ## Attributes (return-only): # - id [String]: unique id returned when PaymentRequest is created. ex: '5656565656565656' - # - amount [integer, default nil]: PaymentRequest amount. ex: 100000 = R$1.000,00 - # - status [string, default nil]: current PaymentRequest status.ex: 'pending' or 'approved' - # - actions [list of dictionaries, default nil]: list of actions that are affecting this PaymentRequest. ex: [{'type': 'member', 'id': '56565656565656, 'action': 'requested'}] - # - updated [DateTime, default nil]: latest update datetime for the PaymentRequest. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - # - created [DateTime, default nil]: creation datetime for the PaymentRequest. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) + # - amount [integer]: PaymentRequest amount. ex: 100000 = R$1.000,00 + # - description [string]: payment request description. ex: "Tony Stark's Suit" + # - status [string]: current PaymentRequest status.ex: 'pending' or 'approved' + # - actions [list of dictionaries]: list of actions that are affecting this PaymentRequest. ex: [{'type': 'member', 'id': '56565656565656, 'action': 'requested'}] + # - updated [DateTime]: latest update datetime for the PaymentRequest. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) + # - created [DateTime]: creation datetime for the PaymentRequest. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) # class PaymentRequest < StarkCore::Utils::Resource - attr_reader :center_id, :payment, :type, :due, :tags, :amount, :status, :actions, :updated, :created + attr_reader :center_id, :payment, :type, :due, :tags, :amount, :description, :status, :actions, :updated, :created def initialize( payment:, center_id:, id: nil, type: nil, due: nil, tags: nil, amount: nil, status: nil, - actions: nil, updated: nil, created: nil + description: nil, actions: nil, updated: nil, created: nil ) super(id) @center_id = center_id @@ -42,6 +46,7 @@ def initialize( @amount = amount @status = status @actions = actions + @description = description @updated = updated @created = created @@ -173,6 +178,7 @@ def self.resource tags: json['tags'], amount: json['amount'], status: json['status'], + description: json['description'], actions: json['actions'], updated: json['updated'], created: json['created'] diff --git a/lib/starkbank.rb b/lib/starkbank.rb index 190f666..294dab0 100644 --- a/lib/starkbank.rb +++ b/lib/starkbank.rb @@ -7,10 +7,11 @@ require_relative('invoice/log') require_relative('invoice/payment') require_relative('dict_key/dict_key') +require_relative('dynamic_brcode/dynamic_brcode') require_relative('deposit/deposit') require_relative('deposit/log') -require_relative('brcode_preview/brcode_preview') require_relative('brcode_payment/brcode_payment') +require_relative('brcode_payment/rule') require_relative('brcode_payment/log') require_relative('boleto/boleto') require_relative('boleto/log') @@ -18,6 +19,7 @@ require_relative('boleto_holmes/log') require_relative('transfer/transfer') require_relative('transfer/log') +require_relative('transfer/rule') require_relative('boleto_payment/boleto_payment') require_relative('boleto_payment/log') require_relative('utility_payment/utility_payment') diff --git a/lib/tax_payment/log.rb b/lib/tax_payment/log.rb index ef6814d..e60390d 100644 --- a/lib/tax_payment/log.rb +++ b/lib/tax_payment/log.rb @@ -14,7 +14,7 @@ class TaxPayment # user, but it can be retrieved to check additional information # on the TaxPayment. # - # ## Attributes: + # ## Attributes (return-only): # - id [string]: unique id returned when the log is created. ex: '5656565656565656' # - payment [TaxPayment]: TaxPayment entity to which the log refers to. # - errors [list of strings]: list of errors linked to this TaxPayment event diff --git a/lib/tax_payment/tax_payment.rb b/lib/tax_payment/tax_payment.rb index 2e05aa7..0417173 100644 --- a/lib/tax_payment/tax_payment.rb +++ b/lib/tax_payment/tax_payment.rb @@ -23,18 +23,19 @@ module StarkBank # - 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' - # - type [string, default nil]: tax type. ex: 'das' - # - status [string, default nil]: current payment status. ex: 'success' or 'failed' - # - amount [int, default nil]: amount automatically calculated from line or bar_code. ex: 23456 (= R$ 234.56) - # - fee [integer, default nil]: fee charged when tax payment is created. ex: 200 (= R$ 2.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) + # - id [string]: unique id returned when payment is created. ex: '5656565656565656' + # - type [string]: tax type. ex: 'das' + # - status [string]: current payment status. ex: 'success' or 'failed' + # - amount [int]: amount automatically calculated from line or bar_code. ex: 23456 (= R$ 234.56) + # - fee [integer]: fee charged when tax payment is created. ex: 200 (= R$ 2.00) + # - transaction_ids [list of strings]: ledger transaction ids linked to this TaxPayment. ex: ["19827356981273"] + # - 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 TaxPayment < StarkCore::Utils::Resource - attr_reader :id, :line, :bar_code, :description, :tags, :scheduled, :status, :amount, :fee, :type, :updated, :created + attr_reader :id, :line, :bar_code, :description, :tags, :scheduled, :status, :amount, :fee, :type, :transaction_ids, :updated, :created def initialize( id: nil, line: nil, bar_code: nil, description:, tags: nil, scheduled: nil, - status: nil, amount: nil, fee: nil, type: nil, updated: nil, created: nil + status: nil, amount: nil, fee: nil, transaction_ids: nil, type: nil, updated: nil, created: nil ) super(id) @line = line @@ -46,6 +47,7 @@ def initialize( @amount = amount @fee = fee @type = type + @transaction_ids = transaction_ids @updated = StarkCore::Utils::Checks.check_datetime(updated) @created = StarkCore::Utils::Checks.check_datetime(created) end @@ -193,6 +195,7 @@ def self.resource amount: json['amount'], fee: json['fee'], type: json['type'], + transaction_ids: json['transaction_ids'], updated: json['updated'], created: json['created'] ) diff --git a/lib/transaction/transaction.rb b/lib/transaction/transaction.rb index b7fece4..19ba880 100644 --- a/lib/transaction/transaction.rb +++ b/lib/transaction/transaction.rb @@ -26,11 +26,11 @@ module StarkBank # # ## Attributes (return-only): # - sender_id [string]: unique id of the sending workspace. ex: '5656565656565656' - # - source [string, default nil]: locator of the entity that generated the transaction. ex: 'charge/1827351876292', 'transfer/92873912873/chargeback' - # - id [string, default nil]: unique id returned when Transaction is created. ex: '7656565656565656' - # - fee [integer, default nil]: fee charged when transaction is created. ex: 200 (= R$ 2.00) - # - balance [integer, default nil]: account balance after transaction was processed. ex: 100000000 (= R$ 1,000,000.00) - # - created [DateTime, default nil]: creation datetime for the boleto. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) + # - source [string]: locator of the entity that generated the transaction. ex: 'charge/1827351876292', 'transfer/92873912873/chargeback' + # - id [string]: unique id returned when Transaction is created. ex: '7656565656565656' + # - fee [integer]: fee charged when transaction is created. ex: 200 (= R$ 2.00) + # - balance [integer]: account balance after transaction was processed. ex: 100000000 (= R$ 1,000,000.00) + # - created [DateTime]: creation datetime for the boleto. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) class Transaction < StarkCore::Utils::Resource attr_reader :amount, :description, :external_id, :receiver_id, :sender_id, :tags, :id, :fee, :created, :source def initialize(amount:, description:, external_id:, receiver_id:, sender_id: nil, tags: nil, id: nil, fee: nil, source: nil, balance: nil, created: nil) diff --git a/lib/transfer/log.rb b/lib/transfer/log.rb index 2c9276c..6988d4c 100644 --- a/lib/transfer/log.rb +++ b/lib/transfer/log.rb @@ -13,7 +13,7 @@ class Transfer # is generated for the entity. This log is never generated by the # user. # - # ## Attributes: + # ## Attributes (return-only): # - id [string]: unique id returned when the log is created. ex: '5656565656565656' # - transfer [Transfer]: Transfer entity to which the log refers to. # - errors [list of strings]: list of errors linked to this BoletoPayment event. diff --git a/lib/transfer/rule.rb b/lib/transfer/rule.rb new file mode 100644 index 0000000..6517fb2 --- /dev/null +++ b/lib/transfer/rule.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require_relative('../utils/rest') + + +module StarkBank + class Transfer + # # Transfer::Rule object + # + # The Transfer::Rule object modifies the behavior of Transfer objects when passed as an argument upon their creation. + # + # ## Parameters (required): + # - key [string]: Rule to be customized, describes what Transfer behavior will be altered. ex: "resendingLimit" + # - value [integer]: Value of the rule. ex: 5 + 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::Transfer::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/transfer/transfer.rb b/lib/transfer/transfer.rb index 3bedb9a..c885f6d 100644 --- a/lib/transfer/transfer.rb +++ b/lib/transfer/transfer.rb @@ -15,7 +15,7 @@ module StarkBank # - amount [integer]: amount in cents to be transferred. ex: 1234 (= R$ 12.34) # - name [string]: receiver full name. ex: 'Anthony Edward Stark' # - tax_id [string]: receiver tax ID (CPF or CNPJ) with or without formatting. ex: '01234567890' or '20.018.183/0001-80' - # - bank_code [string]: code of the receiver bank institution in Brazil. If an ISPB (8 digits) is informed, a PIX transfer will be created, else a TED will be issued. ex: '20018183' or '260' + # - bank_code [string]: code of the receiver bank institution in Brazil. If an ISPB (8 digits) is informed, a Pix transfer will be created, else a TED will be issued. ex: '20018183' or '260' # - branch_code [string]: receiver bank account branch. Use '-' in case there is a verifier digit. ex: '1357-9' # - account_number [string]: receiver bank account number. Use '-' before the verifier digit. ex: '876543-2' # @@ -25,17 +25,18 @@ module StarkBank # - scheduled [string, default now]: datetime when the transfer will be processed. May be pushed to next business day if necessary. ex: DateTime.new(2020, 3, 11, 8, 13, 12, 11) # - description [string, default nil]: optional description to override default description to be shown in the bank statement. ex: 'Payment for service #1234' # - tags [list of strings]: list of strings for reference when searching for transfers. ex: ['employees', 'monthly'] + # - rules [list of Transfer::Rules, default []]: list of Transfer::Rule objects for modifying transfer behavior. ex: [Transfer::Rule(key: "resendingLimit", value: 5)] # # ## Attributes (return-only): - # - id [string, default nil]: unique id returned when Transfer is created. ex: '5656565656565656' - # - fee [integer, default nil]: fee charged when transfer is created. ex: 200 (= R$ 2.00) - # - status [string, default nil]: current transfer status. ex: 'success' or 'failed' - # - transaction_ids [list of strings, default nil]: ledger transaction ids linked to this transfer (if there are two, second is the chargeback). ex: ['19827356981273'] - # - created [DateTime, default nil]: creation datetime for the transfer. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) - # - updated [DateTime, default nil]: latest update datetime for the transfer. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) + # - id [string]: unique id returned when Transfer is created. ex: '5656565656565656' + # - fee [integer]: fee charged when transfer is created. ex: 200 (= R$ 2.00) + # - status [string]: current transfer status. ex: 'success' or 'failed' + # - transaction_ids [list of strings]: ledger transaction ids linked to this transfer (if there are two, second is the chargeback). ex: ['19827356981273'] + # - created [DateTime]: creation datetime for the transfer. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) + # - updated [DateTime]: latest update datetime for the transfer. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) class Transfer < StarkCore::Utils::Resource - attr_reader :amount, :name, :tax_id, :bank_code, :branch_code, :account_number, :account_type, :external_id, :scheduled, :description, :transaction_ids, :fee, :tags, :status, :id, :created, :updated - def initialize(amount:, name:, tax_id:, bank_code:, branch_code:, account_number:, account_type: nil, external_id: nil, scheduled: nil, description: nil, transaction_ids: nil, fee: nil, tags: nil, status: nil, id: nil, created: nil, updated: nil) + attr_reader :amount, :name, :tax_id, :bank_code, :branch_code, :account_number, :account_type, :external_id, :scheduled, :description, :transaction_ids, :fee, :tags, :rules, :status, :id, :created, :updated + def initialize(amount:, name:, tax_id:, bank_code:, branch_code:, account_number:, account_type: nil, external_id: nil, scheduled: nil, description: nil, transaction_ids: nil, fee: nil, tags: nil, rules: nil, status: nil, id: nil, created: nil, updated: nil) super(id) @amount = amount @name = name @@ -47,9 +48,9 @@ def initialize(amount:, name:, tax_id:, bank_code:, branch_code:, account_number @external_id = external_id @scheduled = StarkCore::Utils::Checks.check_date_or_datetime(scheduled) @description = description - @transaction_ids = transaction_ids - @fee = fee @tags = tags + @rules = StarkBank::Transfer::Rule.parse_rules(rules) + @fee = fee @status = status @transaction_ids = transaction_ids @created = StarkCore::Utils::Checks.check_datetime(created) @@ -197,7 +198,6 @@ def self.resource resource_name: 'Transfer', resource_maker: proc { |json| Transfer.new( - id: json['id'], amount: json['amount'], name: json['name'], tax_id: json['tax_id'], @@ -208,10 +208,12 @@ def self.resource external_id: json['external_id'], scheduled: json['scheduled'], description: json['description'], - transaction_ids: json['transaction_ids'], - fee: json['fee'], tags: json['tags'], + rules: json['rules'], + id: json['id'], + fee: json['fee'], status: json['status'], + transaction_ids: json['transaction_ids'], created: json['created'], updated: json['updated'] ) diff --git a/lib/utility_payment/log.rb b/lib/utility_payment/log.rb index 65cd9bf..7c288fa 100644 --- a/lib/utility_payment/log.rb +++ b/lib/utility_payment/log.rb @@ -13,7 +13,7 @@ class UtilityPayment # is generated for the entity. This log is never generated by the user, but it can # be retrieved to check additional information on the UtilityPayment. # - # ## Attributes: + # ## Attributes (return-only): # - id [string]: unique id returned when the log is created. ex: '5656565656565656' # - payment [UtilityPayment]: UtilityPayment entity to which the log refers to. # - errors [list of strings]: list of errors linked to this UtilityPayment event. diff --git a/lib/utility_payment/utility_payment.rb b/lib/utility_payment/utility_payment.rb index 7609354..460e20a 100644 --- a/lib/utility_payment/utility_payment.rb +++ b/lib/utility_payment/utility_payment.rb @@ -23,14 +23,17 @@ module StarkBank # - tags [list of strings]: 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]: amount automatically calculated from line or bar_code. ex: 23456 (= R$ 234.56) - # - fee [integer, default nil]: fee charged when utility payment is created. ex: 200 (= R$ 2.00) - # - created [DateTime, default nil]: creation datetime for the payment. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) + # - id [string]: unique id returned when payment is created. ex: '5656565656565656' + # - status [string]: current payment status. ex: 'success' or 'failed' + # - amount [int]: amount automatically calculated from line or bar_code. ex: 23456 (= R$ 234.56) + # - fee [integer]: fee charged when utility payment is created. ex: 200 (= R$ 2.00) + # - type [string]: payment type. ex: "utility" + # - transaction_ids [list of strings]: ledger transaction ids linked to this UtilityPayment. ex: ["19827356981273"] + # - created [DateTime]: creation datetime for the payment. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) + # - updated [DateTime]: latest update datetime for the payment. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) class UtilityPayment < StarkCore::Utils::Resource - attr_reader :description, :line, :bar_code, :tags, :scheduled, :id, :amount, :fee, :status, :created - def initialize(description:, line: nil, bar_code: nil, tags: nil, scheduled: nil, id: nil, amount: nil, fee: nil, status: nil, created: nil) + attr_reader :description, :line, :bar_code, :tags, :scheduled, :id, :amount, :fee, :type, :transaction_ids, :status, :created, :updated + def initialize(description:, line: nil, bar_code: nil, tags: nil, scheduled: nil, id: nil, amount: nil, fee: nil, type: nil, transaction_ids: nil, status: nil, created: nil, updated: nil) super(id) @description = description @line = line @@ -40,7 +43,10 @@ def initialize(description:, line: nil, bar_code: nil, tags: nil, scheduled: nil @amount = amount @fee = fee @status = status + @type = type + @transaction_ids = transaction_ids @created = StarkCore::Utils::Checks.check_datetime(created) + @updated = StarkCore::Utils::Checks.check_datetime(updated) end # # Create UtilityPayments @@ -184,8 +190,11 @@ def self.resource scheduled: json['scheduled'], amount: json['amount'], fee: json['fee'], + type: json['type'], + transaction_ids: json['transaction_ids'], status: json['status'], - created: json['created'] + created: json['created'], + updated: json['updated'] ) } } diff --git a/lib/webhook/webhook.rb b/lib/webhook/webhook.rb index a62e7f5..86d84e4 100644 --- a/lib/webhook/webhook.rb +++ b/lib/webhook/webhook.rb @@ -15,8 +15,8 @@ module StarkBank # - url [string]: Url that will be notified when an event occurs. # - subscriptions [list of strings]: list of any non-empty combination of the available services. ex: ['transfer', 'deposit'] # - # ## Attributes: - # - id [string, default nil]: unique id returned when the webhook is created. ex: '5656565656565656' + # ## Attributes (return-only): + # - id [string]: unique id returned when the webhook is created. ex: '5656565656565656' class Webhook < StarkCore::Utils::Resource attr_reader :url, :subscriptions, :id def initialize(url:, subscriptions:, id: nil) diff --git a/lib/workspace/workspace.rb b/lib/workspace/workspace.rb index 83a90cb..2362a24 100644 --- a/lib/workspace/workspace.rb +++ b/lib/workspace/workspace.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require('starkcore') +require('base64') require_relative('../utils/rest') @@ -18,15 +19,23 @@ module StarkBank # ## Parameters (optional): # - allowed_tax_ids [list of strings]: list of tax IDs that will be allowed to send Deposits to this Workspace. ex: ['012.345.678-90', '20.018.183/0001-80'] # - # ## Attributes: - # - id [string, default nil]: unique id returned when the workspace is created. ex: '5656565656565656' + # ## Attributes (return-only): + # - id [string]: unique id returned when the workspace is created. ex: '5656565656565656' + # - status [string]: current Workspace status. Options: 'active', 'closed', 'frozen' or 'blocked' + # - organization_id [string]: unique organization id returned when the organization is created. ex: '5656565656565656' + # - picture_url [string]: public workspace image (png) URL. ex: 'https://storage.googleapis.com/api-ms-workspace-sbx.appspot.com/pictures/workspace/6284441752174592.png?20230208220551' + # - created [DateTime]: creation datetime for the Workspace. ex: DateTime.new(2020, 3, 10, 10, 30, 0, 0) class Workspace < StarkCore::Utils::Resource - attr_reader :username, :name, :allowed_tax_ids, :id - def initialize(username:, name:, allowed_tax_ids: nil, id: nil) + attr_reader :username, :name, :allowed_tax_ids, :id, :status, :organization_id, :picture_url, :created + def initialize(username:, name:, allowed_tax_ids: nil, id: nil, status: nil, organization_id: nil, picture_url: nil, created: nil) super(id) @username = username @name = name @allowed_tax_ids = allowed_tax_ids + @status = status + @organization_id = organization_id + @picture_url = picture_url + @created = StarkCore::Utils::Checks.check_datetime(created) end # # Create Workspace @@ -112,16 +121,31 @@ def self.page(cursor: nil, limit: nil, username: nil, ids: nil, user: nil) # ## Parameters (required): # - id [string]: Workspace unique id. ex: '5656565656565656' # + # ## Parameters (conditionally required): + # - picture_type [string]: picture MIME type. This parameter will be required if the picture parameter is informed ex: 'image/png' or 'image/jpeg' + # # ## Parameters (optional): # - username [string, default nil]: query by the simplified name that defines the workspace URL. This name is always unique across all Stark Bank Workspaces. Ex: 'starkbankworkspace' # - name [string, default nil]: Full name that identifies the Workspace. This name will appear when people access the Workspace on our platform, for example. Ex: 'Stark Bank Workspace' # - allowed_tax_ids [list of strings, default nil]: list of tax IDs that will be allowed to send Deposits to this Workspace. If empty, all are allowed. ex: ['012.345.678-90', '20.018.183/0001-80'] + # - status [string, default nil]: current Workspace status. Options: 'active' or 'blocked' + # - picture [bytes, default nil]: Binary buffer of the picture. ex: open('/path/to/file.png', 'rb').read() # - user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call # # ## Return: # - updated Workspace object - def self.update(id, user: nil, username: nil, name: nil, allowed_tax_ids: nil) - StarkBank::Utils::Rest.patch_id(id: id, user: user, username: username, name: name, allowed_tax_ids: allowed_tax_ids, **resource) + def self.update(id, username: nil, name: nil, allowed_tax_ids: nil, status: nil, picture: nil, picture_type: nil, user: nil) + + payload = { + 'allowed_tax_ids': allowed_tax_ids, + 'status': status, + } + + unless picture.nil? + payload['picture'] = "data:#{picture_type};base64,#{Base64.encode64(picture)}" + end + + StarkBank::Utils::Rest.patch_id(id: id, user: user, username: username, name: name, **payload, **resource) end def self.resource @@ -132,7 +156,11 @@ def self.resource id: json['id'], username: json['username'], name: json['name'], - allowed_tax_ids: json['allowed_tax_ids'] + allowed_tax_ids: json['allowed_tax_ids'], + status: json['status'], + organization_id: json['organization_id'], + picture_url: json['picture_url'], + created: json['created'] ) } } diff --git a/test/example_generator.rb b/test/example_generator.rb index c3de563..e1932e6 100644 --- a/test/example_generator.rb +++ b/test/example_generator.rb @@ -116,8 +116,14 @@ def self.brcode_payment_example(invoice: nil, schedule: true) StarkBank::BrcodePayment.new( brcode: invoice.brcode, scheduled: schedule ? DateTime.now + 600 : nil, - description: 'paying a PIX', - tax_id: '20.018.183/0001-80' + description: 'paying a Pix', + tax_id: '20.018.183/0001-80', + rules: [ + StarkBank::BrcodePayment::Rule.new( + key: 'resendingLimit', + value: 5 + ) + ] ) end @@ -140,7 +146,13 @@ def self.transfer_example(schedule: false) account_number: '10000-0', account_type: 'checking', external_id: 'ruby-' + rand(1e10).to_s, - scheduled: schedule ? Time.now + 24 * 3600 : nil + scheduled: schedule ? Time.now + 24 * 3600 : nil, + rules: [ + StarkBank::Transfer::Rule.new( + key: 'resendingLimit', + value: 5 + ) + ] ) end @@ -205,6 +217,14 @@ def self.create_payment end end + def self.dynamic_brcode_example + StarkBank::DynamicBrcode.new( + amount: 100_000, + expiration: 3600 * 2, + tags: ["test1", "test2"], + ) + end + def self.organization_example StarkBank::Organization.new( environment: 'sandbox', diff --git a/test/starkbank/test_brcode_preview.rb b/test/starkbank/test_brcode_preview.rb deleted file mode 100644 index 8ac01e0..0000000 --- a/test/starkbank/test_brcode_preview.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -require_relative('../test_helper.rb') -require_relative('../example_generator.rb') - -describe(StarkBank::BrcodePreview, '#brcode-preview#') do - it 'query' do - previews = StarkBank::BrcodePreview.query(brcodes: ["00020126580014br.gov.bcb.pix013635719950-ac93-4bab-8ad6-56d7fb63afd252040000530398654040.005802BR5915Stark Bank S.A.6009Sao Paulo62070503***6304AA26"]).to_a - expect(previews.length).must_equal(1) - previews.each do |preview| - expect(preview.amount).wont_be_nil - end - end -end diff --git a/test/starkbank/test_dynamic_brcode.rb b/test/starkbank/test_dynamic_brcode.rb new file mode 100644 index 0000000..b47ae57 --- /dev/null +++ b/test/starkbank/test_dynamic_brcode.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require_relative('../test_helper.rb') +require_relative('../example_generator.rb') + +describe(StarkBank::DynamicBrcode, '#dynamic-brcode#') do + it 'query' do + brcodes = StarkBank::DynamicBrcode.query(limit: 10).to_a + expect(brcodes.length).must_equal(10) + brcodes.each do |brcode| + expect(brcode.uuid).wont_be_nil + end + end + + it 'page' do + uuids = [] + cursor = nil + brcodes = nil + (0..1).step(1) do + brcodes, cursor = StarkBank::DynamicBrcode.page(limit: 5, cursor: cursor) + brcodes.each do |brcode| + expect(uuids).wont_include(brcode.uuid) + uuids << brcode.uuid + end + break if cursor.nil? + end + expect(uuids.length).must_equal(10) + end + + it 'create and get' do + brcode = StarkBank::DynamicBrcode.create([ExampleGenerator.dynamic_brcode_example])[0] + get_brcode = StarkBank::DynamicBrcode.get(brcode.uuid) + expect(brcode.uuid).must_equal(get_brcode.uuid) + end +end diff --git a/test/starkbank/test_workspace.rb b/test/starkbank/test_workspace.rb index ca442f4..00122e1 100644 --- a/test/starkbank/test_workspace.rb +++ b/test/starkbank/test_workspace.rb @@ -43,4 +43,30 @@ end expect(ids.length).must_be :==, 4 end + + it 'update status in workspace' do + workspaces, cursor = StarkBank::Workspace.page(limit: 1, user: ExampleGenerator.organization_example) + + workspace = StarkBank::Workspace.update( + workspaces[0].id, + status: "blocked", + user: StarkBank::Organization.replace(ExampleGenerator.organization_example, workspaces[0].id) + ) + expect(workspace.status).must_equal("blocked") + end + + it 'update picture in workspace' do + workspaces, cursor = StarkBank::Workspace.page(limit: 1, user: ExampleGenerator.organization_example) + file = open('test/utils/logo.png', 'rb') + picture = file.read + file.close + + workspace = StarkBank::Workspace.update( + workspaces[0].id, + picture: picture, + picture_type: 'image/png', + user: StarkBank::Organization.replace(ExampleGenerator.organization_example, workspaces[0].id) + ) + expect(workspace.id).must_equal(workspaces[0].id) + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 416b7ce..00bc0ee 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,6 +2,7 @@ require('minitest/autorun') require('minitest/spec') +require('bundler/setup') require('./lib/starkbank') require_relative('user.rb') diff --git a/test/utils/logo.png b/test/utils/logo.png new file mode 100644 index 0000000..00867ec Binary files /dev/null and b/test/utils/logo.png differ