sicoob_api 0.0.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 379213dc1149f57c5ea925740825602f1b2a2588ddd920573cd2a46361352b2f
4
- data.tar.gz: dd9d6241a23d5ee391f92d5af616bb936182b790686b0d64b084d87e6bbdace8
3
+ metadata.gz: e3e3e5670dd78f93b7f2b89f397e04bcf143e28fd1282088eef522f45f7d94bd
4
+ data.tar.gz: a98857367b8abe24a5a026426904c8f21b35d5c5af4e47b54e92ba496cf982c3
5
5
  SHA512:
6
- metadata.gz: d8e23de98b295fed49413bb3ae6f4bad84c887dae5cc72502b6c0e63d073e4c05dec7fc02e795eee3ad3ba222673ad7892d3947efbe915ccf12a642268e69ff4
7
- data.tar.gz: d86094dde5033a70b688d32fc394e766d0a12556311a16df281d845dfde89d76419dc15153f4d0e6f896a8b4ff93b857e7dd1324bbac831170cf8c0dcfbc91f6
6
+ metadata.gz: b2d90d62e9c1a81bc813bc0eabdde0cc12e3f2fda8cf28d0b6c838c33f7faff9f1dbd21b7b960f749336d23c1e58cbd48cba13a7efd751340726be88cec70c2a
7
+ data.tar.gz: 469c4b5d1d14695de9e763191786292f5d9ee849826667f6bec438b41360b149497f6dc4aa5cb47fe4e70f46198073964e51c255668bbb1c81d13d4e4c78cacd
@@ -0,0 +1,110 @@
1
+ module SicoobApi
2
+ class ClientProduction < Ac::Base
3
+ BASE_URL = "https://api.sicoob.com.br/pix/api/v2"
4
+ SAVE_RESPONSES = true
5
+ MAX_RETIES = 2
6
+
7
+ attr_accessor :client_id, :chave_pix, :crt, :key, :token_expires_at, :scopes
8
+
9
+ def initialize(client_id:, chave_pix:, crt:, key:, access_token: nil, token_expires_at: nil, scopes: "cob.write cob.read cobv.write cobv.read lotecobv.write lotecobv.read pix.write pix.read webhook.read webhook.write payloadlocation.write payloadlocation.read", test_mode: false)
10
+ @client_id = client_id
11
+ @chave_pix = chave_pix
12
+ @crt = crt
13
+ @key = key
14
+ @access_token = access_token
15
+ @token_expires_at = token_expires_at
16
+ @test_mode = test_mode
17
+ @scopes = scopes
18
+ end
19
+
20
+ def default_options
21
+ {
22
+ sslcert: crt,
23
+ sslkey: key,
24
+ headers: {
25
+ "Content-Type" => "application/json"
26
+ }
27
+ }
28
+ end
29
+
30
+ def refresh_token
31
+ headers = {
32
+ "Content-Type" => "application/x-www-form-urlencoded"
33
+ }
34
+ body = {
35
+ client_id: client_id,
36
+ scope: scopes,
37
+ grant_type: "client_credentials"
38
+ }
39
+
40
+ response = post("https://auth.sicoob.com.br/auth/realms/cooperado/protocol/openid-connect/token", headers: headers, body: body, skip_authentication: true)
41
+ @access_token = response.access_token
42
+ @token_expires_at = Time.now + response.expires_in
43
+ end
44
+
45
+ def create_payment amount:, payer_tax_id: nil, payer_name: nil, expiration: 3600, solicitacao_pagador: nil, info_adicionais: []
46
+ body = {
47
+ calendario: {
48
+ expiracao: expiration
49
+ },
50
+ valor: {
51
+ original: format("%.2f", amount),
52
+ modalidadeAlteracao: 0
53
+ },
54
+ chave: chave_pix,
55
+ devedor: devedor(payer_tax_id, payer_name),
56
+ solicitacaoPagador: solicitacao_pagador,
57
+ infoAdicionais: info_adicionais
58
+ }.compact
59
+ post("cob", body:)
60
+ end
61
+
62
+ def get_payment txid
63
+ get("cob/#{txid}")
64
+ end
65
+
66
+ def update_payment(payment_id:, status:)
67
+ body = {
68
+ status: status
69
+ }
70
+ patch("cob/#{payment_id}", body:)
71
+ end
72
+
73
+ def refund_payment(end_to_end_id:, amount:, refund_nature:, description: nil)
74
+ id = SecureRandom.hex(10)
75
+ body = {
76
+ valor: format("%.2f", amount),
77
+ natureza: refund_nature,
78
+ descricao: description
79
+ }
80
+ put("pix/#{end_to_end_id}/devolucao/#{id}", body:)
81
+ end
82
+
83
+ def get_refund(end_to_end_id:, txid:)
84
+ get("pix/#{end_to_end_id}/devolucao/#{txid}")
85
+ end
86
+
87
+ def update_webhook url
88
+ body = {
89
+ webhookUrl: url
90
+ }
91
+ put("webhook/#{chave_pix}", body:)
92
+ end
93
+
94
+ def get_webhook
95
+ get("webhook/#{chave_pix}")
96
+ end
97
+
98
+ private
99
+
100
+ def devedor(payer_tax_id, payer_name)
101
+ return {} if payer_tax_id.nil? || payer_name.nil?
102
+ formatted_tax_id = payer_tax_id.gsub(/\D/, "")
103
+ document_type = (formatted_tax_id.length == 11) ? "cpf" : "cnpj"
104
+ {
105
+ document_type => formatted_tax_id,
106
+ "nome" => payer_name
107
+ }
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,5 @@
1
+ module SicoobApi
2
+ class ClientSandbox < ClientProduction
3
+ BASE_URL = "https://sandbox.sicoob.com.br/sicoob/sandbox/pix/api/v2"
4
+ end
5
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SicoobApi
4
- VERSION = "0.0.2"
4
+ VERSION = "2.0.0"
5
5
  end
