Class: Bs2Api::Payment::Async

Inherits:
Base
  • Object
show all
Defined in:
lib/bs2_api/payment/async.rb

Overview

Class used to store a bucket of requests which will be sent altogether.

Instance Attribute Summary collapse

Attributes inherited from Base

#payment

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id: Bs2Api.configuration.client_id, client_secret: Bs2Api.configuration.client_secret, proxy: nil) ⇒ Async

Returns a new instance of Async.



60
61
62
63
64
65
66
67
68
69
# File 'lib/bs2_api/payment/async.rb', line 60

def initialize(
  client_id: Bs2Api.configuration.client_id,
  client_secret: Bs2Api.configuration.client_secret,
  proxy: nil
)
  @client_id = client_id
  @client_secret = client_secret
  @proxy = proxy
  @requests = []
end

Instance Attribute Details

#requestsObject (readonly)

Returns the value of attribute requests.



8
9
10
# File 'lib/bs2_api/payment/async.rb', line 8

def requests
  @requests
end

Class Method Details

.check_payment_status(request_id, client_id: Bs2Api.configuration.client_id, client_secret: Bs2Api.configuration.client_secret, proxy: nil) ⇒ Object

Check the status of a request manually. Usually we will be notified via webhook, but in case we do not get notification for a specific payment we need to start to poll the BS2 API manually.

@param request_id The id (SolicitacaoId) for the payment returned by Bs2Api::Payment::Async#call @param client_id ID of the client issued by BS2 used for authorizatino. Optional, if not passed the default will be used @param client_secret The password for the account with id client_id. Optional, if not passed the default will be used.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bs2_api/payment/async.rb', line 21

def check_payment_status(
  request_id,
  client_id: Bs2Api.configuration.client_id,
  client_secret: Bs2Api.configuration.client_secret,
  proxy: nil
)

  bearer_token = Bs2Api::Request::Auth.token(
    client_id: client_id,
    client_secret: client_secret
  )

  response = HTTParty.get(
    request_status_url(request_id), 
    http_proxyaddr: proxy&.host,
    http_proxyport: proxy&.port,
    http_proxyuser: proxy&.user,
    http_proxypass: proxy&.password,
    headers: { 
      'Authorization': "Bearer #{bearer_token}"
    }
  )

  raise Bs2Api::Errors::Base, response.body.to_s unless response.success?

  Bs2Api::Entities::AsyncStatus.from_response response.parsed_response
end

Instance Method Details

#add_request(request) ⇒ Object

Push request into the request bucker. This will not send the request. When Bs2Api::Payment::Async#call is called all requeststs from the bucket will be sent simultaneously

@param The async request which is going to be placed in the bucket



77
78
79
# File 'lib/bs2_api/payment/async.rb', line 77

def add_request(request)
  @requests << request
end

#callObject

The response from call differs from Payment::Base#call because for the async method we get 202 and a list of payment transactions IDs Later on we will notified via webhook for all payments transactions which were successful.

data (PIX key, value, etc).

@return List of payments which we should expect to be confirmed via webhook. The response will contain field “SolicitacaoId” genreated by BS2 this ID is used to match the payment in the webhook



97
98
99
100
101
102
103
104
# File 'lib/bs2_api/payment/async.rb', line 97

def call
  response = post_request
  raise Bs2Api::Errors::BadRequest, ::Util::Response.parse_error(response) unless response.code == 202

  response.parsed_response.map do |async_response|
    Bs2Api::Entities::AsyncResponse.from_response(async_response)
  end
end

#requests_countObject

Get the size of the bucket list



82
83
84
# File 'lib/bs2_api/payment/async.rb', line 82

def requests_count
  @requests.length
end