bs2_api 0.1.0 → 0.2.0

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: 9c1e001ddc04c41bef41d3e70d36e8855ccbf8c2f798bcf780053d0e832e924b
4
- data.tar.gz: 5eb113c82664566db0fbc4ac9ade7023a55ff7cf7f44820fb7965ec132fc3922
3
+ metadata.gz: b46010721b6fd642ba8db84aff501d9a29ecf78faacf023c4dc844ff50d58b41
4
+ data.tar.gz: '0982876fca9f7c65f23c58286d9f60bcb01217d934287b38505fd21d2000f9c2'
5
5
  SHA512:
6
- metadata.gz: cc1f09dbd72fe994bf3703eb57244cea11ee52cdc83f0b96277af64bb8c385fbf0b1f692d3d513202847fa359c1a336ba17aa1e2df5cb02966e83099718c5442
7
- data.tar.gz: 0ab3b8f4202173d42d3ddcef476489e1378f05df4d4d9c632c7141617385e0592e7f15a1615920c8af5c8fd4d3d87c52f59781c361a340ce83a609c2cd4de25c
6
+ metadata.gz: 6fcee38ab1c4c3c3ada7eb9e2ada3f8090223ad0d28e0797a7494bd8c00bd11a9d828e2ccc545f9ad23278d9ac86b124116f6b604e7cc476ad5aab08cc5c074a
7
+ data.tar.gz: 98e9b97e08e256ee31145a404758702dbd40c6a62f12d018fcf1e0f2de9456efb3604d25fe8361b43d004104003d96ba237020b7afa690d1bf11088c8646c7cf
data/.env.example ADDED
@@ -0,0 +1,3 @@
1
+ BS2_ENVIRONMENT=sandbox
2
+ BS2_CLIENT_ID=123
3
+ BS2_CLIENT_SECRET=123
data/.gitignore CHANGED
@@ -9,3 +9,7 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+
13
+ *.gem
14
+
15
+ .env.test
data/Gemfile CHANGED
@@ -5,4 +5,9 @@ source "https://rubygems.org"
5
5
  # Specify your gem's dependencies in bs2_api.gemspec
6
6
  gemspec
7
7
 
8
- gem "rake", "~> 13.0"
8
+ gem "rake", "~> 13.0"
9
+
10
+ group :test do
11
+ gem "pry-byebug", "~> 3.9"
12
+ gem 'uuid', '~> 2.3', '>= 2.3.9'
13
+ end
data/Gemfile.lock CHANGED
@@ -1,34 +1,60 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bs2_api (0.1.0)
5
- http (~> 5.0)
4
+ bs2_api (0.2.0)
5
+ activesupport (~> 6.1, >= 6.1.3.2)
6
+ http (~> 4.4)
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
9
10
  specs:
11
+ activesupport (6.1.3.2)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 1.6, < 2)
14
+ minitest (>= 5.1)
15
+ tzinfo (~> 2.0)
16
+ zeitwerk (~> 2.3)
10
17
  addressable (2.7.0)
11
18
  public_suffix (>= 2.0.2, < 5.0)
19
+ byebug (11.1.3)
20
+ coderay (1.1.3)
21
+ concurrent-ruby (1.1.9)
22
+ crack (0.4.5)
23
+ rexml
12
24
  diff-lcs (1.4.4)
13
25
  domain_name (0.5.20190701)
14
26
  unf (>= 0.0.5, < 1.0.0)
27
+ dotenv (2.7.6)
15
28
  ffi (1.15.1)
16
29
  ffi-compiler (1.0.1)
17
30
  ffi (>= 1.0.0)
18
31
  rake
19
- http (5.0.0)
32
+ hashdiff (1.0.1)
33
+ http (4.4.1)
20
34
  addressable (~> 2.3)
21
35
  http-cookie (~> 1.0)
22
36
  http-form_data (~> 2.2)
23
- llhttp-ffi (~> 0.0.1)
37
+ http-parser (~> 1.2.0)
24
38
  http-cookie (1.0.4)
25
39
  domain_name (~> 0.5)
26
40
  http-form_data (2.3.0)
