diff --git a/barte/client.py b/barte/client.py index 8964e22..f4a38ec 100644 --- a/barte/client.py +++ b/barte/client.py @@ -1,4 +1,5 @@ -from typing import Dict, Any, Optional, Union +from decimal import Decimal +from typing import Dict, Any, Optional, Union, List from dataclasses import asdict import requests from dacite import from_dict @@ -13,6 +14,7 @@ Order, ChargeList, OrderPayload, + InstallmentOption, ) @@ -131,3 +133,16 @@ def refund_charge(self, charge_id: str, as_fraud: Optional[bool] = False) -> Ref "PATCH", f"/v2/charges/{charge_id}/refund", json={"asFraud": as_fraud} ) return from_dict(data_class=Refund, data=json_response, config=DACITE_CONFIG) + + def get_installments( + self, amount: Decimal, max_installments: int + ) -> List[InstallmentOption]: + """Get a list of installments value""" + json_response = self._request( + "GET", + "/v2/orders/installments-payment", + params={"amount": amount, "maxInstallments": max_installments}, + ) + return [ + from_dict(data_class=InstallmentOption, data=item) for item in json_response + ] diff --git a/barte/models.py b/barte/models.py index 57a3093..9b2bbee 100644 --- a/barte/models.py +++ b/barte/models.py @@ -292,3 +292,10 @@ class OrderPayload: description: Optional[str] subSellerPaymentRequest: Optional[SubSellerPaymentRequest] metadata: Optional[List[Metadata]] + + +@dataclass +class InstallmentOption: + installments: int + installmentAmount: float + totalAmount: float diff --git a/tests/test_client.py b/tests/test_client.py index 8269b16..63b9439 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -3,7 +3,7 @@ from unittest.mock import patch, Mock from dacite import from_dict from barte import BarteClient, Charge, CardToken, Refund, PixCharge -from barte.models import DACITE_CONFIG, Order +from barte.models import DACITE_CONFIG, Order, InstallmentOption @pytest.fixture @@ -136,6 +136,16 @@ def mock_list_response(): } +@pytest.fixture +def mock_installments_reponse(): + return [ + {"installments": 1, "installmentAmount": 200.00, "totalAmount": 200.00}, + {"installments": 2, "installmentAmount": 107.30, "totalAmount": 214.60}, + {"installments": 3, "installmentAmount": 72.20, "totalAmount": 216.60}, + {"installments": 4, "installmentAmount": 54.50, "totalAmount": 218.00}, + ] + + class TestBarteClient: @patch("barte.client.requests.Session.request") def test_request_get(self, mock_request, barte_client): @@ -392,6 +402,29 @@ def test_pix_charge_get_qrcode( json=None, ) + @patch("barte.client.requests.Session.request") + def test_get_installments( + self, mock_request, barte_client, mock_installments_reponse + ): + """Test getting a specific charge using get_charge""" + mock_request.return_value.json.return_value = mock_installments_reponse + mock_request.return_value.raise_for_status = Mock() + + amount = 200 + max_installments = 4 + + installments = barte_client.get_installments(amount, max_installments) + assert len(installments) == 4 + assert installments[0].installmentAmount == 200 + assert isinstance(installments[0], InstallmentOption) + + mock_request.assert_called_once_with( + "GET", + f"{barte_client.base_url}/v2/orders/installments-payment", + params={"amount": amount, "maxInstallments": max_installments}, + json=None, + ) + def test_client_singleton(self): """Test client singleton pattern""" # Reset singleton for initial state diff --git a/tests/test_integration.py b/tests/test_integration.py index d15d046..96929e7 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -123,3 +123,8 @@ def test_create_order_and_fetch_charge(self, client, buyer_data, payment_templat charge = client.get_charge(order.charges[0].uuid) assert charge.uuid == order.charges[0].uuid + + def test_get_installments(self, client): + installments = client.get_installments(amount=200, max_installments=4) + assert len(installments) == 4 + assert installments[0].totalAmount == 200