Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API PIX > Ajustes e correção do ambiente de sandbox #75

Merged
merged 5 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions bb_wrapper/wrapper/bb.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ def __init__(
if gw_app_key is None:
gw_app_key = GW_APP_KEY

self.__basic_token = basic_token
self.__gw_app_key = gw_app_key
self._basic_token = basic_token
self._gw_app_key = gw_app_key
self._is_sandbox = is_sandbox

if self.__basic_token == "" or self.__gw_app_key == "":
if self._basic_token == "" or self._gw_app_key == "":
raise ValueError("Configure o basic_token/gw_app_key do BB!")

if self._is_sandbox:
Expand Down Expand Up @@ -122,7 +122,7 @@ def _construct_url(self, *args, **kwargs):
else:
url += "&"

url += f"gw-dev-app-key={self.__gw_app_key}"
url += f"gw-dev-app-key={self._gw_app_key}"

return url

Expand Down Expand Up @@ -196,7 +196,7 @@ def __authenticate(self):
O endpoint oauth recebe application/x-www-form-urlencoded!
"""
url = self.__oauth_url()
header = {"Authorization": f"Basic {self.__basic_token}"}
header = {"Authorization": f"Basic {self._basic_token}"}

data = {
"grant_type": "client_credentials",
Expand Down
41 changes: 3 additions & 38 deletions bb_wrapper/wrapper/pix_cob.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PIXCobBBWrapper(BaseBBWrapper):

SCOPE = "cob.read cob.write pix.read pix.write"

SANDBOX_BASE_URL = "https://api-pix.hm.bb.com.br/pix/v2"
SANDBOX_BASE_URL = "https://api.hm.bb.com.br/pix/v2"
BASE_URL = "https://api-pix.bb.com.br/pix/v2"

def listar_pix(self, inicio=None, fim=None, page=0):
Expand All @@ -31,7 +31,7 @@ def listar_pix(self, inicio=None, fim=None, page=0):
if fim:
search["fim"] = fim

url = self._construct_url("pix", end_bar=False, search=search)
url = self._construct_url("pix", search=search)

response = self._get(url)

Expand Down Expand Up @@ -156,49 +156,14 @@ def criar_cobranca(
expiracao, chave, documento_devedor, nome_devedor, valor, descricao, info
)

url = self._construct_url("cob", end_bar=True)
url = self._construct_url("cob")

response = self._post(url, data)

self._injeta_qrcode_data(response, nome_recebedor)

return response

def criar_cobranca_qrcode(
self,
expiracao: int,
chave: str,
documento_devedor: str,
nome_devedor: str,
nome_recebedor: str,
valor: float,
descricao: str,
info: list = None,
):
"""
Criar uma cobrança PIX com QRCode dinâmico

Args:
expiracao: segundos antes da expiracao
chave: chave PIX
documento_devedor: CPF ou CNPJ
nome_devedor: Nome do devedor
nome_recebedor: Nome do recebedor
valor: valor da cobrança
descricao: descrição da cobrança
"""
data = self._create_and_validate_cobranca_data(
expiracao, chave, documento_devedor, nome_devedor, valor, descricao, info
)

url = self._construct_url("cobqrcode", end_bar=True)

response = self._put(url, data)

self._injeta_qrcode_data(response, nome_recebedor)

return response

def consultar_cobranca(self, txid):
"""
Consultar uma cobrança PIX
Expand Down
28 changes: 12 additions & 16 deletions bb_wrapper/wrapper/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ class RequestsWrapper:
wrapper da lib requests

Attributes:
__base_url: Url base para construir os requests
__timeout: Tempo máximo de espera de requests
_base_url: Url base para construir os requests
_timeout: Tempo máximo de espera de requests
_verify_https: flag que ativa verificação https
__cert: certificado http
_cert: certificado http
"""

def __init__(self, *args, base_url, verify_https=True, cert=None, **kwargs):
self.__base_url = base_url
self._base_url = base_url
self._verify_https = verify_https
self.__cert = cert
self.__timeout = kwargs.get("timeout", None)
self._cert = cert
self._timeout = kwargs.get("timeout", None)

@staticmethod
def _process_response(response: requests.Response) -> requests.Response:
Expand Down Expand Up @@ -132,10 +132,6 @@ def _auth(self):
def _authorization_header_data(self):
return {"Authorization": self._auth}

@property
def _base_url(self):
return self.__base_url

def _get_request_info(self, headers=None):
if not headers:
headers = self._authorization_header_data
Expand All @@ -145,7 +141,7 @@ def _get_request_info(self, headers=None):
return dict(
headers=headers,
verify=self._verify_https,
cert=self.__cert,
cert=self._cert,
)

@retry_request(max_retries=3)
Expand All @@ -160,7 +156,7 @@ def _delete(self, url, headers=None) -> requests.Response:
(:class:`.requests.Response`)
"""
request_info = self._get_request_info(headers)
response = requests.delete(url, timeout=self.__timeout, **request_info)
response = requests.delete(url, timeout=self._timeout, **request_info)
response = self._process_response(response)
return response

Expand All @@ -176,7 +172,7 @@ def _get(self, url, headers=None) -> requests.Response:
(:class:`.requests.Response`)
"""
request_info = self._get_request_info(headers)
response = requests.get(url, timeout=self.__timeout, **request_info)
response = requests.get(url, timeout=self._timeout, **request_info)
response = self._process_response(response)
return response

Expand All @@ -199,7 +195,7 @@ def _post(self, url, data, headers=None, use_json=True) -> requests.Response:
request_info["json"] = data
else:
request_info["data"] = data
response = requests.post(url, timeout=self.__timeout, **request_info)
response = requests.post(url, timeout=self._timeout, **request_info)
response = self._process_response(response)
return response

Expand All @@ -220,7 +216,7 @@ def _put(self, url, data, headers=None, use_json=True) -> requests.Response:
request_info["json"] = data
else:
request_info["data"] = data
response = requests.put(url, timeout=self.__timeout, **request_info)
response = requests.put(url, timeout=self._timeout, **request_info)
response = self._process_response(response)
return response

Expand All @@ -231,6 +227,6 @@ def _patch(self, url, data, headers=None, use_json=True) -> requests.Response:
request_info["json"] = data
else:
request_info["data"] = data
response = requests.patch(url, timeout=self.__timeout, **request_info)
response = requests.patch(url, timeout=self._timeout, **request_info)
response = self._process_response(response)
return response
2 changes: 1 addition & 1 deletion examples/pix_cob/consultar_cobranca.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

c = PIXCobBBWrapper(cert=("./certs/cert.pem", "./certs/key.pem"))

txid = "HUAY0i0XMbuq6W3EcGapjsGCp5V19ToaRNR"
txid = "6lEZLwT4o1fjmg1BVW3KeIPLc0"

response = c.consultar_cobranca(txid)

Expand Down
4 changes: 2 additions & 2 deletions examples/pix_cob/criar_cobrança_cpf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

from bb_wrapper.wrapper import PIXCobBBWrapper

c = PIXCobBBWrapper()
c = PIXCobBBWrapper(cert=("./certs/cert.pem", "./certs/key.pem"))

data = {
"expiracao": 60 * 60, # 60 segundos = 1 minuto. 60 minutos = 1h
"documento_devedor": "12345678909",
"nome_devedor": "Francisco da SilvaFrancisco da SilvaFrancisco da SilvaFrancisco da SilvaFrancisco da Silva", # noqa: E501
"valor": 130.44,
"nome_recebedor": "Imobanco",
"chave": "7f6844d0-de89-47e5-9ef7-e0a35a681615",
"chave": "9e881f18-cc66-4fc7-8f2c-a795dbb2bfc1",
"descricao": "Cobrança dos serviços prestados.",
"info": [{"nome": "Sacado", "valor": "Nome do sacado aqui"}],
}
Expand Down
2 changes: 1 addition & 1 deletion tests/wrapper/test_cobrancas.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def test_build_url(self):

expected = (
"https://api.sandbox.bb.com.br/cobrancas/v2/boletos"
f"?gw-dev-app-key={wrapper._BaseBBWrapper__gw_app_key}"
f"?gw-dev-app-key={wrapper._gw_app_key}"
)

self.assertEqual(result, expected)
Expand Down
4 changes: 2 additions & 2 deletions tests/wrapper/test_pagamentos.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_construct_url_1(self):

expected = (
f"https://api.sandbox.bb.com.br/pagamentos-lote/v1/"
f"?gw-dev-app-key={PagamentoLoteBBWrapper()._BaseBBWrapper__gw_app_key}"
f"?gw-dev-app-key={PagamentoLoteBBWrapper()._gw_app_key}"
)

self.assertEqual(expected, result)
Expand All @@ -53,7 +53,7 @@ def test_construct_url_prod(self):

expected = (
f"https://api-ip.bb.com.br/pagamentos-lote/v1/"
f"?gw-dev-app-key={PagamentoLoteBBWrapper()._BaseBBWrapper__gw_app_key}"
f"?gw-dev-app-key={PagamentoLoteBBWrapper()._gw_app_key}"
)

self.assertEqual(expected, result)
Expand Down
4 changes: 2 additions & 2 deletions tests/wrapper/test_pix_cob.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def test_construct_url_1(self):
"""
result = PIXCobBBWrapper()._construct_url(end_bar=True)

expected = "https://api-pix.hm.bb.com.br/pix/v2/?gw-dev-app-key=KEY"
expected = "https://api.hm.bb.com.br/pix/v2/?gw-dev-app-key=KEY"

self.assertIn(expected, result)

Expand All @@ -188,7 +188,7 @@ def test_construct_url_2(self):
"""
result = PIXCobBBWrapper()._construct_url(end_bar=False)

expected = "https://api-pix.hm.bb.com.br/pix/v2?gw-dev-app-key="
expected = "https://api.hm.bb.com.br/pix/v2?gw-dev-app-key="
self.assertIn(expected, result)

def test_consultar_cobranca_content_bizarro(self):
Expand Down