data/lib/sicoob_api.rb CHANGED
@@ -3,12 +3,10 @@
3
3
  require "ac"
4
4
  require "bigdecimal/util"
5
5
  require "securerandom"
6
- require "rqrcode"
7
6
 
8
7
  require_relative "sicoob_api/version"
9
- require_relative "sicoob_api/client"
10
- require_relative "sicoob_api/payment"
11
- require_relative "sicoob_api/payment_error"
8
+ require_relative "sicoob_api/client_production"
9
+ require_relative "sicoob_api/client_sandbox"
12
10
 
13
11
  module SicoobApi
14
12
  Typhoeus::Config.timeout = 10
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sicoob_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hamilton
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2025-01-27 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: ac
@@ -24,20 +23,6 @@ dependencies:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
25
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rqrcode
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
26
  - !ruby/object:Gem::Dependency
42
27
  name: bigdecimal
43
28
  requirement: !ruby/object:Gem::Requirement
@@ -74,14 +59,12 @@ extra_rdoc_files: []
74
59
  files:
75
60
  - README.md
76
61
  - lib/sicoob_api.rb
77
- - lib/sicoob_api/client.rb
78
- - lib/sicoob_api/payment.rb
79
- - lib/sicoob_api/payment_error.rb
62
+ - lib/sicoob_api/client_production.rb
63
+ - lib/sicoob_api/client_sandbox.rb
80
64
  - lib/sicoob_api/version.rb
81
65
  homepage: https://github.com/todasessascoisas/sicoob_api
82
66
  licenses: []
83
67
  metadata: {}
84
- post_install_message:
85
68
  rdoc_options: []
86
69
  require_paths:
87
70
  - lib
@@ -99,8 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
82
  - !ruby/object:Gem::Version
100
83
  version: '0'
101
84
  requirements: []
102
- rubygems_version: 3.5.10
103
- signing_key:
85
+ rubygems_version: 3.6.9
104
86
  specification_version: 4
105
87
  summary: ''
106
88
  test_files: []