27
- llhttp-ffi (0.0.1)
28
- ffi-compiler (~> 1.0)
29
- rake (~> 13.0)
41
+ http-parser (1.2.3)
42
+ ffi-compiler (>= 1.0, < 2.0)
43
+ i18n (1.8.10)
44
+ concurrent-ruby (~> 1.0)
45
+ macaddr (1.7.2)
46
+ systemu (~> 2.6.5)
47
+ method_source (1.0.0)
48
+ minitest (5.14.4)
49
+ pry (0.13.1)
50
+ coderay (~> 1.1)
51
+ method_source (~> 1.0)
52
+ pry-byebug (3.9.0)
53
+ byebug (~> 11.0)
54
+ pry (~> 0.13.0)
30
55
  public_suffix (4.0.6)
31
56
  rake (13.0.3)
57
+ rexml (3.2.5)
32
58
  rspec (3.10.0)
33
59
  rspec-core (~> 3.10.0)
34
60
  rspec-expectations (~> 3.10.0)
@@ -42,17 +68,33 @@ GEM
42
68
  diff-lcs (>= 1.2.0, < 2.0)
43
69
  rspec-support (~> 3.10.0)
44
70
  rspec-support (3.10.2)
71
+ systemu (2.6.5)
72
+ tzinfo (2.0.4)
73
+ concurrent-ruby (~> 1.0)
45
74
  unf (0.1.4)
46
75
  unf_ext
47
76
  unf_ext (0.0.7.7)
77
+ uuid (2.3.9)
78
+ macaddr (~> 1.0)
79
+ vcr (6.0.0)
80
+ webmock (3.13.0)
81
+ addressable (>= 2.3.6)
82
+ crack (>= 0.3.2)
83
+ hashdiff (>= 0.4.0, < 2.0.0)
84
+ zeitwerk (2.4.2)
48
85
 
49
86
  PLATFORMS
50
87
  x86_64-linux
51
88
 
52
89
  DEPENDENCIES
53
90
  bs2_api!
91
+ dotenv (~> 2.7, >= 2.7.6)
92
+ pry-byebug (~> 3.9)
54
93
  rake (~> 13.0)
55
94
  rspec (~> 3.10)
95
+ uuid (~> 2.3, >= 2.3.9)
96
+ vcr (~> 6.0)
97
+ webmock (~> 3.13)
56
98
 
57
99
  BUNDLED WITH
58
100
  2.2.18
data/Makefile ADDED
@@ -0,0 +1,2 @@
1
+ console:
2
+ ruby bin/console
data/README.md CHANGED
@@ -1,35 +1,144 @@
1
1
  # Bs2Api
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/bs2_api`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Integração com a API do Banco BS2: https://devs.bs2.com/manual/pix-clientes
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
5
+ ## Instalação
10
6
 
7
+ Adicionar no seu Gemfile:
11
8
  ```ruby
12
9
  gem 'bs2_api'
13
10
  ```
14
11
 
15
- And then execute:
12
+ E então execute:
13
+ ```bash
14
+ bundle install
15
+ ```
16
+
17
+ Ou instale diretamente via:
18
+ ```bash
19
+ gem install bs2_api
20
+ ```
21
+
22
+ ### Configuração
23
+ Via variável de ambiente:
24
+ ```bash
25
+ BS2_CLIENT_ID
26
+ BS2_CLIENT_SECRET
27
+ BS2_CLIENT_ENVIRONMENT # production or sandbox. Default to sandbox
28
+ ```
29
+
30
+ ou via initializer:
16
31
 
17
- $ bundle install
32
+ ```ruby
33
+ Bs2Api.configure do |config|
34
+ config.client_id = 'you_bs2_client_id'
35
+ config.client_secret = 'you_bs2_client_secret'
36
+ config.pix_environment = 'pix_enviroment'
37
+ end
38
+ ```
18
39
 
19
- Or install it yourself as:
40
+ ### Iniciar pagamento por Chave
20
41
 
21
- $ gem install bs2_api
42
+ ```ruby
43
+ pix_key = Bs2Api::Entities::PixKey.new(
44
+ key: 'joao@gmail.com',
45
+ type: 'EMAIL'
46
+ )
22
47
 
