Skip to content

Commit

Permalink
Fix authentication method and add get and create buyer to client
Browse files Browse the repository at this point in the history
  • Loading branch information
hersonls committed Feb 7, 2025
1 parent 30e904f commit 540c0b3
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 74 deletions.
16 changes: 14 additions & 2 deletions barte/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .models import (
Charge, CardToken, Refund, InstallmentOptions,
PixCharge, PixQRCode, DACITE_CONFIG, Config,
InstallmentSimulation
InstallmentSimulation, Buyer, BuyerList
)

class BarteClient:
Expand All @@ -28,7 +28,7 @@ def __init__(self, api_key: str, environment: str = "production"):
self.api_key = api_key
self.base_url = "https://api.barte.com" if environment == "production" else "https://sandbox-api.barte.com"
self.headers = {
"Authorization": f"Bearer {api_key}",
"X-Token-Api": api_key,
"Content-Type": "application/json"
}
BarteClient._instance = self
Expand Down Expand Up @@ -67,6 +67,18 @@ def cancel_charge(self, charge_id: str) -> Charge:
response.raise_for_status()
return from_dict(data_class=Charge, data=response.json(), config=DACITE_CONFIG)

def create_buyer(self, buyer_data: Dict[str, any]) -> Buyer:
endpoint = f"{self.base_url}/v2/buyers"
response = requests.post(endpoint, headers=self.headers, json=buyer_data)
response.raise_for_status()
return from_dict(data_class=Buyer, data=response.json(), config=DACITE_CONFIG)

def get_buyer(self, filters: Dict[str, any]) -> BuyerList:
endpoint = f"{self.base_url}/v2/buyers"
response = requests.get(endpoint, params=filters, headers=self.headers)
response.raise_for_status()
return from_dict(data_class=BuyerList, data=response.json(), config=DACITE_CONFIG)

def create_card_token(self, card_data: Dict[str, Any]) -> CardToken:
"""Create a token for a credit card"""
endpoint = f"{self.base_url}/v1/tokens"
Expand Down
64 changes: 59 additions & 5 deletions barte/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

# Default config for dacite with datetime conversion
DACITE_CONFIG = Config(
type_hooks={
datetime: lambda x: parse_date(x) if isinstance(x, str) else x
}
type_hooks={datetime: lambda x: parse_date(x) if isinstance(x, str) else x}
)


@dataclass
class Customer:
name: str
tax_id: str
email: str


@dataclass
class CardToken:
id: str
Expand All @@ -28,6 +28,7 @@ class CardToken:
expiration_year: int
brand: str


@dataclass
class Charge:
id: str
Expand All @@ -44,12 +45,17 @@ class Charge:

def refund(self, amount: Optional[int] = None) -> "Refund":
from .client import BarteClient
return BarteClient.get_instance().refund_charge(self.id, {"amount": amount} if amount else None)

return BarteClient.get_instance().refund_charge(
self.id, {"amount": amount} if amount else None
)

def cancel(self) -> "Charge":
from .client import BarteClient

return BarteClient.get_instance().cancel_charge(self.id)


@dataclass
class PixCharge(Charge):
qr_code: Optional[str] = None
Expand All @@ -58,12 +64,14 @@ class PixCharge(Charge):

def get_qr_code(self) -> "PixCharge":
from .client import BarteClient

qr_data = BarteClient.get_instance().get_pix_qrcode(self.id)
self.qr_code = qr_data.qr_code
self.qr_code_image = qr_data.qr_code_image
self.copy_and_paste = qr_data.copy_and_paste
return self


@dataclass
class Refund:
id: str
Expand All @@ -73,19 +81,65 @@ class Refund:
created_at: datetime
metadata: Optional[Dict[str, Any]] = None


@dataclass
class InstallmentSimulation:
installments: int
amount: int
total: int
interest_rate: float


@dataclass
class InstallmentOptions:
installments: List[InstallmentSimulation]


@dataclass
class PixQRCode:
qr_code: str
qr_code_image: str
copy_and_paste: str
copy_and_paste: str


@dataclass
class Buyer:
uuid: str
document: str
name: str
countryCode: str
phone: str
email: str
alternativeEmail: str


@dataclass
class SortInfo:
unsorted: bool
sorted: bool
empty: bool


@dataclass
class Pageable:
sort: SortInfo
pageNumber: int
pageSize: int
offset: int
paged: bool
unpaged: bool


@dataclass
class BuyerList:
content: List[Buyer]
pageable: Pageable
totalPages: int
totalElements: int
last: bool
numberOfElements: int
size: int
number: int
sort: SortInfo
first: bool
empty: bool
Loading

0 comments on commit 540c0b3

Please # to comment.