@@ -1,107 +0,0 @@
1
- module SicoobApi
2
- class Client < Ac::Base
3
- BASE_URL = "https://api.sicoob.com.br/pix/api/v2"
4
- SAVE_RESPONSES = true
5
- MAX_RETIES = 2
6
-
7
- attr_accessor :client_id, :chave_pix, :crt, :key, :token, :token_expires_at, :scopes
8
-
9
- def initialize(client_id:, chave_pix:, crt:, key:, token: nil, token_expires_at: nil, scopes: "cob.write cob.read cobv.write cobv.read lotecobv.write lotecobv.read pix.write pix.read webhook.read webhook.write payloadlocation.write payloadlocation.read")
10
- @client_id = client_id
11
- @chave_pix = chave_pix
12
- @crt = crt
13
- @key = key
14
- @token = token
15
- @token_expires_at = token_expires_at
16
- @scopes = scopes
17
- access_token
18
- end
19
-
20
- def access_token
21
- if token.nil? || token_expires_at.nil? || token_expires_at < Time.now
22
- response = Typhoeus.post("https://auth.sicoob.com.br/auth/realms/cooperado/protocol/openid-connect/token",
23
- body: {
24
- client_id: client_id,
25
- scope: scopes,
26
- grant_type: "client_credentials"
27
- },
28
- sslcert: crt,
29
- sslkey: key) { |response| validate_response(response, "access_token") }
30
- raise "Erro ao obter token: #{response.body}" unless response.success?
31
- @token = response.json["access_token"]
32
- @token_expires_at = Time.now + response.json["expires_in"]
33
- end
34
- token
35
- end
36
-
37
- def create_payment amount:, payer_tax_id: nil, payer_name: nil, expiration: 3600, solicitacao_pagador: nil, info_adicionais: []
38
- body = {
39
- calendario: {
40
- expiracao: expiration
41
- },
42
- valor: {
43
- original: format("%.2f", amount),
44
- modalidadeAlteracao: 0
45
- },
46
- chave: chave_pix
47
- }
48
- body[:devedor] = devedor(payer_tax_id, payer_name) if payer_tax_id && payer_name
49
- body[:solicitacaoPagador] = solicitacao_pagador if solicitacao_pagador
50
- body[:infoAdicionais] = info_adicionais if info_adicionais.any?
51
- response = post("cob", sslcert: crt, sslkey: key, body: JSON.dump(body), headers: {"content-type": "application/json"}) { |response| validate_response(response, "txid") }
52
- SicoobApi::Payment.new(response.json, self)
53
- end
54
-
55
- def get_payment txid
56
- response = get("cob/#{txid}", sslcert: crt, sslkey: key) { |response| validate_response(response, "txid") }
57
- SicoobApi::Payment.new(response.json, self)
58
- end
59
-
60
- def update_payment(payment_id, body)
61
- response = patch("cob/#{payment_id}", headers: {"content-type": "application/json"}, sslcert: @crt, sslkey: @key, body: JSON.dump(body)) { |response| validate_response(response, "txid") }
62
- SicoobApi::Payment.new(response.json, self)
63
- end
64
-
65
- def refund_payment(end_to_end_id, amount, refund_nature, description)
66
- id = SecureRandom.hex(10)
67
- body = {
68
- valor: format("%.2f", amount),
69
- natureza: refund_nature,
70
- descricao: description
71
- }
72
- response = put("pix/#{end_to_end_id}/devolucao/#{id}", headers: {"content-type": "application/json"}, sslcert: @crt, sslkey: @key, body: JSON.dump(body)) { |response| validate_response(response, "id") }
73
- response.json
74
- end
75
-
76
- def get_refund(end_to_end_id, id)
77
- response = get("pix/#{end_to_end_id}/devolucao/#{id}", headers: {"content-type": "application/json"}, sslcert: @crt, sslkey: @key) { |response| validate_response(response, "id") }
78
- response.json
79
- end
80
-
81
- def update_webhook url
82
- put("webhook/#{chave_pix}", sslcert: crt, sslkey: key, body: JSON.dump({webhookUrl: url})) { |response| validate_response(response, "") }
83
- end
84
-
85
- def get_webhook
86
- response = get("webhook/#{chave_pix}", sslcert: crt, sslkey: key) { |response| validate_response(response, "webhookUrl") }
87
- response.json
88
- end
89
-
90
- private
91
-
92
- def devedor(payer_tax_id, payer_name)
93
- return {} if payer_tax_id.nil? || payer_name.nil?
94
- formatted_tax_id = payer_tax_id.gsub(/\D/, "")
95
- document_type = (formatted_tax_id.length == 11) ? "cpf" : "cnpj"
96
- {
97
- document_type => formatted_tax_id,
98
- "nome" => payer_name
99
- }
100
- end
101
-
102
- def validate_response(response, required_response_key)
103
- puts response.json unless response.success?
104
- ![500, 429].include?(response.code) || response.json.dig(*required_response_key)
105
- end
106
- end
107
- end
@@ -1,87 +0,0 @@
1
- module SicoobApi
2
- class Payment
3
- attr_reader :response, :txid, :end_to_end_id, :devolucoes, :copia_e_cola, :status, :solicitacao, :info_adicionais, :valor_original, :valor_pago, :criacao, :expiracao, :pago_em, :devedor_nome, :devedor_tax_id, :internal_error
4
- def initialize(response, client)
5
- @client = client
6
- @response = response
7
- @txid = response.dig("txid")
8
- @end_to_end_id = response.dig("pix", 0, "endToEndId")
9
- @devolucoes = response.dig("pix", 0, "devolucoes")
10
- @copia_e_cola = response.dig("brcode") || response.dig("qrCode")
11
- @status = response.dig("status")
12
- @solicitacao = response.dig("solicitacaoPagador")
13
- @info_adicionais = response.dig("infoAdicionais")
14
- @valor_original = response.dig("valor", "original")
15
- @valor_pago = response.dig("pix", 0, "valor")
16
- @criacao = creation_date
17
- @expiracao = expiration_date
18
- @pago_em = paid_date
19
- @devedor_nome = response.dig("devedor", "nome")
20
- @devedor_tax_id = response.dig("devedor", "cpf") || response.dig("devedor", "cnpj")
21
- @internal_error = PaymentError.new(response).internal_error
22
- end
23
-
24
- def paid? external_value = nil
25
- external_value = @valor_original if external_value.nil?
26
- @status == "CONCLUIDA" && @valor_original.to_d == @valor_pago.to_d && external_value.to_d == @valor_pago.to_d
27
- end
28
-
29
- def valid?
30
- @status == "ATIVA" && @expiracao > Time.now
31
- end
32
-
33
- def retrieve
34
- @client.get_payment(@txid)
35
- end
36
-
37
- def reload!
38
- new_payment = retrieve
39
- instance_variables.each do |variable|
40
- instance_variable_set(variable, new_payment.instance_variable_get(variable))
41
- end
42
- self
43
- end
44
-
45
- def update(body)
46
- @client.update_payment(@txid, body)
47
- end
48
-
49
- def invalidate!
50
- update({status: "REMOVIDA_PELO_USUARIO_RECEBEDOR"})
51
- reload!
52
- end
53
-
54
- def refund(amount: nil, refund_nature: nil, description: nil)
55
- amount ||= @valor_pago
56
- refund_nature ||= "ORIGINAL"
57
- @client.refund_payment(@end_to_end_id, amount, refund_nature, description)
58
- end
59
-
60
- def qr_code(module_size: 4)
61
- return false unless @copia_e_cola
62
- qr = RQRCode::QRCode.new(@copia_e_cola, size: 10, level: :l)
63
- qr.as_svg(module_size: module_size)
64
- end
65
-
66
- def puts_qrcode
67
- puts RQRCode::QRCode.new(@copia_e_cola).to_s(dark: "██", light: " ")
68
- end
69
-
70
- private
71
-
72
- def creation_date
73
- return nil unless @response.dig("calendario", "criacao")
74
- Time.new(@response.dig("calendario", "criacao"))
75
- end
76
-
77
- def expiration_date
78
- return nil unless @response.dig("calendario", "criacao")
79
- Time.new(@response.dig("calendario", "criacao")) + @response.dig("calendario", "expiracao")
80
- end
81
-
82
- def paid_date
83
- return nil unless @response.dig("pix", 0, "horario")
84
- Time.new(@response.dig("pix", 0, "horario"))
85
- end
86
- end
87
- end
@@ -1,25 +0,0 @@
1
- module SicoobApi
2
- class PaymentError
3
- STATUS_ERROR = [400, 403, 404, 503]
4
- attr_accessor :json_response
5
- def initialize json_response
6
- @json_response = json_response
7
- end
8
-
9
- def internal_error
10
- return nil unless json_response["violacoes"] || STATUS_ERROR.include?(json_response["status"])
11
- failed_message_error || rejected_message_error || "Pagamento recusado."
12
- end
13
-
14
- private
15
-
16
- def rejected_message_error
17
- "#{json_response["title"]} => #{json_response["detail"]}"
18
- end
19
-
20
- def failed_message_error
21
- return false unless json_response["violacoes"]
22
- "#{json_response["violacoes"][0]["razao"]} => #{json_response["violacoes"][0]["propriedade"]}"
23
- end
24
- end
25
- end