From 3513f960ccd7f91ea6d6ada7050111882ae15aba Mon Sep 17 00:00:00 2001 From: Mario Hernandez Date: Fri, 13 Dec 2024 21:52:10 -0300 Subject: [PATCH] cliente api --- django_payments_chile/FlowProvider.py | 19 ++++++++++++++----- django_payments_chile/clientes.py | 12 ++++++++++++ tests/test_flowprovider.py | 12 ++++++------ tox.ini | 2 +- 4 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 django_payments_chile/clientes.py diff --git a/django_payments_chile/FlowProvider.py b/django_payments_chile/FlowProvider.py index e1bbe19..d942e39 100644 --- a/django_payments_chile/FlowProvider.py +++ b/django_payments_chile/FlowProvider.py @@ -7,6 +7,8 @@ from payments.core import BasicProvider, get_base_url from payments.forms import PaymentForm as BasePaymentForm +from .clientes import ClienteAPI + class FlowProvider(BasicProvider): """ @@ -63,9 +65,9 @@ def get_form(self, payment, data: Optional[dict] = None) -> Any: if not payment.transaction_id: datos_para_flow = { "apiKey": self.api_key, - "commerceOrder": payment.token, + "commerceOrder": str(payment.token), "urlReturn": payment.get_success_url(), - "urlConfirmation": f"{get_base_url()}{payment.get_process_url()}", + "urlConfirmation": payment.get_process_url(), "subject": payment.description, "amount": int(payment.total), "paymentMethod": self.api_medio, @@ -83,9 +85,10 @@ def get_form(self, payment, data: Optional[dict] = None) -> Any: except Exception as e: # noqa # Dificil llegar acá, y si llegamos es problema de django-payments raise PaymentError(f"Ocurrió un error al guardar attrs.datos_flow: {e}") # noqa - - firma_datos = ... + datos_para_flow = dict(sorted(datos_para_flow.items())) + firma_datos = ClienteAPI.genera_firma(datos_para_flow, self.api_secret) datos_para_flow.update({"s": firma_datos}) + print(f"{datos_para_flow = }") try: pago_req = requests.post(f"{self.api_endpoint}/payment/create", data=datos_para_flow, timeout=5) pago_req.raise_for_status() @@ -135,9 +138,15 @@ def actualiza_estado(self, payment) -> dict: Returns: dict: Diccionario con valores del objeto `PaymentStatus`. """ + datos_para_flow = {"apiKey": self.api_key, "token": payment.token} + datos_para_flow = dict(sorted(datos_para_flow.items())) + firma_datos = ClienteAPI.genera_firma(datos_para_flow, self.api_secret) + datos_para_flow.update({"s": firma_datos}) + print(f"{datos_para_flow = }") + try: # status = FlowPayment.getStatus(self._client, payment.transaction_id) - estado_req = requests.post(f"{self.api_endpoint}/payment/getStatus", timeout=5) + estado_req = requests.get(f"{self.api_endpoint}/payment/getStatus", data=datos_para_flow, timeout=5) estado_req.raise_for_status() except Exception as e: diff --git a/django_payments_chile/clientes.py b/django_payments_chile/clientes.py new file mode 100644 index 0000000..be01185 --- /dev/null +++ b/django_payments_chile/clientes.py @@ -0,0 +1,12 @@ +import hashlib +import hmac +from dataclasses import dataclass + + +@dataclass +class ClienteAPI: + @classmethod + def genera_firma(cls, datos: list, secret_key: str): + datos_flow = "".join(f"{str(key)}{str(value)}" for key, value in datos.items()) + firma = hmac.new(key=secret_key.encode(), msg=datos_flow.encode(), digestmod=hashlib.sha256) + return firma.hexdigest() diff --git a/tests/test_flowprovider.py b/tests/test_flowprovider.py index 5b7248e..158aa1d 100644 --- a/tests/test_flowprovider.py +++ b/tests/test_flowprovider.py @@ -47,7 +47,7 @@ def get_success_url(self): class TestFlowProviderLive(TestCase): def test_provider_create_session_success(self): - payment = Payment() + test_payment = Payment() provider = FlowProvider(api_key=API_KEY, api_secret=API_SECRET) with patch("django_payments_chile.FlowProvider.requests.post") as mock_post: # Configure mock response @@ -57,12 +57,12 @@ def test_provider_create_session_success(self): mock_post.return_value = mock_response with self.assertRaises(RedirectNeeded): - provider.get_form(payment) + provider.get_form(test_payment) - self.assertEqual(payment.status, PaymentStatus.WAITING) - self.assertEqual(payment.attrs.respuesta_flow["url"], "https://flow.cl") - self.assertEqual(payment.attrs.respuesta_flow["token"], "TOKEN_ID") - self.assertEqual(payment.attrs.respuesta_flow["flowOrder"], "ORDER_ID") + self.assertEqual(test_payment.status, PaymentStatus.WAITING) + self.assertEqual(test_payment.attrs.respuesta_flow["url"], "https://flow.cl") + self.assertEqual(test_payment.attrs.respuesta_flow["token"], "TOKEN_ID") + self.assertEqual(test_payment.attrs.respuesta_flow["flowOrder"], "ORDER_ID") def test_provider_create_session_error(self): payment = Payment() diff --git a/tox.ini b/tox.ini index 8fd6085..23d34a5 100644 --- a/tox.ini +++ b/tox.ini @@ -30,7 +30,7 @@ deps= extras= transkbank-sdk commands= - pytest -v {posargs} + pytest -vs {posargs} coverage xml [gh-actions]