sicoob_api 0.0.1 → 1.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: 4d201618226be8e7c5495ff1dba58de27b32c04a1cb32f23fb0f2aa8fc152f53
4
- data.tar.gz: e6e2c854ff1ec1f2b9cf3a80a0324c71279638dfd251fd4f21742ea3c0320c68
3
+ metadata.gz: ef77cf8e56978c3dd6504084483aab19593189f89bff0336227beeaf02b866c9
4
+ data.tar.gz: 6634e62e1b6b9592172abd71d457b1e0e8e830631b70d77a5cab494fc2d637f9
5
5
  SHA512:
6
- metadata.gz: 5ef3778e556f6cd251d7d0dc30a2a4e5f9475b707f264de5d79cd7ac95803fef44bcb72b8d1205d6a630ca8073a5a63a37e2fc33d0a0139cdc8acf15c88cdc9d
7
- data.tar.gz: ef8e4e299689e1e30d156b5d87d9cd1bb8440fe90cb0124fa9c8762b9ca33f9c2fee3ef2f8087808717d7c4eb39e7dc632e7a18274a029cd4e3fdec3d1afab39
6
+ metadata.gz: be37bb23833f966b89137ae7f07d1e36fcb479a9195699b010d47c47611ec11fd9ecd449a35672d4672f4c21de90f0aa7227ceff05bedd2f3e8fdd58981bb175
7
+ data.tar.gz: 531263f4a6830a239deb21f736c8cbc0d9f1ece2731044dc1079df06b5fc9e2ec91f40599cf9b77d8393f3f022fbd67f127870115139d8e25d083a80cf103df7
@@ -1,37 +1,44 @@
1
1
  module SicoobApi
2
2
  class Client < Ac::Base
3
3
  BASE_URL = "https://api.sicoob.com.br/pix/api/v2"
4
- # SAVE_RESPONSES = true
4
+ SAVE_RESPONSES = true
5
5
  MAX_RETIES = 2
6
6
 
7
- attr_accessor :client_id, :chave_pix, :crt, :key, :token, :token_expires_at, :scopes
7
+ attr_accessor :client_id, :chave_pix, :crt, :key, :token_expires_at, :scopes
8
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")
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")
10
10
  @client_id = client_id
11
11
  @chave_pix = chave_pix
12
12
  @crt = crt
13
13
  @key = key
14
- @token = token
14
+ @access_token = access_token
15
15
  @token_expires_at = token_expires_at
16
16
  @scopes = scopes
17
- access_token
18
17
  end
19
18
 
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
19
+ def default_options
20
+ {
21
+ sslcert: crt,
22
+ sslkey: key,
23
+ headers: {
24
+ "Content-Type" => "application/json"
25
+ }
26
+ }
27
+ end
28
+
29
+ def refresh_token
30
+ headers = {
31
+ "Content-Type" => "application/x-www-form-urlencoded"
32
+ }
33
+ body = {
34
+ client_id: client_id,
35
+ scope: scopes,
36
+ grant_type: "client_credentials"
37
+ }
38
+
39
+ response = post("https://auth.sicoob.com.br/auth/realms/cooperado/protocol/openid-connect/token", headers: headers, body: body, skip_authentication: true)
40
+ @access_token = response.access_token
41
+ @token_expires_at = Time.now + response.expires_in
35
42
  end
36
43
 
37
44
  def create_payment amount:, payer_tax_id: nil, payer_name: nil, expiration: 3600, solicitacao_pagador: nil, info_adicionais: []
@@ -48,18 +55,18 @@ module SicoobApi
48
55
  body[:devedor] = devedor(payer_tax_id, payer_name) if payer_tax_id && payer_name
49
56
  body[:solicitacaoPagador] = solicitacao_pagador if solicitacao_pagador
50
57
  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)
58
+ response = post("cob", body:)
59
+ SicoobApi::Payment.new(response, self)
53
60
  end
54
61
 
55
62
  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)
63
+ response = get("cob/#{txid}")
64
+ SicoobApi::Payment.new(response, self)
58
65
  end
59
66
 
60
67
  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)
68
+ response = patch("cob/#{payment_id}", body:)
69
+ SicoobApi::Payment.new(response, self)
63
70
  end
64
71
 
65
72
  def refund_payment(end_to_end_id, amount, refund_nature, description)
@@ -69,21 +76,19 @@ module SicoobApi
69
76
  natureza: refund_nature,
70
77
  descricao: description
71
78
  }
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
79
+ put("pix/#{end_to_end_id}/devolucao/#{id}", body:)
74
80
  end
75
81
 
76
82
  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
83
+ get("pix/#{end_to_end_id}/devolucao/#{id}")
79
84
  end
80
85
 
81
86
  def update_webhook url
82
- put("webhook/#{chave_pix}", sslcert: crt, sslkey: key, body: JSON.dump({webhookUrl: url})) { |response| validate_response(response, "") }
87
+ put("webhook/#{chave_pix}", body: {webhookUrl: url})
83
88
  end
