bs2_api 0.3.3 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2b8b144d4d8f5045e87461029c823e9b5f5a7c3d5a9191f1e39a542ac5d7400
4
- data.tar.gz: c9bcf3bb230b56e8a0ac0d0e2d4373ec37a49869bc1918597b36dd57d1ff5bab
3
+ metadata.gz: fedfacb857654251513141faee3fe9594e888c3d10f6de512016d4902f532e17
4
+ data.tar.gz: 5d424e8fd30eeeeb261470ac5aef0e4bc09cf511aafe9f221f8f684a684477b6
5
5
  SHA512:
6
- metadata.gz: d4a1c8e8823be43d5c6f8a69b4817552e58e3e1a82d81780ed3a181ecae7449d6da2f0235bda18d36b2632dc7e58d06346624690efc9ed6f1b46ef526167572f
7
- data.tar.gz: 242287b0695c77833428867361fb1a469eb912ffa0b387c8443f8fbba3db23a2bdd4aa1dbc6e9e172084166fb087ff8fbe9ff19a0cdc6260a12449963ecc7e86
6
+ metadata.gz: ac0eb6711af976b5354a2f4a8b61e1aa63f7818df87601f7234390788b88621176dde42e1c34d6c175ba2a4801c783750bb47b2372a3e6d62f93112480d2864d
7
+ data.tar.gz: 0ecd2596091a8a4c664307e4f9d40e7c0a95fb113466d97bb3ac59c8e45e9914630e4a590bbba0813bce72a53d08e921af61f036152a02f711a9f78dcfd5ddd1
@@ -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
@@ -1,16 +1,23 @@
1
- ## [0.3.1] - 2021-06-16
1
+ ## [1.0.1] - 2021-06-25
2
+ - Adjust HTTP response code when create payments into BS2
3
+
4
+ ## [1.0.0] - 2021-06-18
5
+ - Changed payment.id to payment.payment_id
6
+ - Changed payment.merchant_id to payment.end_to_end_id
2
7
 
8
+ ## [0.4.0] - 2021-06-17
9
+ - Configuration#valid? missing return true
10
+ - Created Util::Response.parse_error
11
+ - Bs2Api::Payment::Confirmation#success? was private, moved it to public
12
+
13
+ ## [0.3.1] - 2021-06-16
3
14
  - Method Hash#to_query conflicting with Rails
4
15
 
5
16
  ## [0.3.0] - 2021-06-16
6
-
7
17
  - Confirmation, Adjust README.md
8
18
 
9
19
  ## [0.2.0] - 2021-06-15
10
-
11
20
  - Entities, Auth, Create Pix Key Payment
12
21
 
13
-
14
22
  ## [0.1.0] - 2021-06-11
15
-
16
23
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bs2_api (0.3.2)
4
+ bs2_api (1.0.1)
5
5
  activesupport
6
6
  builder
7
7
  bundler
@@ -11,7 +11,7 @@ PATH
11
11
  GEM
12
12
  remote: https://rubygems.org/
13
13
  specs:
14
- activesupport (6.1.3.2)
14
+ activesupport (6.1.4)
15
15
  concurrent-ruby (~> 1.0, >= 1.0.2)
16
16
  i18n (>= 1.6, < 2)
