Skip to content
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
17 changes: 16 additions & 1 deletion barte/client.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,6 +14,7 @@
Order,
ChargeList,
OrderPayload,
InstallmentOption,
)


Expand Down Expand Up @@ -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
]
7 changes: 7 additions & 0 deletions barte/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 34 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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