Skip to content

Commit

Permalink
Adding request timeout when making requests to Braspag Gateway (Dinda…
Browse files Browse the repository at this point in the history
  • Loading branch information
stunts authored and marciotoshio committed Nov 17, 2016
1 parent a7586b2 commit eff541c
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 22 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ development:
query_url: 'https://apiquerysandbox.braspag.com.br'
merchant_id: 'Your MerchantId here'
merchant_key: 'Your MerchantKey here'
request_timeout: 60
```
If you want to use a different file or manually set which environment should be
Expand All @@ -59,11 +60,13 @@ development:
query_url: 'https://apiquerysandbox.braspag.com.br'
merchant_id: 'Your MerchantId here'
merchant_key: 'Your MerchantKey here'
request_timeout: 60
production:
url: <%= ENV['BRASPAG_URL'] %>
query_url: <%= ENV['BRASPAG_QUERY_URL'] %>
merchant_id: <%= ENV['BRASPAG_MERCHANT_ID'] %>
merchant_key: <%= ENV['BRASPAG_MERCHANT_KEY'] %>
request_timeout: <%= ENV['BRASPAG_REQUEST_TIMEOUT'] %>
```

### Authorize an order
Expand Down
4 changes: 4 additions & 0 deletions lib/braspag-rest/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def merchant_key
config['merchant_key']
end

def request_timeout
config.fetch('request_timeout', 60)
end

private

def config
Expand Down
33 changes: 29 additions & 4 deletions lib/braspag-rest/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,53 @@ def authorize(request_id, params)
config.logger.info("[BraspagRest][Authorize] endpoint: #{sale_url}, params: #{params.to_json}") if config.log_enabled?

execute_braspag_request do
RestClient.post(sale_url, params.to_json, default_headers.merge('RequestId' => request_id))
RestClient::Request.execute(
method: :post,
url: sale_url,
payload: params.to_json,
headers: default_headers.merge('RequestId' => request_id),
timeout: config.request_timeout
)
end
end

def void(request_id, payment_id, amount)
config.logger.info("[BraspagRest][Void] endpoint: #{void_url(payment_id, amount)}") if config.log_enabled?

execute_braspag_request do
RestClient.put(void_url(payment_id, amount), nil, default_headers.merge('RequestId' => request_id))
RestClient::Request.execute(
method: :put,
url: void_url(payment_id, amount),
headers: default_headers.merge('RequestId' => request_id),
timeout: config.request_timeout
)
end
end

def get_sale(request_id, payment_id)
config.logger.info("[BraspagRest][GetSale] endpoint: #{search_sale_url(payment_id)}") if config.log_enabled?

execute_braspag_request do
RestClient.get(search_sale_url(payment_id), default_headers.merge('RequestId' => request_id))
RestClient::Request.execute(
method: :get,
url: search_sale_url(payment_id),
headers: default_headers.merge('RequestId' => request_id),
timeout: config.request_timeout
)
end
end

def capture(request_id, payment_id, amount)
config.logger.info("[BraspagRest][Capture] endpoint: #{capture_url(payment_id)}, amount: #{amount}") if config.log_enabled?

execute_braspag_request do
RestClient.put(capture_url(payment_id), { Amount: amount }.to_json, default_headers.merge('RequestId' => request_id))
RestClient::Request.execute(
method: :put,
url: capture_url(payment_id),
payload: { Amount: amount }.to_json,
headers: default_headers.merge('RequestId' => request_id),
timeout: config.request_timeout
)
end
end

Expand All @@ -48,6 +70,9 @@ def execute_braspag_request(&block)
rescue RestClient::ResourceNotFound => e
config.logger.error("[BraspagRest][Error] message: #{e.message}, status: #{e.http_code}, body: #{e.http_body.inspect}") if config.log_enabled?
raise
rescue RestClient::RequestTimeout => e
config.logger.error("[BraspagRest][Timeout] message: #{e.message}") if config.log_enabled?
raise
rescue RestClient::ExceptionWithResponse => e
config.logger.warn("[BraspagRest][Error] message: #{e.message}, status: #{e.http_code}, body: #{e.http_body.inspect}") if config.log_enabled?
BraspagRest::Response.new(e.response)
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ test:
merchant_id: "12345678-1234-1234-1234-123456789012"
merchant_key: "1234567890123456789012345678901234567890"
log_enable: true
request_timeout: 60
production:
merchant_key: <%= ENV["BRASPAG_MERCHANT_KEY"] %>
4 changes: 2 additions & 2 deletions spec/hashie/iutrash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class Person < Hashie::IUTrash

it 'nested inverse attributes' do
params['Telephones'] = [
{ 'Code' => '+55', 'Number' => '555-555' },
{ 'Code' => '+1', 'Number' => '222-222' }
{ :code => '+55', :number => '555-555' },
{ :code => '+1', :number => '222-222' }
]
person = Person.new(params)

Expand Down
95 changes: 79 additions & 16 deletions spec/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@
let(:gateway_response) { double(code: 200, body: '{}') }

it 'calls sale creation with request_id and their parameters' do
expect(RestClient).to receive(:post).with(sale_url, params.to_json, headers)
expect(RestClient::Request).to receive(:execute).with(
method: :post,
url: sale_url,
payload: params.to_json,
headers: headers,
timeout: config['request_timeout']
)
described_class.authorize(request_id, params)
end

it 'returns a braspag successful response' do
allow(RestClient).to receive(:post).and_return(gateway_response)
allow(RestClient::Request).to receive(:execute).and_return(gateway_response)

response = described_class.authorize(request_id, params)
expect(response).to be_success
Expand All @@ -53,7 +59,7 @@
let(:gateway_response) { double(code: 400, body: '{}') }

it 'returns a braspag unsuccessful response and log it as a warning' do
allow(RestClient).to receive(:post).and_raise(RestClient::ExceptionWithResponse, gateway_response)
allow(RestClient::Request).to receive(:execute).and_raise(RestClient::ExceptionWithResponse, gateway_response)
expect(logger).to receive(:warn).with("[BraspagRest][Error] message: RestClient::ExceptionWithResponse, status: 400, body: \"{}\"")

response = described_class.authorize(request_id, params)
Expand All @@ -66,12 +72,21 @@
let(:gateway_response) { double(code: 500, body: '{}') }

it 'raises the exception and log it as an error' do
allow(RestClient).to receive(:post).and_raise(RestClient::Exception, gateway_response)
allow(RestClient::Request).to receive(:execute).and_raise(RestClient::Exception, gateway_response)
expect(logger).to receive(:error).with("[BraspagRest][Error] message: RestClient::Exception, status: 500, body: \"{}\"")

expect { described_class.authorize(request_id, params) }.to raise_error(RestClient::Exception)
end
end

context 'when a timeout occurs' do
it 'raises the exception and log it as an error' do
allow(RestClient::Request).to receive(:execute).and_raise(RestClient::RequestTimeout)
expect(logger).to receive(:error).with("[BraspagRest][Timeout] message: Request Timeout")

expect { described_class.authorize(request_id, params) }.to raise_error(RestClient::RequestTimeout)
end
end
end

describe '.void' do
Expand All @@ -93,7 +108,12 @@
let(:void_url) { config['url'] + '/v2/sales/' + payment_id + '/void' }

it 'does not specify an amount to be voided' do
expect(RestClient).to receive(:put).with(void_url, nil, headers)
expect(RestClient::Request).to receive(:execute).with(
method: :put,
url: void_url,
headers: headers,
timeout: config['request_timeout']
)
described_class.void(request_id, payment_id, amount)
end
end
Expand All @@ -103,7 +123,12 @@
let(:amount) { 100 }

it 'includes specific amount to void in request' do
expect(RestClient).to receive(:put).with(void_url, nil, headers)
expect(RestClient::Request).to receive(:execute).with(
method: :put,
url: void_url,
headers: headers,
timeout: config['request_timeout']
)
described_class.void(request_id, payment_id, amount)
end
end
Expand All @@ -112,7 +137,7 @@
let(:gateway_response) { double(code: 200, body: '{}') }

it 'returns a braspag successful response' do
allow(RestClient).to receive(:put).and_return(gateway_response)
allow(RestClient::Request).to receive(:execute).and_return(gateway_response)

response = described_class.void(request_id, payment_id, amount)
expect(response).to be_success
Expand All @@ -124,7 +149,7 @@
let(:gateway_response) { double(code: 400, body: '{}') }

it 'returns a braspag unsuccessful response and log it as a warning' do
allow(RestClient).to receive(:put).and_raise(RestClient::ExceptionWithResponse, gateway_response)
allow(RestClient::Request).to receive(:execute).and_raise(RestClient::ExceptionWithResponse, gateway_response)
expect(logger).to receive(:warn).with("[BraspagRest][Error] message: RestClient::ExceptionWithResponse, status: 400, body: \"{}\"")

response = described_class.void(request_id, payment_id, amount)
Expand All @@ -137,12 +162,21 @@
let(:gateway_response) { double(code: 500, body: '{}') }

it 'raises the exception and log it as an error' do
allow(RestClient).to receive(:put).and_raise(RestClient::Exception, gateway_response)
allow(RestClient::Request).to receive(:execute).and_raise(RestClient::Exception, gateway_response)
expect(logger).to receive(:error).with("[BraspagRest][Error] message: RestClient::Exception, status: 500, body: \"{}\"")

expect { described_class.void(request_id, payment_id, amount) }.to raise_error(RestClient::Exception)
end
end

context 'when a timeout occurs' do
it 'raises the exception and log it as an error' do
allow(RestClient::Request).to receive(:execute).and_raise(RestClient::RequestTimeout)
expect(logger).to receive(:error).with("[BraspagRest][Timeout] message: Request Timeout")

expect { described_class.void(request_id, payment_id, amount) }.to raise_error(RestClient::RequestTimeout)
end
end
end

describe '.get_sale' do
Expand All @@ -164,12 +198,17 @@
let(:gateway_response) { double(code: 200, body: '{}') }

it 'calls sale void with request_id and amount' do
expect(RestClient).to receive(:get).with(search_url, headers)
expect(RestClient::Request).to receive(:execute).with(
method: :get,
url: search_url,
headers: headers,
timeout: config['request_timeout']
)
described_class.get_sale(request_id, payment_id)
end

it 'returns a braspag successful response' do
allow(RestClient).to receive(:get).and_return(gateway_response)
allow(RestClient::Request).to receive(:execute).and_return(gateway_response)

response = described_class.get_sale(request_id, payment_id)
expect(response).to be_success
Expand All @@ -181,12 +220,21 @@
let(:gateway_response) { double(code: 404, body: '{}') }

it 'raises the exception and log it as an error' do
allow(RestClient).to receive(:get).and_raise(RestClient::ResourceNotFound)
allow(RestClient::Request).to receive(:execute).and_raise(RestClient::ResourceNotFound)
expect(logger).to receive(:error).with("[BraspagRest][Error] message: Resource Not Found, status: , body: nil")

expect { described_class.get_sale(request_id, payment_id) }.to raise_error(RestClient::ResourceNotFound)
end
end

context 'when a timeout occurs' do
it 'raises the exception and log it as an error' do
allow(RestClient::Request).to receive(:execute).and_raise(RestClient::RequestTimeout)
expect(logger).to receive(:error).with("[BraspagRest][Timeout] message: Request Timeout")

expect { described_class.get_sale(request_id, payment_id) }.to raise_error(RestClient::RequestTimeout)
end
end
end

describe '.capture' do
Expand All @@ -209,12 +257,18 @@
let(:gateway_response) { double(code: 200, body: '{}') }

it 'calls sale capture with request_id and amount' do
expect(RestClient).to receive(:put).with(capture_url, { Amount: amount }.to_json, headers)
expect(RestClient::Request).to receive(:execute).with(
method: :put,
url: capture_url,
payload: { Amount: amount }.to_json,
headers: headers,
timeout: config['request_timeout']
)
described_class.capture(request_id, payment_id, amount)
end

it 'returns a braspag successful response' do
allow(RestClient).to receive(:put).and_return(gateway_response)
allow(RestClient::Request).to receive(:execute).and_return(gateway_response)

response = described_class.capture(request_id, payment_id, amount)
expect(response).to be_success
Expand All @@ -226,7 +280,7 @@
let(:gateway_response) { double(code: 400, body: '{}') }

it 'returns a braspag unsuccessful response and log it as a warning' do
allow(RestClient).to receive(:put).and_raise(RestClient::ExceptionWithResponse, gateway_response)
allow(RestClient::Request).to receive(:execute).and_raise(RestClient::ExceptionWithResponse, gateway_response)
expect(logger).to receive(:warn).with("[BraspagRest][Error] message: RestClient::ExceptionWithResponse, status: 400, body: \"{}\"")

response = described_class.capture(request_id, payment_id, amount)
Expand All @@ -239,11 +293,20 @@
let(:gateway_response) { double(code: 500, body: '{}') }

it 'raises the exception and log it as an error' do
allow(RestClient).to receive(:put).and_raise(RestClient::Exception, gateway_response)
allow(RestClient::Request).to receive(:execute).and_raise(RestClient::Exception, gateway_response)
expect(logger).to receive(:error).with("[BraspagRest][Error] message: RestClient::Exception, status: 500, body: \"{}\"")

expect { described_class.capture(request_id, payment_id, amount) }.to raise_error(RestClient::Exception)
end
end

context 'when a timeout occurs' do
it 'raises the exception and log it as an error' do
allow(RestClient::Request).to receive(:execute).and_raise(RestClient::RequestTimeout)
expect(logger).to receive(:error).with("[BraspagRest][Timeout] message: Request Timeout")

expect { described_class.capture(request_id, payment_id, amount) }.to raise_error(RestClient::RequestTimeout)
end
end
end
end

0 comments on commit eff541c

Please sign in to comment.