17
17
  minitest (>= 5.1)
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
@@ -61,10 +66,10 @@ pix_key = Bs2Api::Entities::PixKey.new(
61
66
  # Veja abaixo (Classes de errors) quais erros que podem ser lançados
62
67
  pay_key = Bs2Api::Payment::Key.new(pix_key).call
63
68
 
64
- pay_key.payment.id
69
+ pay_key.payment.payment_idid
65
70
  => "96f0b3c4-4c76-4a7a-9933-9c9f86df7490" # pagamentoId gerado no BS2
66
71
 
67
- pay_key.payment.merchantId
72
+ pay_key.payment.end_to_end_id
68
73
  => "E710278662021061618144401750781P" # endToEndId gerado no BS2
69
74
 
70
75
  ```
@@ -93,40 +98,41 @@ receiver_bank = Bs2Api::Entities::Bank.new(
93
98
 
94
99
  pay_manual = Bs2Api::Payment::Manual.new(receiver_bank).call
95
100
 
96
- pay_manual.payment.id
101
+ pay_manual.payment.payment_id
97
102
  => "96f0b3c4-4c76-4a7a-9933-9c9f86df7490" # UUID gerado no BS2
98
103
 
99
- pay_manual.payment.merchantId
104
+ pay_manual.payment.end_to_end_id
100
105
  => "E710278662021061618144401750781P" # endToEndId gerado no BS2
101
106
  ```
102
107
 
103
108
  ### Confirmar ordem de transferência
104
- ```ruby
105
- # Após criar um Payment é necessário confirmar
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
- # Tanto a ordem de transferência via chave ou manual tem o mesmo payment.
109
- # Caso tenha criado via chave no passo anterior:
110
- payment = pay_key.payment
112
+ ```ruby
111
113
 
112
- # Ou caso tenha criado a ordem via Manual no passo anterior:
113
- payment = pay_manual.payment
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 de problema, um erro será lançado.
123
+ # Caso a confirmação problema, um erro será lançado.
119
124
  raise Bs2Api::Errors::ConfirmationError
120
125
 
121
- # Caso nenhum erro seja lançado (já é sucesso) porém, você pode ter certeza com
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
137
-
138
- # Caso não queira tratar um erro em específico basta fazer rescue do Base
139
- rescue Bs2Api::Errors::Base => e
140
- puts "Erro: #{e.message}"
141
- end
142
143
  ```
144
+
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
@@ -13,6 +13,8 @@ Gem::Specification.new do |spec|
13
13
  spec.homepage = "https://github.com/latamgateway/bs2_api"
14
14
  spec.required_ruby_version = "~> 2.7", "< 3"
15
15
 
16
+ spec.license = "MIT"
17
+
16
18
  spec.metadata["homepage_uri"] = spec.homepage
17
19
  spec.metadata["source_code_uri"] = "https://github.com/latamgateway/bs2_api"
18
20
  spec.metadata["changelog_uri"] = "https://github.com/latamgateway/bs2_api/blob/main/CHANGELOG.md"
data/lib/bs2_api.rb CHANGED
@@ -8,6 +8,9 @@ require "active_support/core_ext/object/blank"
8
8
  require 'bs2_api/version'
9
9
  require 'bs2_api/configuration'
10
10
 
11
+ require 'bs2_api/util/bank_service'
12
+ require 'bs2_api/util/response'
13
+
11
14
  require 'bs2_api/errors/base'
12
15
  require 'bs2_api/errors/invalid_pix_key'
13
16
  require 'bs2_api/errors/invalid_bank'
@@ -32,8 +35,6 @@ require 'bs2_api/payment/confirmation'
32
35
 
33
36
  require 'bs2_api/request/auth'
34
37
 
35
- require 'bs2_api/util/bank_service'
36
-
37
38
  module Bs2Api
38
39
  ENDPOINT = {
39
40
  production: 'https://api.bs2.com',
@@ -11,5 +11,7 @@ class Configuration
11
11
 
12
12
  def valid?
13
13
  raise Bs2Api::Errors::MissingConfiguration, 'Missing configuration credentials' if @client_id.blank? || @client_secret.blank?
14
+
15
+ true
14
16
  end
15
17
  end
@@ -3,22 +3,22 @@
3
3
  module Bs2Api
4
4
  module Entities
5
5
  class Payment
6
- attr_accessor :id, :merchant_id, :receiver, :payer
6
+ attr_accessor :payment_id, :end_to_end_id, :receiver, :payer
7
7
 
8
8
  def initialize(args = {})
9
- @id = args.fetch(:id, nil)
10
- @merchant_id = args.fetch(:merchant_id, nil)
11
- @receiver = args.fetch(:receiver, nil)
12
- @payer = args.fetch(:payer, nil)
9
+ @payment_id = args.fetch(:payment_id, nil)
10
+ @end_to_end_id = args.fetch(:end_to_end_id, nil)
11
+ @receiver = args.fetch(:receiver, nil)
12
+ @payer = args.fetch(:payer, nil)
13
13
  end
14
14
 
15
15
  def to_hash
16
16
  ActiveSupport::HashWithIndifferentAccess.new(
17
17
  {
18
- "pagamentoId": @id,
19
- "endToEndId": @merchant_id,
20
- "recebedor": @receiver.to_hash,
21
- "pagador": @payer.to_hash
18
+ "pagamentoId": @payment_id,
19
+ "endToEndId": @end_to_end_id,
20
+ "recebedor": @receiver.to_hash,
21
+ "pagador": @payer.to_hash
22
22
  }
23
23
  )
24
24
  end
@@ -27,10 +27,10 @@ module Bs2Api
27
27
  hash = ActiveSupport::HashWithIndifferentAccess.new(hash_payload)
28
28
 
29
29
  Bs2Api::Entities::Payment.new(
30
- id: hash["pagamentoId"],
31
- merchant_id: hash["endToEndId"],
32
- receiver: Bs2Api::Entities::Bank.from_response(hash["recebedor"]),
33
- payer: Bs2Api::Entities::Bank.from_response(hash["pagador"])
30
+ payment_id: hash["pagamentoId"],
31
+ end_to_end_id: hash["endToEndId"],
32
+ receiver: Bs2Api::Entities::Bank.from_response(hash["recebedor"]),
33
+ payer: Bs2Api::Entities::Bank.from_response(hash["pagador"])
34
34
  )
35
35
  end
36
36
  end
@@ -9,8 +9,8 @@ module Bs2Api
9
9
 
10
10
  def call
11
11
  response = post_request
12
- raise Bs2Api::Errors::BadRequest, parse_error(response) unless response.created?
13
-
12
+ raise Bs2Api::Errors::BadRequest, ::Util::Response.parse_error(response) unless response.ok?
13
+
14
14
  @payment = Bs2Api::Entities::Payment.from_response(response)
15
15
  self
16
16
  end
@@ -32,28 +32,13 @@ 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
53
-
38
+
54
39
  def url
55
40
  raise NoMethodError, "Missing #{__method__} to #{self.class}"
56
41
  end
57
42
  end
58
43
  end
59
- end
44
+ 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
@@ -27,11 +31,7 @@ module Bs2Api
27
31
  end
28
32
 
29
33
  def url
30
- "#{Bs2Api.endpoint}/pix/direto/forintegration/v1/pagamentos/#{@payment.id}/confirmacao"
31
- end
32
-
33
- def success?
34
- !!@success
34
+ "#{Bs2Api.endpoint}/pix/direto/forintegration/v1/pagamentos/#{@payment.payment_id}/confirmacao"
35
35
  end
36
36
  end
37
37
  end
@@ -39,7 +39,7 @@ module Bs2Api
39
39
  def body
40
40
  {
41
41
  grant_type: "client_credentials",
42
- scope: "pix.write%20pix.read"
42
+ scope: "pix.write pix.read"
43
43
  }.to_query
44
44
  end
45
45
 
@@ -12,10 +12,9 @@ module Bs2Api
12
12
  bank
13
13
  end
14
14
 
15
- private
16
- def bank_list
17
- @bank_list ||= YAML.load_file(File.join(__dir__, 'banks.yml'))
18
- end
15
+ def bank_list
16
+ @bank_list ||= YAML.load_file(File.join(__dir__, 'banks.yml'))
17
+ end
19
18
  end
20
19
  end
21
20
  end
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bs2Api
4
- VERSION = "0.3.3"
4
+ VERSION = "1.0.2"
5
5
  end
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.3.3
4
+ version: 1.0.2
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-17 00:00:00.000000000 Z
11
+ date: 2021-07-06 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"
@@ -221,9 +222,11 @@ files:
221
222
  - lib/bs2_api/request/auth.rb
222
223
  - lib/bs2_api/util/bank_service.rb
223
224
  - lib/bs2_api/util/banks.yml
225
+ - lib/bs2_api/util/response.rb
224
226
  - lib/bs2_api/version.rb
225
227
  homepage: https://github.com/latamgateway/bs2_api
226
- licenses: []
228
+ licenses:
229
+ - MIT
227
230
  metadata:
228
231
  homepage_uri: https://github.com/latamgateway/bs2_api
229
232
  source_code_uri: https://github.com/latamgateway/bs2_api