Class: StarkBank::PaymentRequest

Inherits:
Utils::Resource show all
Defined in:
lib/payment_request/payment_request.rb

Overview

# 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
a specific cost center by its ID, which can be retrieved on our website at the
cost center page.

## Parameters (required):

  • 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):

  • type [String]: payment type, inferred from the payment parameter if it is not a dictionary. ex: ‘transfer’, ‘brcode-payment’

  • 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: [‘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)

Instance Attribute Summary collapse

Attributes inherited from Utils::Resource

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Utils::SubResource

#inspect, #to_s

Constructor Details

#initialize(payment:, center_id:, id: nil, type: nil, due: nil, tags: nil, amount: nil, status: nil, actions: nil, updated: nil, created: nil) ⇒ PaymentRequest

Returns a new instance of PaymentRequest.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/payment_request/payment_request.rb', line 34

def initialize(
  payment:, center_id:, id: nil, type: nil, due: nil, tags: nil, amount: nil, status: nil,
  actions: nil, updated: nil, created: nil
)
  super(id)
  @center_id = center_id
  @due = due
  @tags = tags
  @amount = amount
  @status = status
  @actions = actions
  @updated = updated
  @created = created

  @payment, @type = parse_payment(payment: payment, type: type)
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



33
34
35
# File 'lib/payment_request/payment_request.rb', line 33

def actions
  @actions
end

#amountObject (readonly)

Returns the value of attribute amount.



33
34
35
# File 'lib/payment_request/payment_request.rb', line 33

def amount
  @amount
end

#center_idObject (readonly)

Returns the value of attribute center_id.



33
34
35
# File 'lib/payment_request/payment_request.rb', line 33

def center_id
  @center_id
end

#createdObject (readonly)

Returns the value of attribute created.



33
34
35
# File 'lib/payment_request/payment_request.rb', line 33

def created
  @created
end

#dueObject (readonly)

Returns the value of attribute due.



33
34
35
# File 'lib/payment_request/payment_request.rb', line 33

def due
  @due
end

#paymentObject (readonly)

Returns the value of attribute payment.



33
34
35
# File 'lib/payment_request/payment_request.rb', line 33

def payment
  @payment
end

#statusObject (readonly)

Returns the value of attribute status.



33
34
35
# File 'lib/payment_request/payment_request.rb', line 33

def status
  @status
end

#tagsObject (readonly)

Returns the value of attribute tags.



33
34
35
# File 'lib/payment_request/payment_request.rb', line 33

def tags
  @tags
end

#typeObject (readonly)

Returns the value of attribute type.



33
34
35
# File 'lib/payment_request/payment_request.rb', line 33

def type
  @type
end

#updatedObject (readonly)

Returns the value of attribute updated.



33
34
35
# File 'lib/payment_request/payment_request.rb', line 33

def updated
  @updated
end

Class Method Details

.create(payment_requests, user: nil) ⇒ Object

Create PaymentRequests Sends a list of PaymentRequests objects for creating in the Stark Bank API

## Parameters

  • payment_requests [list of PaymentRequest objects]: list of PaymentRequest objects to be created in the API

  • user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call

## Return

  • list of PaymentRequest objects with updated attributes



61
62
63
# File 'lib/payment_request/payment_request.rb', line 61

def self.create(payment_requests, user: nil)
  StarkBank::Utils::Rest.post(entities: payment_requests, user: user, **resource)
end

.page(cursor: nil, center_id:, limit: nil, after: nil, before: nil, status: nil, type: nil, sort: nil, tags: nil, ids: nil, user: nil) ⇒ Object

# Retrieve paged PaymentRequests

Receive a list of up to 100 PaymentRequest 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)

  • status [string, default nil]: filter for status of retrieved objects. ex: ‘paid’ or ‘registered’

  • 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’]

  • user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call

## Return:

  • list of PaymentRequest objects with updated attributes and cursor to retrieve the next page of PaymentRequest objects



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/payment_request/payment_request.rb', line 119

def self.page(cursor: nil, center_id:, limit: nil, after: nil, before: nil, status: nil, type: nil, sort: 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,
    center_id: center_id,
    limit: limit,
    after: after,
    before: before,
    status: status,
    type: type,
    sort: sort,
    tags: tags,
    ids: ids,
    user: user,
    **resource
  )
end

.query(center_id:, limit: nil, after: nil, before: nil, status: nil, type: nil, sort: nil, tags: nil, ids: nil, user: nil) ⇒ Object

# Retrieve PaymentRequests

Receive a generator of PaymentRequest objects previously created in the Stark Bank API

## Parameters (required):

  • center_id [string]: target cost center ID. ex: ‘5656565656565656’

## 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)

  • status [string, default ‘-created’]: sort order considered in response. Valid options are ‘-created’ or ‘-due’.

  • type [string, default nil]: payment type, inferred from the payment parameter if it is not a dictionary. ex: ‘transfer’, ‘brcode-payment’

  • sort [list of strings, default nil]: tags to filter retrieved objects. ex: [‘tony’, ‘stark’]

  • 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’]

  • user [Organization/Project object]: Organization or Project object. Not necessary if StarkBank.user was set before function call

## Return:

  • generator of PaymentRequest objects with updated attributes



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/payment_request/payment_request.rb', line 84

def self.query(center_id:, limit: nil, after: nil, before: nil, status: nil, type: nil, sort: 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(
    center_id: center_id,
    limit: limit,
    after: after,
    before: before,
    status: status,
    type: type,
    sort: sort,
    tags: tags,
    ids: ids,
    user: user,
    **resource
  )
end

.resourceObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/payment_request/payment_request.rb', line 164

def self.resource
  {
    resource_name: 'PaymentRequest',
    resource_maker: proc { |json|
      PaymentRequest.new(
        id: json['id'],
        payment: json['payment'],
        center_id: json['centerId'],
        type: json['type'],
        tags: json['tags'],
        amount: json['amount'],
        status: json['status'],
        actions: json['actions'],
        updated: json['updated'],
        created: json['created']
      )
    }
  }
end

Instance Method Details

#parse_payment(payment:, type:) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/payment_request/payment_request.rb', line 138

def parse_payment(payment:, type:)
  return [payment, 'transfer'] if payment.is_a?(StarkBank::Transfer)
  return [payment, 'transaction'] if payment.is_a?(StarkBank::Transaction)
  return [payment, 'brcode-payment'] if payment.is_a?(StarkBank::BrcodePayment)
  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, 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,
    'darf-payment': StarkBank::DarfPayment.resource
  }[type.to_sym]

  payment = StarkBank::Utils::API.from_api_json(resource[:resource_maker], payment) unless resource.nil?

  [payment, type]
end