bs2_api 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +34 -0
- data/CHANGELOG.md +7 -1
- data/Gemfile.lock +2 -1
- data/README.md +27 -20
- data/bs2_api.gemspec +6 -4
- data/lib/bs2_api.rb +8 -8
- data/lib/bs2_api/configuration.rb +2 -0
- data/lib/bs2_api/payment/base.rb +1 -16
- data/lib/bs2_api/payment/confirmation.rb +5 -5
- data/lib/bs2_api/request/auth.rb +2 -2
- data/lib/bs2_api/util/response.rb +20 -0
- data/lib/bs2_api/version.rb +1 -1
- metadata +15 -11
- data/lib/bs2_api/initializers/hash.rb +0 -9
- data/lib/bs2_api/initializers/object.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e00122261c7f101e2aa06cd712a97dffc16c9217efff07c83eab99cff74110fe
|
4
|
+
data.tar.gz: fbbaae2c421c022b774601e94e10cc25cc2324770e7db7621c5bd6e173184e63
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c146dc459d908d337fd1920522d70498a985f5fac4a64f2daecc491b43436dbe462564a66d91467f90420edb0f2ed3390e88c9dd7af74d910d6cffe12ed7111
|
7
|
+
data.tar.gz: f4a492028c841aca65b635d6a12ba2dd9500b656fd3eae6dd7df88d0480b3104c5640ae5eb3bc8647ff453b18a04018e3bdae85e6afa917932ec5c57c6771795
|
@@ -0,0 +1,34 @@
|
|
1
|
+
name: bs2_api test
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ main ]
|
6
|
+
pull_request:
|
7
|
+
types: [ opened, synchronize, reopened ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
env:
|
13
|
+
BS2_ENVIRONMENT: 'sandbox'
|
14
|
+
BS2_CLIENT_ID: '123'
|
15
|
+
BS2_CLIENT_SECRET: '123'
|
16
|
+
BS2_EVP_TEST_KEY: '123'
|
17
|
+
|
18
|
+
strategy:
|
19
|
+
matrix:
|
20
|
+
ruby-version: ["2.7.2"]
|
21
|
+
|
22
|
+
steps:
|
23
|
+
- uses: actions/checkout@v2
|
24
|
+
|
25
|
+
- name: Set up Ruby
|
26
|
+
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
|
27
|
+
with:
|
28
|
+
ruby-version: ${{ matrix.ruby-version }}
|
29
|
+
|
30
|
+
- name: Install dependencies
|
31
|
+
run: bundle install
|
32
|
+
|
33
|
+
- name: Run tests
|
34
|
+
run: bundle exec rspec spec/
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
[![CI status](https://github.com/latamgateway/bs2_api/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/latamgateway/bs2_api/actions/workflows/test.yml)
|
2
|
+
[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/latamgateway/bs2_api.svg?style=flat-square)](http://github.com/latamgateway/bs2_api/releases)
|
3
|
+
[![Version](https://img.shields.io/gem/v/bs2_api.svg?style=flat-square)](https://rubygems.org/gems/bs2_api)
|
4
|
+
[![GitHub](https://img.shields.io/github/license/latamgateway/bs2_api?style=flat-square)](https://github.com/latamgateway/bs2_api/blob/main/LICENSE)
|
5
|
+
|
1
6
|
# Bs2Api
|
2
7
|
|
3
8
|
Integração com a API do Banco BS2: https://devs.bs2.com/manual/pix-clientes
|
@@ -43,9 +48,9 @@ ou via initializer:
|
|
43
48
|
|
44
49
|
```ruby
|
45
50
|
Bs2Api.configure do |config|
|
46
|
-
config.client_id
|
47
|
-
config.client_secret
|
48
|
-
config.
|
51
|
+
config.client_id = 'you_bs2_client_id'
|
52
|
+
config.client_secret = 'you_bs2_client_secret'
|
53
|
+
config.env = 'sandbox' # ou production
|
49
54
|
end
|
50
55
|
```
|
51
56
|
|
@@ -64,7 +69,7 @@ pay_key = Bs2Api::Payment::Key.new(pix_key).call
|
|
64
69
|
pay_key.payment.id
|
65
70
|
=> "96f0b3c4-4c76-4a7a-9933-9c9f86df7490" # pagamentoId gerado no BS2
|
66
71
|
|
67
|
-
pay_key.payment.
|
72
|
+
pay_key.payment.merchant_id
|
68
73
|
=> "E710278662021061618144401750781P" # endToEndId gerado no BS2
|
69
74
|
|
70
75
|
```
|
@@ -101,32 +106,33 @@ pay_manual.payment.merchantId
|
|
101
106
|
```
|
102
107
|
|
103
108
|
### Confirmar ordem de transferência
|
104
|
-
|
105
|
-
|
106
|
-
# Nessa etapa o dinheiro é de fato transferido
|
109
|
+
Após criar um Payment é necessário confirmar, nessa etapa o dinheiro é de fato transferido.
|
110
|
+
Nessa etapa é necessário **informar o valor** que deseja ser transferido.
|
107
111
|
|
108
|
-
|
109
|
-
# Caso tenha criado via chave no passo anterior:
|
110
|
-
payment = pay_key.payment
|
112
|
+
```ruby
|
111
113
|
|
112
|
-
#
|
113
|
-
|
114
|
+
# Ambos modelos de criação da ordem de pagamento possuem o mesmo objeto payment
|
115
|
+
# podendo ser utilizado da mesma forma nos dois casos:
|
116
|
+
# pay_key.payment ou pay_manual.payment
|
114
117
|
|
118
|
+
payment = pay_key.payment
|
115
119
|
amount = 10.50
|
120
|
+
|
116
121
|
confirmation = Bs2Api::Payment::Confirmation.new(payment, value: amount).call
|
117
122
|
|
118
|
-
# Caso a confirmação
|
123
|
+
# Caso a confirmação dê problema, um erro será lançado.
|
119
124
|
raise Bs2Api::Errors::ConfirmationError
|
120
125
|
|
121
|
-
# Caso nenhum erro seja lançado
|
126
|
+
# Caso nenhum erro seja lançado significa que foi sucesso. Você pode ter certeza com
|
122
127
|
confirmation.success?
|
123
128
|
```
|
124
|
-
|
129
|
+
|
125
130
|
### Classes de erros:
|
126
131
|
```ruby
|
127
132
|
# Todos erros herdam de:
|
128
133
|
Bs2Api::Errors::Base
|
129
134
|
|
135
|
+
# Errors possíveis de serem lançados
|
130
136
|
Bs2Api::Errors::BadRequest
|
131
137
|
Bs2Api::Errors::ConfirmationError
|
132
138
|
Bs2Api::Errors::InvalidCustomer
|
@@ -134,9 +140,10 @@ Bs2Api::Errors::InvalidPixKey
|
|
134
140
|
Bs2Api::Errors::MissingConfiguration
|
135
141
|
Bs2Api::Errors::ServerError
|
136
142
|
Bs2Api::Errors::Unauthorized
|
143
|
+
```
|
137
144
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
145
|
+
---
|
146
|
+
|
147
|
+
### Observações
|
148
|
+
- Método `call` retorna o próprio objeto
|
149
|
+
- Em caso de retorno diferente de sucesso na comunicação com a API do Bs2, um erro sempre será lançado.
|
data/bs2_api.gemspec
CHANGED
@@ -10,12 +10,14 @@ Gem::Specification.new do |spec|
|
|
10
10
|
|
11
11
|
spec.summary = "Integração com a API do BS2"
|
12
12
|
spec.description = "Fazer transferências via PIX"
|
13
|
-
spec.homepage = "https://github.com/
|
14
|
-
spec.required_ruby_version = "
|
13
|
+
spec.homepage = "https://github.com/latamgateway/bs2_api"
|
14
|
+
spec.required_ruby_version = "~> 2.7", "< 3"
|
15
|
+
|
16
|
+
spec.license = "MIT"
|
15
17
|
|
16
18
|
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
-
spec.metadata["source_code_uri"] = "https://github.com/
|
18
|
-
spec.metadata["changelog_uri"] = "https://github.com/
|
19
|
+
spec.metadata["source_code_uri"] = "https://github.com/latamgateway/bs2_api"
|
20
|
+
spec.metadata["changelog_uri"] = "https://github.com/latamgateway/bs2_api/blob/main/CHANGELOG.md"
|
19
21
|
|
20
22
|
# Specify which files should be added to the gem when it is released.
|
21
23
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
data/lib/bs2_api.rb
CHANGED
@@ -2,12 +2,14 @@
|
|
2
2
|
require "httparty"
|
3
3
|
require "active_support/core_ext/hash/indifferent_access"
|
4
4
|
require "active_support/core_ext/hash/except"
|
5
|
+
require "active_support/core_ext/object/to_query"
|
6
|
+
require "active_support/core_ext/object/blank"
|
5
7
|
|
6
8
|
require 'bs2_api/version'
|
7
9
|
require 'bs2_api/configuration'
|
8
10
|
|
9
|
-
require 'bs2_api/
|
10
|
-
require 'bs2_api/
|
11
|
+
require 'bs2_api/util/bank_service'
|
12
|
+
require 'bs2_api/util/response'
|
11
13
|
|
12
14
|
require 'bs2_api/errors/base'
|
13
15
|
require 'bs2_api/errors/invalid_pix_key'
|
@@ -33,11 +35,9 @@ require 'bs2_api/payment/confirmation'
|
|
33
35
|
|
34
36
|
require 'bs2_api/request/auth'
|
35
37
|
|
36
|
-
require 'bs2_api/util/bank_service'
|
37
|
-
|
38
38
|
module Bs2Api
|
39
39
|
ENDPOINT = {
|
40
|
-
production: 'https://api.bs2.com
|
40
|
+
production: 'https://api.bs2.com',
|
41
41
|
sandbox: 'https://apihmz.bancobonsucesso.com.br'
|
42
42
|
}
|
43
43
|
|
@@ -47,7 +47,7 @@ module Bs2Api
|
|
47
47
|
def configuration
|
48
48
|
@configuration ||= Configuration.new
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
def configure
|
52
52
|
yield(configuration)
|
53
53
|
end
|
@@ -59,7 +59,7 @@ module Bs2Api
|
|
59
59
|
def production?
|
60
60
|
env == 'production'
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
def sandbox?
|
64
64
|
env == 'sandbox'
|
65
65
|
end
|
@@ -68,4 +68,4 @@ module Bs2Api
|
|
68
68
|
configuration.env
|
69
69
|
end
|
70
70
|
end
|
71
|
-
end
|
71
|
+
end
|
data/lib/bs2_api/payment/base.rb
CHANGED
@@ -9,7 +9,7 @@ module Bs2Api
|
|
9
9
|
|
10
10
|
def call
|
11
11
|
response = post_request
|
12
|
-
raise Bs2Api::Errors::BadRequest, parse_error(response) unless response.created?
|
12
|
+
raise Bs2Api::Errors::BadRequest, ::Util::Response.parse_error(response) unless response.created?
|
13
13
|
|
14
14
|
@payment = Bs2Api::Entities::Payment.from_response(response)
|
15
15
|
self
|
@@ -32,21 +32,6 @@ module Bs2Api
|
|
32
32
|
Bs2Api::Request::Auth.token
|
33
33
|
end
|
34
34
|
|
35
|
-
def parse_error(response)
|
36
|
-
hash = JSON.parse(response.body)
|
37
|
-
message = "#{response.code}: "
|
38
|
-
|
39
|
-
if hash.is_a?(Array)
|
40
|
-
message << hash[0]["descricao"]
|
41
|
-
elsif hash.key?("error_description")
|
42
|
-
message << hash["error_description"]
|
43
|
-
else
|
44
|
-
message << hash.to_s
|
45
|
-
end
|
46
|
-
|
47
|
-
message
|
48
|
-
end
|
49
|
-
|
50
35
|
def payload
|
51
36
|
raise NoMethodError, "Missing #{__method__} to #{self.class}"
|
52
37
|
end
|
@@ -13,12 +13,16 @@ module Bs2Api
|
|
13
13
|
|
14
14
|
def call
|
15
15
|
response = post_request
|
16
|
-
raise Bs2Api::Errors::ConfirmationError, parse_error(response) unless response.accepted?
|
16
|
+
raise Bs2Api::Errors::ConfirmationError, ::Util::Response.parse_error(response) unless response.accepted?
|
17
17
|
|
18
18
|
@success = true
|
19
19
|
self
|
20
20
|
end
|
21
21
|
|
22
|
+
def success?
|
23
|
+
!!@success
|
24
|
+
end
|
25
|
+
|
22
26
|
private
|
23
27
|
def payload
|
24
28
|
@payment.to_hash
|
@@ -29,10 +33,6 @@ module Bs2Api
|
|
29
33
|
def url
|
30
34
|
"#{Bs2Api.endpoint}/pix/direto/forintegration/v1/pagamentos/#{@payment.id}/confirmacao"
|
31
35
|
end
|
32
|
-
|
33
|
-
def success?
|
34
|
-
!!@success
|
35
|
-
end
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
data/lib/bs2_api/request/auth.rb
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Util
|
2
|
+
class Response
|
3
|
+
class << self
|
4
|
+
def parse_error res
|
5
|
+
hash = JSON.parse(res.body)
|
6
|
+
message = "#{res.code}: "
|
7
|
+
|
8
|
+
if hash.is_a?(Array)
|
9
|
+
message << hash[0]["descricao"]
|
10
|
+
elsif hash.key?("error_description")
|
11
|
+
message << hash["error_description"]
|
12
|
+
else
|
13
|
+
message << hash.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
message
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/bs2_api/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bs2_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kim Pastro
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-06-
|
11
|
+
date: 2021-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: builder
|
@@ -184,6 +184,7 @@ extensions: []
|
|
184
184
|
extra_rdoc_files: []
|
185
185
|
files:
|
186
186
|
- ".env.example"
|
187
|
+
- ".github/workflows/test.yml"
|
187
188
|
- ".gitignore"
|
188
189
|
- ".rspec"
|
189
190
|
- ".ruby-version"
|
@@ -214,8 +215,6 @@ files:
|
|
214
215
|
- lib/bs2_api/errors/missing_configuration.rb
|
215
216
|
- lib/bs2_api/errors/server_error.rb
|
216
217
|
- lib/bs2_api/errors/unauthorized.rb
|
217
|
-
- lib/bs2_api/initializers/hash.rb
|
218
|
-
- lib/bs2_api/initializers/object.rb
|
219
218
|
- lib/bs2_api/payment/base.rb
|
220
219
|
- lib/bs2_api/payment/confirmation.rb
|
221
220
|
- lib/bs2_api/payment/key.rb
|
@@ -223,22 +222,27 @@ files:
|
|
223
222
|
- lib/bs2_api/request/auth.rb
|
224
223
|
- lib/bs2_api/util/bank_service.rb
|
225
224
|
- lib/bs2_api/util/banks.yml
|
225
|
+
- lib/bs2_api/util/response.rb
|
226
226
|
- lib/bs2_api/version.rb
|
227
|
-
homepage: https://github.com/
|
228
|
-
licenses:
|
227
|
+
homepage: https://github.com/latamgateway/bs2_api
|
228
|
+
licenses:
|
229
|
+
- MIT
|
229
230
|
metadata:
|
230
|
-
homepage_uri: https://github.com/
|
231
|
-
source_code_uri: https://github.com/
|
232
|
-
changelog_uri: https://github.com/
|
231
|
+
homepage_uri: https://github.com/latamgateway/bs2_api
|
232
|
+
source_code_uri: https://github.com/latamgateway/bs2_api
|
233
|
+
changelog_uri: https://github.com/latamgateway/bs2_api/blob/main/CHANGELOG.md
|
233
234
|
post_install_message:
|
234
235
|
rdoc_options: []
|
235
236
|
require_paths:
|
236
237
|
- lib
|
237
238
|
required_ruby_version: !ruby/object:Gem::Requirement
|
238
239
|
requirements:
|
239
|
-
- - "
|
240
|
+
- - "~>"
|
241
|
+
- !ruby/object:Gem::Version
|
242
|
+
version: '2.7'
|
243
|
+
- - "<"
|
240
244
|
- !ruby/object:Gem::Version
|
241
|
-
version:
|
245
|
+
version: '3'
|
242
246
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
243
247
|
requirements:
|
244
248
|
- - ">="
|