84
89
 
85
90
  def get_webhook
86
- get("webhook/#{chave_pix}", sslcert: crt, sslkey: key) { |response| validate_response(response, "webhookUrl") }
91
+ get("webhook/#{chave_pix}")
87
92
  end
88
93
 
89
94
  private
@@ -97,10 +102,5 @@ module SicoobApi
97
102
  "nome" => payer_name
98
103
  }
99
104
  end
100
-
101
- def validate_response(response, required_response_key)
102
- puts response.json unless response.success?
103
- ![500, 429].include?(response.code) || response.json.dig(*required_response_key)
104
- end
105
105
  end
106
106
  end
@@ -1,24 +1,23 @@
1
1
  module SicoobApi
2
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
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
4
4
  def initialize(response, client)
5
5
  @client = client
6
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")
7
+ @txid = response.txid
8
+ @end_to_end_id = end_to_end
9
+ @devolucoes = devolutions
10
+ @copia_e_cola = response.brcode || response.qrCode
11
+ @status = response.status
12
+ @solicitacao = response["solicitacaoPagador"]
13
+ @info_adicionais = response["infoAdicionais"]
14
+ @valor_original = response.valor.original
15
+ @valor_pago = total_paid
16
16
  @criacao = creation_date
17
17
  @expiracao = expiration_date
18
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
19
+ @devedor_nome = debtor_name
20
+ @devedor_tax_id = debtor_tax_id
22
21
  end
23
22
 
24
23
  def paid? external_value = nil
@@ -70,18 +69,43 @@ module SicoobApi
70
69
  private
71
70
 
72
71
  def creation_date
73
- return nil unless @response.dig("calendario", "criacao")
74
- Time.new(@response.dig("calendario", "criacao"))
72
+ return nil unless @response.calendario.criacao
73
+ Time.new(@response.calendario.criacao)
75
74
  end
76
75
 
77
76
  def expiration_date
78
- return nil unless @response.dig("calendario", "criacao")
79
- Time.new(@response.dig("calendario", "criacao")) + @response.dig("calendario", "expiracao")
77
+ return nil unless @response.calendario.criacao
78
+ Time.new(@response.calendario.criacao) + @response.calendario.expiracao
80
79
  end
81
80
 
82
81
  def paid_date
83
- return nil unless @response.dig("pix", 0, "horario")
84
- Time.new(@response.dig("pix", 0, "horario"))
82
+ return nil unless @response["pix"] && @response.pix.any? && @response.pix[0].horario
83
+ Time.new(@response.pix[0].horario)
84
+ end
85
+
86
+ def end_to_end
87
+ return nil unless @response["pix"] && @response.pix.any?
88
+ @response.pix[0].endToEndId
89
+ end
90
+
91
+ def devolutions
92
+ return nil unless @response["pix"] && @response.pix.any?
93
+ @response.pix[0].devolucoes
94
+ end
95
+
96
+ def total_paid
97
+ return nil unless @response["pix"] && @response.pix.any?
98
+ @response.pix[0].valor
99
+ end
100
+
101
+ def debtor_name
102
+ return nil unless @response["devedor"]
103
+ @response.devedor.nome
104
+ end
105
+
106
+ def debtor_tax_id
107
+ return nil unless @response["devedor"]
108
+ @response.devedor.cpf || @response.devedor.cnpj
85
109
  end
86
110
  end
87
111
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SicoobApi
4
- VERSION = "0.0.1"
4
+ VERSION = "1.0.0"
5
5
  end
data/lib/sicoob_api.rb CHANGED
@@ -8,7 +8,6 @@ require "rqrcode"
8
8
  require_relative "sicoob_api/version"
9
9
  require_relative "sicoob_api/client"
10
10
  require_relative "sicoob_api/payment"
11
- require_relative "sicoob_api/payment_error"
12
11
 
13
12
  module SicoobApi
14
13
  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.1
4
+ version: 1.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
@@ -76,12 +75,10 @@ files:
76
75
  - lib/sicoob_api.rb
77
76
  - lib/sicoob_api/client.rb
78
77
  - lib/sicoob_api/payment.rb
79
- - lib/sicoob_api/payment_error.rb
80
78
  - lib/sicoob_api/version.rb
81
79
  homepage: https://github.com/todasessascoisas/sicoob_api
82
80
  licenses: []
83
81
  metadata: {}
84
- post_install_message:
85
82
  rdoc_options: []
86
83
  require_paths:
87
84
  - lib
@@ -99,8 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
96
  - !ruby/object:Gem::Version
100
97
  version: '0'
101
98
  requirements: []
102
- rubygems_version: 3.5.10
103
- signing_key:
99
+ rubygems_version: 3.6.9
104
100
  specification_version: 4
105
101
  summary: ''
106
102
  test_files: []
@@ -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