diff --git a/santander_sdk/__init__.py b/santander_sdk/__init__.py index c45e1eb..296b9b9 100644 --- a/santander_sdk/__init__.py +++ b/santander_sdk/__init__.py @@ -3,6 +3,7 @@ from santander_sdk.api_client.helpers import ( get_pix_key_type, document_type, + truncate_value, ) from santander_sdk.pix import transfer_pix, get_transfer @@ -48,4 +49,6 @@ # Comom exceptions "SantanderRequestError", "SantanderError", + # Helpers + "truncate_value", ] diff --git a/santander_sdk/api_client/helpers.py b/santander_sdk/api_client/helpers.py index 8442c8c..b39f195 100644 --- a/santander_sdk/api_client/helpers.py +++ b/santander_sdk/api_client/helpers.py @@ -1,3 +1,4 @@ +from datetime import date, datetime from decimal import ROUND_DOWN, Decimal import logging from itertools import cycle @@ -158,3 +159,18 @@ def polling_until_condition( return result sleep(interval) raise TimeoutError("Timeout polling until condition is met") + + +def today() -> date: + return datetime.now().date() + + +def to_iso_date_string(value: str | date) -> str: + if isinstance(value, date): + return value.isoformat() + try: + return date.fromisoformat(value).isoformat() + except ValueError: + raise ValueError( + f"Invalid date format: {value}. Expected Iso Format YYYY-MM-DD." + ) diff --git a/santander_sdk/barcode_payment.py b/santander_sdk/barcode_payment.py new file mode 100644 index 0000000..9de535d --- /dev/null +++ b/santander_sdk/barcode_payment.py @@ -0,0 +1,94 @@ +from datetime import date +from decimal import Decimal as D +from typing import Literal, cast + +from santander_sdk import SantanderApiClient +from santander_sdk.api_client.exceptions import SantanderClientError +from santander_sdk.api_client.helpers import ( + to_iso_date_string, + truncate_value, +) +from santander_sdk.transfer_flow import SantanderPaymentFlow +from santander_sdk.types import SantanderResponse, TransferResult + +SantanderBoletoResponse = SantanderResponse +TransferBoletoReponse = TransferResult + +BASE_ENDPOINT = "/management_payments_partners/v1/workspaces/:workspaceid" +BANKSLIP_ENDPOINT = f"{BASE_ENDPOINT}/bank_slip_payments" # Boleto bancário +BARCODE_ENDPOINT = f"{BASE_ENDPOINT}/barcode_payments" # Boleto de arrecadação + +BarCodeType = Literal["barcode", "bankslip"] + + +def pay_barcode( + client: SantanderApiClient, + barcode: str, + value: D, + payment_date: str | date, + tags: list[str] = [], + bar_code_type: BarCodeType = "bankslip", +) -> TransferBoletoReponse: + payment_id = "" + flow = SantanderPaymentFlow(client, _resolve_endpoint(bar_code_type)) + try: + # Step 1: Create the payment + create_barcode_response = flow.create_payment( + { + "code": barcode, + "tags": tags, + "paymentData": to_iso_date_string(payment_date), + } + ) + payment_id = create_barcode_response.get("id") + if not payment_id: + raise SantanderClientError("Payment ID was not returned on creation") + if create_barcode_response.get("status") is None: + raise SantanderClientError("Payment status was not returned on creation") + + # Step 2: Ensure the payment is ready to be confirmed + flow.ensure_ready_to_pay(create_barcode_response) + + # Step 3: Confirm the payment + confirm_response = flow.confirm_payment( + { + "status": "AUTHORIZED", + "paymentValue": truncate_value(value), + "debitAccount": create_barcode_response.get("debitAccount"), + "finalPayer": create_barcode_response.get("finalPayer"), + }, + payment_id, + ) + return { + "success": True, + "request_id": flow.request_id, + "data": confirm_response, + "error": "", + } + except Exception as e: + error_message = str(e) + client.logger.error(error_message) + return { + "success": False, + "request_id": flow.request_id, + "error": error_message, + "data": None, + } + + +def get_barcode_status( + client: SantanderApiClient, payment_id: str, barCodeType: BarCodeType +) -> SantanderBoletoResponse: + if not payment_id: + raise ValueError("pix_payment_id not provided") + endpoint = _resolve_endpoint(barCodeType) + response = client.get(f"{endpoint}/{payment_id}") + return cast(SantanderBoletoResponse, response) + + +def _resolve_endpoint(bar_code_type: BarCodeType) -> str: + if bar_code_type not in ("bankslip", "barcode"): + raise ValueError("bar_code_type must be 'bankslip' or 'barcode'") + if bar_code_type == "bankslip": + return BANKSLIP_ENDPOINT + return BARCODE_ENDPOINT diff --git a/santander_sdk/pix.py b/santander_sdk/pix.py index d413e2b..2308fcd 100644 --- a/santander_sdk/pix.py +++ b/santander_sdk/pix.py @@ -12,8 +12,8 @@ from santander_sdk.transfer_flow import SantanderPaymentFlow from santander_sdk.types import ( SantanderBeneficiary, - SantanderPixResponse, - TransferPixResult, + SantanderTransferResponse, + TransferResult, ) PIX_ENDPOINT = "/management_payments_partners/v1/workspaces/:workspaceid/pix_payments" @@ -26,7 +26,7 @@ def transfer_pix( description: str, tags: list[str] = [], id: uuid.UUID | str | None = None, -) -> TransferPixResult: +) -> TransferResult: transfer_flow = SantanderPaymentFlow(client, PIX_ENDPOINT) try: @@ -37,7 +37,8 @@ def transfer_pix( pix_key, value, description, tags, id ) create_pix_response = transfer_flow.create_payment(create_pix_dict) - if not create_pix_response.get("id"): + payment_id = create_pix_response.get("id") + if not payment_id: raise SantanderClientError("Payment ID was not returned on creation") if create_pix_response.get("status") is None: raise SantanderClientError("Payment status was not returned on creation") @@ -47,9 +48,7 @@ def transfer_pix( "status": "AUTHORIZED", "paymentValue": truncate_value(value), } - confirm_response = transfer_flow.confirm_payment( - payment_data, create_pix_response.get("id") - ) + confirm_response = transfer_flow.confirm_payment(payment_data, payment_id) return { "success": True, "request_id": transfer_flow.request_id, @@ -69,11 +68,11 @@ def transfer_pix( def get_transfer( client: SantanderApiClient, pix_payment_id: str -) -> SantanderPixResponse: +) -> SantanderTransferResponse: if not pix_payment_id: raise ValueError("pix_payment_id not provided") response = client.get(f"{PIX_ENDPOINT}/{pix_payment_id}") - return cast(SantanderPixResponse, response) + return cast(SantanderTransferResponse, response) def _generate_create_pix_dict( @@ -83,7 +82,7 @@ def _generate_create_pix_dict( tags: list = [], id: uuid.UUID | str | None = None, ) -> dict: - data = { + data: dict = { "tags": tags, "paymentValue": truncate_value(value), "remittanceInformation": description, diff --git a/santander_sdk/transfer_flow.py b/santander_sdk/transfer_flow.py index 91d9cd6..7314bd4 100644 --- a/santander_sdk/transfer_flow.py +++ b/santander_sdk/transfer_flow.py @@ -12,12 +12,7 @@ from santander_sdk.api_client.helpers import ( retry_one_time_on_request_exception, ) -from santander_sdk.types import ( - ConfirmOrderStatus, - CreateOrderStatus, - OrderStatus, - SantanderPixResponse, -) +from santander_sdk.types import SantanderResponse MAX_UPDATE_STATUS_AFTER_CONFIRM = 120 MAX_UPDATE_STATUS_BEFORE_CONFIRM = 10 @@ -36,10 +31,8 @@ def __init__( self.endpoint = endpoint self.request_id = None - def create_payment(self, data: dict) -> SantanderPixResponse: - response = cast( - SantanderPixResponse, self.client.post(self.endpoint, data=data) - ) + def create_payment(self, data: dict) -> SantanderResponse: + response = cast(SantanderResponse, self.client.post(self.endpoint, data=data)) self.request_id = response.get("id") self._check_for_rejected_error(response) self.client.logger.info("Payment created: ", response.get("id")) @@ -47,24 +40,22 @@ def create_payment(self, data: dict) -> SantanderPixResponse: def ensure_ready_to_pay(self, confirm_data) -> None: payment_status = confirm_data.get("status") - if payment_status != CreateOrderStatus.READY_TO_PAY: - self.client.logger.info("PIX is not ready for payment", payment_status) + if payment_status != "READY_TO_PAY": + self.client.logger.info("Payment is not ready for payment", payment_status) self._payment_status_polling( payment_id=confirm_data.get("id"), - until_status=[CreateOrderStatus.READY_TO_PAY], + until_status=["READY_TO_PAY"], max_update_attemps=MAX_UPDATE_STATUS_BEFORE_CONFIRM, ) - def confirm_payment( - self, confirm_data: dict, payment_id: str - ) -> SantanderPixResponse: + def confirm_payment(self, confirm_data: dict, payment_id: str) -> SantanderResponse: try: confirm_response = self._request_confirm_payment(confirm_data, payment_id) except SantanderRequestError as e: self.client.logger.error(str(e), payment_id, "checking current status") confirm_response = self._request_payment_status(payment_id) - if not confirm_response.get("status") == ConfirmOrderStatus.PAYED: + if not confirm_response.get("status") == "PAYED": try: confirm_response = self._resolve_lazy_status_payed( payment_id, confirm_response.get("status", "") @@ -76,27 +67,27 @@ def confirm_payment( return confirm_response @retry_one_time_on_request_exception - def _request_payment_status(self, payment_id: str) -> SantanderPixResponse: + def _request_payment_status(self, payment_id: str) -> SantanderResponse: if not payment_id: raise ValueError("payment_id not provided") response = self.client.get(f"{self.endpoint}/{payment_id}") - response = cast(SantanderPixResponse, response) + response = cast(SantanderResponse, response) self._check_for_rejected_error(response) return response def _request_confirm_payment( self, confirm_data: dict, payment_id: str - ) -> SantanderPixResponse: + ) -> SantanderResponse: self.current_step = "CONFIRM" if not payment_id: raise ValueError("payment_id not provided") response = self.client.patch(f"{self.endpoint}/{payment_id}", data=confirm_data) - response = cast(SantanderPixResponse, response) + response = cast(SantanderResponse, response) self._check_for_rejected_error(response) return response - def _check_for_rejected_error(self, payment_response: SantanderPixResponse): - if not payment_response.get("status") == OrderStatus.REJECTED: + def _check_for_rejected_error(self, payment_response: SantanderResponse): + if not payment_response.get("status") == "REJECTED": return reject_reason = payment_response.get( "rejectReason", "Reason not returned by Santander" @@ -106,20 +97,20 @@ def _check_for_rejected_error(self, payment_response: SantanderPixResponse): ) def _resolve_lazy_status_payed(self, payment_id: str, current_status: str): - if not current_status == ConfirmOrderStatus.PENDING_CONFIRMATION: + if not current_status == "PENDING_CONFIRMATION": raise SantanderClientError( f"Unexpected status after confirmation: {current_status}" ) confirm_response = self._payment_status_polling( payment_id=payment_id, - until_status=[ConfirmOrderStatus.PAYED], + until_status=["PAYED"], max_update_attemps=MAX_UPDATE_STATUS_AFTER_CONFIRM, ) return confirm_response def _payment_status_polling( self, payment_id: str, until_status: List[str], max_update_attemps: int - ) -> SantanderPixResponse: + ) -> SantanderResponse: response = None for attempt in range(1, max_update_attemps + 1): diff --git a/santander_sdk/types.py b/santander_sdk/types.py index e4284d1..454a844 100644 --- a/santander_sdk/types.py +++ b/santander_sdk/types.py @@ -104,25 +104,9 @@ class SantanderDebitAccount(TypedDict): number: str -class CreateOrderStatus: - READY_TO_PAY = "READY_TO_PAY" - PENDING_VALIDATION = "PENDING_VALIDATION" - REJECTED = "REJECTED" - - -class ConfirmOrderStatus: - PAYED = "PAYED" - PENDING_CONFIRMATION = "PENDING_CONFIRMATION" - REJECTED = "REJECTED" - - -class OrderStatus(ConfirmOrderStatus, CreateOrderStatus): - pass - - -OrderStatusType = Literal[ - "READY_TO_PAY", "PENDING_VALIDATION", "PAYED", "PENDING_CONFIRMATION", "REJECTED" -] +CreateOrderStatus = Literal["READY_TO_PAY", "PENDING_VALIDATION", "REJECTED"] +ConfirmOrderStatus = Literal["PAYED", "PENDING_CONFIRMATION", "REJECTED"] +OrderStatus = CreateOrderStatus | ConfirmOrderStatus class SantanderTransferResponse(TypedDict): @@ -153,17 +137,17 @@ class SantanderTransferResponse(TypedDict): transaction: SantanderTransaction tags: list[str] paymentValue: str - status: OrderStatusType + status: OrderStatus dictCode: str | None dictCodeType: Literal["CPF", "CNPJ", "CELULAR", "EMAIL", "EVP"] | None beneficiary: SantanderBeneficiary | None -SantanderPixResponse = SantanderTransferResponse | SantanderAPIErrorResponse +SantanderResponse = SantanderTransferResponse | SantanderAPIErrorResponse -class TransferPixResult(TypedDict): +class TransferResult(TypedDict): success: bool request_id: str | None - data: SantanderPixResponse | None + data: SantanderResponse | None error: str diff --git a/tests/conftest.py b/tests/conftest.py index e8a4009..ef383ec 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,4 @@ +import re import pytest from santander_sdk.api_client.client import SantanderApiClient from santander_sdk.api_client.client_configuration import SantanderClientConfiguration @@ -15,3 +16,19 @@ def client_instance(): workspace_id=TEST_WORKSPACE_ID, ) ) + + +@pytest.fixture +def mock_auth(responses): + responses.add( + method=responses.POST, + url=re.compile(r".*oauth/v2/token"), + json={ + "access_token": "mocked_access_token", + "token_type": "bearer", + "expires_in": 3600, + "scope": "read write", + }, + status=200, + ) + yield responses diff --git a/tests/mock/santander_mocker.py b/tests/mock/santander_mocker.py index af3ac21..f5c51bd 100644 --- a/tests/mock/santander_mocker.py +++ b/tests/mock/santander_mocker.py @@ -1,5 +1,6 @@ from decimal import Decimal -from typing import cast +import re +from typing import List, Literal, cast from urllib.parse import urljoin from responses import RequestsMock @@ -7,6 +8,7 @@ from santander_sdk.api_client.client import TOKEN_ENDPOINT from santander_sdk.api_client.workspaces import WORKSPACES_ENDPOINT +from santander_sdk.barcode_payment import BANKSLIP_ENDPOINT, BARCODE_ENDPOINT from santander_sdk.pix import PIX_ENDPOINT from santander_sdk.types import SantanderBeneficiary, OrderStatus @@ -80,7 +82,7 @@ def get_dict_payment_pix_request( key: str | SantanderBeneficiary, key_type: str = "", ) -> dict: - payment = { + payment: dict = { "paymentValue": value, "remittanceInformation": "informação da transferência", "tags": ["RH", "123456"], @@ -199,7 +201,7 @@ def mock_pix_status_endpoint( ) payment_response_by_beneficiary = get_dict_payment_pix_response( - "12345678", Decimal("299.99"), OrderStatus.READY_TO_PAY, santander_beneciary_john + "12345678", Decimal("299.99"), "READY_TO_PAY", santander_beneciary_john ) payment_response_by_beneficiary = get_dict_payment_pix_request( "12345678", Decimal("299.99"), santander_beneciary_john @@ -348,3 +350,63 @@ def mock_auth_endpoint(responses: RequestsMock): return responses.add( responses.POST, urljoin(SANTANDER_URL, TOKEN_ENDPOINT), json=dict_token_response ) + + +def barcode_response_dict( + id, + barcode, + status: OrderStatus, + value: str = str("200.15"), + tags: List | None = ["tag1", "tag2"], +): + return { + "id": id, + "workspaceId": TEST_WORKSPACE_ID, + "code": barcode, + "debitAccount": {"branch": "1", "number": "100022349"}, + "status": status, + "rejectReason": None, + "paymentType": "TAXES", + "accountingDate": "2025-01-17", + "finalPayer": { + "name": "JAJA PENUMPER ALFA LTDA", + "documentType": "CNPJ", + "documentNumber": "63918424000150", + }, + "totalValue": value, + "concessionary": {"code": 48, "name": "AES ELETROPAULO"}, + "transaction": { + "value": value, + "code": "VX43C02501171347300469" if "PAYED" else None, + "date": "2025-01-17T16:47:30Z", + }, + "paymentValue": value, + "tags": tags or [], + } + + +def mock_barcode_response( + rsp: RequestsMock, + json: dict, + step: Literal["CREATE", "CONFIRM", "STATUS"], + status: int = 200, + type: Literal["barcode", "bankslip"] = "bankslip", +): + pathern_endpoint = BARCODE_ENDPOINT if type == "barcode" else BANKSLIP_ENDPOINT + + if step == "CREATE": + pathern = pathern_endpoint + method = "POST" + elif step == "CONFIRM": + pathern = f"{pathern_endpoint}/{json['id']}" + method = "PATCH" + elif step == "STATUS": + pathern = f"{pathern_endpoint}/{json['id']}" + method = "GET" + else: + raise ValueError("Unsuported step") + + pathern = pathern.replace(":workspaceid", TEST_WORKSPACE_ID) + return rsp.add( + url=re.compile(f".*{pathern}"), json=json, method=method, status=status + ) diff --git a/tests/test_barcode_integration.py b/tests/test_barcode_integration.py new file mode 100644 index 0000000..ba4bf47 --- /dev/null +++ b/tests/test_barcode_integration.py @@ -0,0 +1,58 @@ +"""Some inicial data to start tests and mocks""" + +import pytest +from santander_sdk.barcode_payment import pay_barcode +from tests.mock.santander_mocker import barcode_response_dict, mock_barcode_response +from decimal import Decimal as D + +BARCODE_TEST = "8464000000028735002403040150349033427080404014" +TEST_ID = "6c016190-0ab9-4c12-931f-8d6fbf0fd9ab" + + +@pytest.mark.usefixtures("mock_auth") +def test_transfer_boleto_paymenat_success(client_instance, responses): + json_create = barcode_response_dict(TEST_ID, BARCODE_TEST, "READY_TO_PAY") + mock_create = mock_barcode_response(responses, json_create, "CREATE") + + json_status = barcode_response_dict(TEST_ID, BARCODE_TEST, "PAYED") + mock_status = mock_barcode_response(responses, json_status, "STATUS") + + json_confirm = barcode_response_dict(TEST_ID, BARCODE_TEST, "PENDING_CONFIRMATION") + mock_confirm = mock_barcode_response(responses, json_confirm, "CONFIRM") + + result = pay_barcode( + client_instance, BARCODE_TEST, D("200.15"), "2025-01-17", ["tag1", "tag2"] + ) + + assert result["success"] is True + assert mock_confirm.call_count == 1 + assert mock_create.call_count == 1 + assert mock_status.call_count == 1 + + assert result["data"] == { + "id": "6c016190-0ab9-4c12-931f-8d6fbf0fd9ab", + "workspaceId": "8e33d56c-204f-461e-aebe-08baaab6479e", + "code": "8464000000028735002403040150349033427080404014", + "debitAccount": {"branch": "1", "number": "100022349"}, + "status": "PAYED", + "rejectReason": None, + "paymentType": "TAXES", + "accountingDate": "2025-01-17", + "finalPayer": { + "name": "JAJA PENUMPER ALFA LTDA", + "documentType": "CNPJ", + "documentNumber": "63918424000150", + }, + "totalValue": "200.15", + "concessionary": {"code": 48, "name": "AES ELETROPAULO"}, + "transaction": { + "value": "200.15", + "code": "VX43C02501171347300469", + "date": "2025-01-17T16:47:30Z", + }, + "paymentValue": "200.15", + "tags": ["tag1", "tag2"], + } + + +# need more tests here diff --git a/tests/test_client_unit.py b/tests/test_client_unit.py index d513c20..39a2b62 100644 --- a/tests/test_client_unit.py +++ b/tests/test_client_unit.py @@ -19,7 +19,6 @@ SantanderClientError, SantanderRequestError, ) -from santander_sdk.types import OrderStatus @responses.activate @@ -49,7 +48,7 @@ def client(): @patch("santander_sdk.api_client.client.requests.Session.request") def test_request(mock_request, client): response_dict = get_dict_payment_pix_response( - "12345678", D(299.99), OrderStatus.READY_TO_PAY, "12345678909", "CPF" + "12345678", D(299.99), "READY_TO_PAY", "12345678909", "CPF" ) request_dict = get_dict_payment_pix_request( "12345678", D(299.99), "12345678909", "CPF" diff --git a/tests/test_pix_integration.py b/tests/test_pix_integration.py index 952190d..94811f1 100644 --- a/tests/test_pix_integration.py +++ b/tests/test_pix_integration.py @@ -21,7 +21,6 @@ mock_create_pix_endpoint, mock_pix_status_endpoint, ) -from santander_sdk.types import OrderStatus @pytest.fixture @@ -38,13 +37,13 @@ def test_transfer_pix_payment_success(mock_api, client_instance): description = "Pagamento Teste" pix_key = "12345678909" mock_create = mock_create_pix_endpoint( - mock_api, pix_id, value, OrderStatus.READY_TO_PAY, pix_key, "CPF" + mock_api, pix_id, value, "READY_TO_PAY", pix_key, "CPF" ) mock_confirm = mock_confirm_pix_endpoint( - mock_api, pix_id, value, OrderStatus.PENDING_CONFIRMATION, pix_key, "CPF" + mock_api, pix_id, value, "PENDING_CONFIRMATION", pix_key, "CPF" ) mock_status = mock_pix_status_endpoint( - mock_api, pix_id, value, OrderStatus.PAYED, pix_key, "CPF" + mock_api, pix_id, value, "PAYED", pix_key, "CPF" ) transfer_result = transfer_pix( client_instance, pix_key, value, description, tags=["teste"] @@ -123,10 +122,10 @@ def test_transfer_pix_payment_timeout_create( pix_key = "12345678909" mock_create = mock_create_pix_endpoint( - mock_api, pix_id, value, OrderStatus.PENDING_VALIDATION, pix_key, "CPF" + mock_api, pix_id, value, "PENDING_VALIDATION", pix_key, "CPF" ) mock_status = mock_pix_status_endpoint( - mock_api, pix_id, value, OrderStatus.PENDING_VALIDATION, pix_key, "CPF" + mock_api, pix_id, value, "PENDING_VALIDATION", pix_key, "CPF" ) transfer_result = transfer_pix(client_instance, pix_key, value, description) @@ -149,13 +148,13 @@ def test_transfer_pix_payment_timeout_before_authorize( pix_key = "12345678909" mock_create = mock_create_pix_endpoint( - mock_api, pix_id, value, OrderStatus.READY_TO_PAY, pix_key, "CPF" + mock_api, pix_id, value, "READY_TO_PAY", pix_key, "CPF" ) mock_confirm = mock_confirm_pix_endpoint( - mock_api, pix_id, value, OrderStatus.PENDING_CONFIRMATION, pix_key, "CPF" + mock_api, pix_id, value, "PENDING_CONFIRMATION", pix_key, "CPF" ) mock_status = mock_pix_status_endpoint( - mock_api, pix_id, value, OrderStatus.PENDING_CONFIRMATION, pix_key, "CPF" + mock_api, pix_id, value, "PENDING_CONFIRMATION", pix_key, "CPF" ) transfer_result = transfer_pix(client_instance, pix_key, value, description) @@ -180,7 +179,7 @@ def test_transfer_pix_payment_timeout_before_authorize( }, "paymentValue": "123.44", "remittanceInformation": "informação da transferência", - "status": OrderStatus.PENDING_CONFIRMATION, + "status": "PENDING_CONFIRMATION", "tags": [], "totalValue": "123.44", "transaction": { @@ -208,7 +207,7 @@ def test_transfer_pix_payment_rejected_on_create( pix_key = "12345678909" mock_create = mock_create_pix_endpoint( - mock_api, pix_id, value, OrderStatus.REJECTED, pix_key, "CPF" + mock_api, pix_id, value, "REJECTED", pix_key, "CPF" ) transfer_result = transfer_pix(client_instance, pix_key, value, description) @@ -230,10 +229,10 @@ def test_transfer_pix_payment_rejected_on_confirm( pix_key = "12345678909" mock_create = mock_create_pix_endpoint( - mock_api, pix_id, value, OrderStatus.READY_TO_PAY, pix_key, "CPF" + mock_api, pix_id, value, "READY_TO_PAY", pix_key, "CPF" ) mock_confirm = mock_confirm_pix_endpoint( - mock_api, pix_id, value, OrderStatus.REJECTED, pix_key, "CPF" + mock_api, pix_id, value, "REJECTED", pix_key, "CPF" ) transfer_result = transfer_pix(client_instance, pix_key, value, description) @@ -258,14 +257,14 @@ def test_transfer_pix_payment_with_beneficiary( mock_api, pix_id, value, - OrderStatus.PENDING_VALIDATION, + "PENDING_VALIDATION", santander_beneciary_john, ) mock_status = mock_pix_status_endpoint( - mock_api, pix_id, value, OrderStatus.READY_TO_PAY, santander_beneciary_john + mock_api, pix_id, value, "READY_TO_PAY", santander_beneciary_john ) mock_confirm = mock_confirm_pix_endpoint( - mock_api, pix_id, value, OrderStatus.PAYED, santander_beneciary_john + mock_api, pix_id, value, "PAYED", santander_beneciary_john ) transfer_result = transfer_pix( @@ -348,24 +347,24 @@ def test_transfer_pix_payment_lazy_status_update( pix_key = "12345678909" mock_create = mock_create_pix_endpoint( - mock_api, pix_id, value, OrderStatus.PENDING_VALIDATION, pix_key, "CPF" + mock_api, pix_id, value, "PENDING_VALIDATION", pix_key, "CPF" ) mock_confirm = mock_confirm_pix_endpoint( - mock_api, pix_id, value, OrderStatus.PAYED, pix_key, "CPF" + mock_api, pix_id, value, "PAYED", pix_key, "CPF" ) mock_status_pending = mock_api.add( responses.GET, f"{PIX_ENDPOINT_WITH_WORKSPACE}/{pix_id}", json=get_dict_payment_pix_response( - pix_id, value, OrderStatus.PENDING_VALIDATION, pix_key, "CPF" + pix_id, value, "PENDING_VALIDATION", pix_key, "CPF" ), ) mock_status_ready = mock_api.add( responses.GET, f"{PIX_ENDPOINT_WITH_WORKSPACE}/{pix_id}", json=get_dict_payment_pix_response( - pix_id, value, OrderStatus.READY_TO_PAY, pix_key, "CPF" + pix_id, value, "READY_TO_PAY", pix_key, "CPF" ), ) transfer_result = transfer_pix(client_instance, pix_key, value, description) diff --git a/tests/test_pix_unit.py b/tests/test_pix_unit.py index 1c992c4..6344498 100644 --- a/tests/test_pix_unit.py +++ b/tests/test_pix_unit.py @@ -13,7 +13,6 @@ get_dict_payment_pix_response, beneciary_john_dict_json, ) -from santander_sdk.types import OrderStatus import pytest @@ -26,12 +25,10 @@ } tags = ["bf: 1234", "nf: 1234", "nf_data: 2021-10-10"] create_pix_response = get_dict_payment_pix_response( - "1234", D("123"), OrderStatus.PENDING_VALIDATION + "1234", D("123"), "PENDING_VALIDATION" ) -ready_to_pay_response = get_dict_payment_pix_response( - "1234", D("123"), OrderStatus.READY_TO_PAY -) -confirm_response = get_dict_payment_pix_response("1234", D("123"), OrderStatus.PAYED) +ready_to_pay_response = get_dict_payment_pix_response("1234", D("123"), "READY_TO_PAY") +confirm_response = get_dict_payment_pix_response("1234", D("123"), "PAYED") @pytest.fixture diff --git a/tests/test_transfer_flow_unit.py b/tests/test_transfer_flow_unit.py index b5088ce..fdf572c 100644 --- a/tests/test_transfer_flow_unit.py +++ b/tests/test_transfer_flow_unit.py @@ -6,7 +6,6 @@ SantanderRejectedError, SantanderStatusTimeoutError, ) -from santander_sdk.types import OrderStatus from santander_sdk.transfer_flow import SantanderPaymentFlow from tests.mock.santander_mocker import get_dict_payment_pix_response @@ -42,7 +41,7 @@ def _lazy_status_update(payment_id, statuses): def test_create_payment(payment_flow, api_client): data = {"paymentValue": "100.00", "remittanceInformation": "Test Payment"} - response = {"id": "12345", "status": OrderStatus.PENDING_VALIDATION} + response = {"id": "12345", "status": "PENDING_VALIDATION"} api_client.post.return_value = response result = payment_flow.create_payment(data) @@ -51,21 +50,21 @@ def test_create_payment(payment_flow, api_client): def test_ensure_ready_to_pay(payment_flow, api_client): - confirm_data = {"id": "12345", "status": OrderStatus.PENDING_VALIDATION} - api_client.get.return_value = {"id": "12345", "status": OrderStatus.READY_TO_PAY} + confirm_data = {"id": "12345", "status": "PENDING_VALIDATION"} + api_client.get.return_value = {"id": "12345", "status": "READY_TO_PAY"} payment_flow.ensure_ready_to_pay(confirm_data) api_client.get.assert_called_once_with(f"{PIX_ENDPOINT}/12345") def test_ensure_ready_to_pay_lazy(payment_flow, api_client, lazy_status_update): - confirm_data = {"id": "12345", "status": OrderStatus.PENDING_VALIDATION} + confirm_data = {"id": "12345", "status": "PENDING_VALIDATION"} lazy_status_update( "12345", [ - OrderStatus.PENDING_CONFIRMATION, - OrderStatus.PENDING_VALIDATION, - OrderStatus.READY_TO_PAY, + "PENDING_CONFIRMATION", + "PENDING_VALIDATION", + "READY_TO_PAY", ], ) payment_flow.ensure_ready_to_pay(confirm_data) @@ -75,7 +74,7 @@ def test_ensure_ready_to_pay_lazy(payment_flow, api_client, lazy_status_update): def test_confirm_payment(payment_flow, api_client): confirm_data = {"status": "AUTHORIZED", "paymentValue": "100.00"} payment_id = "12345" - response = {"id": payment_id, "status": OrderStatus.PAYED} + response = {"id": payment_id, "status": "PAYED"} api_client.patch.return_value = response result = payment_flow.confirm_payment(confirm_data, payment_id) @@ -86,14 +85,14 @@ def test_confirm_payment(payment_flow, api_client): def test_check_for_rejected_error(payment_flow): - response = {"status": OrderStatus.REJECTED, "rejectReason": "Insufficient funds"} + response = {"status": "REJECTED", "rejectReason": "Insufficient funds"} with pytest.raises(SantanderRejectedError): payment_flow._check_for_rejected_error(response) def test_request_payment_status(payment_flow, api_client): payment_id = "12345" - response = {"id": payment_id, "status": OrderStatus.PENDING_VALIDATION} + response = {"id": payment_id, "status": "PENDING_VALIDATION"} api_client.get.return_value = response result = payment_flow._request_payment_status(payment_id) @@ -104,7 +103,7 @@ def test_request_payment_status(payment_flow, api_client): def test_request_confirm_payment(payment_flow, api_client): confirm_data = {"status": "AUTHORIZED", "paymentValue": "100.00"} payment_id = "12345" - response = {"id": payment_id, "status": OrderStatus.PAYED} + response = {"id": payment_id, "status": "PAYED"} api_client.patch.return_value = response result = payment_flow._request_confirm_payment(confirm_data, payment_id) @@ -121,14 +120,11 @@ def test_payment_status_polling( payment_id = "12345" lazy_status_update( payment_id, - [OrderStatus.PENDING_VALIDATION] * attemps_to_be_ready - + [OrderStatus.READY_TO_PAY], + ["PENDING_VALIDATION"] * attemps_to_be_ready + ["READY_TO_PAY"], ) - result = payment_flow._payment_status_polling( - payment_id, [OrderStatus.READY_TO_PAY], 10 - ) - assert result.get("status") == OrderStatus.READY_TO_PAY + result = payment_flow._payment_status_polling(payment_id, ["READY_TO_PAY"], 10) + assert result.get("status") == "READY_TO_PAY" api_client.get.assert_called_with(f"{PIX_ENDPOINT}/{payment_id}") assert mock_sleep.call_count == attemps_to_be_ready @@ -137,9 +133,9 @@ def test_payment_status_polling_timeout( payment_flow, api_client, mock_sleep, lazy_status_update ): payment_id = "12345" - lazy_status_update(payment_id, [OrderStatus.PENDING_VALIDATION] * 3) + lazy_status_update(payment_id, ["PENDING_VALIDATION"] * 3) with pytest.raises(SantanderStatusTimeoutError): - payment_flow._payment_status_polling(payment_id, [OrderStatus.READY_TO_PAY], 3) + payment_flow._payment_status_polling(payment_id, ["READY_TO_PAY"], 3) assert api_client.get.call_count == 3 assert mock_sleep.call_count == 2