23
- ## Usage
48
+ payment = Bs2Api::Payment::Key.new(pix_key).call
49
+ ```
24
50
 
25
- TODO: Write usage instructions here
51
+ Tipos:
52
+ ```bash
53
+ CPF
54
+ CNPJ
55
+ PHONE
56
+ EMAIL
57
+ EVP (Chave aleatória)
58
+ ```
26
59
 
27
- ## Development
28
60
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
61
+ ### Iniciar pagamento Manual
30
62
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
63
+ ```ruby
64
+ account = Bs2Api::Entities::Account.new(
65
+ bank_code: '341',
66
+ bank_name: 'Itaú',
67
+ agency: '0944',
68
+ number: '09442-1',
69
+ type: 'ContaCorrente'
70
+ )
71
+
72
+ customer = Bs2Api::Entities::Customer.new(
73
+ document: '88899988811',
74
+ type: 'CPF',
75
+ name: 'João Silva'
76
+ )
77
+
78
+ receiver = Bs2Api::Entities::Bank.new(
79
+ account: account,
80
+ customer: customer
81
+ )
82
+
83
+ payment = Bs2Api::Payment::Manual.new(receiver).call
84
+ ```
32
85
 
33
- ## Contributing
86
+ ### Objetos
34
87
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bs2_api.
88
+ ##### Payment
89
+ ```ruby
90
+ payment.id # pagamentoId
91
+ payment.merchant_id # endToEndId
92
+ payment.receiver # recebedor
93
+ payment.payer # pagador
94
+
95
+ payment.receiver.class
96
+ => Bs2Api::Entities::Bank
97
+
98
+ payment.payer.class
99
+ => Bs2Api::Entities::Bank
100
+
101
+ payment.payer.account # conta
102
+ => Bs2Api::Entities::Account
103
+
104
+ payment.payer.customer # pessoa
105
+ => Bs2Api::Entities::Customer
106
+
107
+ payment.to_hash
108
+ {
109
+ "pagamentoId"=>"123",
110
+ "endToEndId"=>"456",
111
+ "recebedor"=>{
112
+ "ispb"=>"71027866",
113
+ "conta"=>{
114
+ "banco"=>"218",
115
+ "bancoNome"=>"BCO BS2 S.A.",
116
+ "agencia"=>"0001",
117
+ "numero"=>"3134806",
118
+ "tipo"=>"ContaCorrente"
119
+ },
120
+ "pessoa"=>{
121
+ "documento"=>"25215188000116",
122
+ "tipoDocumento"=>"CNPJ",
123
+ "nome"=>"Teste Automatizado",
124
+ "nomeFantasia"=>nil
125
+ }
126
+ },
127
+ "pagador"=>{
128
+ "ispb"=>"71027866",
129
+ "conta"=>{
130
+ "banco"=>"218",
131
+ "bancoNome"=>"BCO BS2 S.A.",
132
+ "agencia"=>"0001",
133
+ "numero"=>"3134806",
134
+ "tipo"=>"ContaCorrente"
135
+ },
136
+ "pessoa"=>{
137
+ "documento"=>"25215188000116",
138
+ "tipoDocumento"=>"CNPJ",
139
+ "nome"=>"Teste Automatizado",
140
+ "nomeFantasia"=>nil
141
+ }
142
+ }
143
+ }
144
+ ```
data/bin/setup CHANGED
@@ -3,6 +3,4 @@ set -euo pipefail
3
3
  IFS=$'\n\t'
4
4
  set -vx
5
5
 
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
6
+ bundle install
data/bs2_api.gemspec CHANGED
@@ -27,5 +27,10 @@ Gem::Specification.new do |spec|
27
27
  spec.require_paths = ["lib"]
28
28
 
29
29
  spec.add_development_dependency "rspec", "~> 3.10"
30
- spec.add_dependency "http", "~> 5.0"
30
+ spec.add_development_dependency "webmock", "~> 3.13"
31
+ spec.add_development_dependency "vcr", "~> 6.0"
32
+ spec.add_development_dependency "dotenv", "~> 2.7", ">= 2.7.6"
33
+
34
+ spec.add_dependency "http", "~> 4.4"
35
+ spec.add_dependency "activesupport", "~> 6.1", ">= 6.1.3.2"
31
36
  end
data/lib/bs2_api.rb CHANGED
@@ -1,7 +1,65 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "bs2_api/version"
3
+ require 'bs2_api/version'
4
+ require 'bs2_api/configuration'
5
+
6
+ require 'bs2_api/initializers/object'
7
+ require 'bs2_api/initializers/hash'
8
+
9
+ require 'bs2_api/errors/base'
10
+ require 'bs2_api/errors/invalid_pix_key'
11
+ require 'bs2_api/errors/missing_configuration'
12
+ require 'bs2_api/errors/unauthorized'
13
+ require 'bs2_api/errors/bad_request'
14
+ require 'bs2_api/errors/server_error'
15
+
16
+ require 'bs2_api/entities/account'
17
+ require 'bs2_api/entities/bank'
18
+ require 'bs2_api/entities/customer'
19
+ require 'bs2_api/entities/payment'
20
+ require 'bs2_api/entities/pix_key'
21
+
22
+ require 'bs2_api/payment/base'
23
+ require 'bs2_api/payment/key'
24
+ require 'bs2_api/payment/manual'
25
+
26
+ require 'bs2_api/request/auth'
27
+
28
+ require 'bs2_api/util/bank_service'
29
+
30
+ require "active_support/core_ext/hash/indifferent_access"
4
31
 
5
32
  module Bs2Api
6
- class Bs2ApiError < StandardError; end
7
- end
33
+ ENDPOINT = {
34
+ production: 'https://api.bs2.com:8443',
35
+ sandbox: 'https://apihmz.bancobonsucesso.com.br'
36
+ }
37
+
38
+ class << self
39
+ attr_writer :configuration
40
+
41
+ def configuration
42
+ @configuration ||= Configuration.new
43
+ end
44
+
45
+ def configure
46
+ yield(configuration)
47
+ end
48
+
49
+ def endpoint
50
+ ENDPOINT[configuration.env.to_sym]
51
+ end
52
+
53
+ def production?
54
+ env == 'production'
55
+ end
56
+
57
+ def sandbox?
58
+ env == 'sandbox'
59
+ end
60
+
61
+ def env
62
+ configuration.env
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Configuration
4
+ attr_accessor :client_id, :client_secret, :pix_key, :env
5
+
6
+ def initialize
7
+ @env = ENV.fetch('BS2_ENVIRONMENT', 'sandbox')
8
+ @client_id = ENV.fetch('BS2_CLIENT_ID', nil)
9
+ @client_secret = ENV.fetch('BS2_CLIENT_SECRET', nil)
10
+ end
11
+
12
+ def valid?
13
+ raise Bs2Api::Errors::MissingConfiguration, 'Missing configuration credentials' if @client_id.blank? || @client_secret.blank?
14
+ end
15
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bs2Api
4
+ module Entities
5
+ class Account
6
+ attr_accessor :bank_code, :bank_name, :agency, :number, :type
7
+
8
+ TYPES = {
9
+ checking: 'ContaCorrente',
10
+ salary: 'ContaSalario',
11
+ saving: 'Poupanca'
12
+ }
13
+
14
+ def initialize(args = {})
15
+ @bank_code = args.fetch(:bank_code, nil)
16
+ @bank_name = args.fetch(:bank_name, nil)
17
+ @agency = args.fetch(:agency, nil)
18
+ @number = args.fetch(:number, nil)
19
+ @type = args.fetch(:type, nil)
20
+ end
21
+
22
+ def to_hash
23
+ ActiveSupport::HashWithIndifferentAccess.new(
24
+ {
25
+ "banco": @bank_code,
26
+ "bancoNome": bank_name,
27
+ "agencia": @agency,
28
+ "numero": @number,
29
+ "tipo": @type
30
+ }
31
+ )
32
+ end
33
+
34
+ def self.from_response(hash_payload)
35
+ hash = ActiveSupport::HashWithIndifferentAccess.new(hash_payload)
36
+
37
+ Bs2Api::Entities::Account.new(
38
+ bank_code: hash["banco"],
39
+ bank_name: hash["bancoNome"],
40
+ agency: hash["agencia"],
41
+ number: hash["numero"],
42
+ type: hash["tipo"]
43
+ )
44
+ end
45
+
46
+ def checking?
47
+ @type == TYPES[:checking]
48
+ end
49
+
50
+ def salary?
51
+ @type == TYPES[:salary]
52
+ end
53
+
54
+ def saving?
55
+ @type == TYPES[:saving]
56
+ end
57
+ end
58
+ end
59
+ end