Skip to content

Commit

Permalink
Format all files
Browse files Browse the repository at this point in the history
  • Loading branch information
walison17 committed Feb 13, 2025
1 parent eab77a3 commit 7607f9f
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 89 deletions.
25 changes: 12 additions & 13 deletions examples/basic_usage.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from barte import BarteClient


def main():
# Initialize the client
client = BarteClient(
api_key="your_api_key",
environment="sandbox" # Use "production" for production environment
environment="sandbox", # Use "production" for production environment
)

# Create a credit card charge
Expand All @@ -16,19 +17,16 @@ def main():
"customer": {
"name": "John Doe",
"tax_id": "123.456.789-00",
"email": "john@example.com"
"email": "john@example.com",
},
"metadata": {
"order_id": "123",
"product_id": "456"
}
"metadata": {"order_id": "123", "product_id": "456"},
}

# Create and print charge details
charge = client.create_charge(charge_data)
print("\nCredit Card Charge:")
print(f"ID: {charge.id}")
print(f"Amount: R$ {charge.amount/100:.2f}")
print(f"Amount: R$ {charge.amount / 100:.2f}")
print(f"Status: {charge.status}")
print(f"Customer: {charge.customer.name}")
print(f"Created at: {charge.created_at}")
Expand All @@ -41,8 +39,8 @@ def main():
"customer": {
"name": "John Doe",
"tax_id": "123.456.789-00",
"email": "john@example.com"
}
"email": "john@example.com",
},
}

# Create PIX charge and get QR code
Expand All @@ -51,7 +49,7 @@ def main():

print("\nPIX Charge:")
print(f"ID: {pix_charge.id}")
print(f"Amount: R$ {pix_charge.amount/100:.2f}")
print(f"Amount: R$ {pix_charge.amount / 100:.2f}")
print(f"Status: {pix_charge.status}")
print(f"QR Code: {pix_charge.qr_code}")
print(f"Copy and Paste code: {pix_charge.copy_and_paste}")
Expand All @@ -60,15 +58,16 @@ def main():
print("\nRecent charges:")
charges = client.list_charges({"limit": 5})
for charge in charges:
print(f"- {charge.id}: R$ {charge.amount/100:.2f} ({charge.status})")
print(f"- {charge.id}: R$ {charge.amount / 100:.2f} ({charge.status})")

# Refund the credit card charge
refund = charge.refund(amount=500) # Partial refund of R$ 5,00
print("\nRefund:")
print(f"ID: {refund.id}")
print(f"Amount: R$ {refund.amount/100:.2f}")
print(f"Amount: R$ {refund.amount / 100:.2f}")
print(f"Status: {refund.status}")
print(f"Created at: {refund.created_at}")


if __name__ == "__main__":
main()
main()
29 changes: 15 additions & 14 deletions examples/card_token_example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from barte import BarteClient


def main():
# Initialize the client
client = BarteClient(
api_key="your_api_key",
environment="sandbox" # Use "production" for production environment
environment="sandbox", # Use "production" for production environment
)

# Create a card token
Expand All @@ -13,7 +14,7 @@ def main():
"holder_name": "John Doe",
"expiration_month": 12,
"expiration_year": 2025,
"cvv": "123"
"cvv": "123",
}

# Tokenize the card
Expand All @@ -28,11 +29,13 @@ def main():
# Simulate installments
amount = 10000 # R$ 100,00
installments = client.simulate_installments(amount=amount, brand=card_token.brand)

print("\nInstallment Options:")
for option in installments.installments:
print(f"{option.installments}x of R$ {option.amount/100:.2f} "
f"(total: R$ {option.total/100:.2f}, interest: {option.interest_rate}%)")
print(
f"{option.installments}x of R$ {option.amount / 100:.2f} "
f"(total: R$ {option.total / 100:.2f}, interest: {option.interest_rate}%)"
)

# Create a charge with the card token
charge_data = {
Expand All @@ -42,20 +45,17 @@ def main():
"customer": {
"name": "John Doe",
"tax_id": "123.456.789-00",
"email": "john@example.com"
"email": "john@example.com",
},
"installments": 3, # 3 installments
"metadata": {
"order_id": "123",
"product_id": "456"
}
"metadata": {"order_id": "123", "product_id": "456"},
}

# Create the charge
charge = client.charge_with_card_token(card_token.id, charge_data)
print("\nCharge:")
print(f"ID: {charge.id}")
print(f"Amount: R$ {charge.amount/100:.2f}")
print(f"Amount: R$ {charge.amount / 100:.2f}")
print(f"Status: {charge.status}")
print(f"Customer: {charge.customer.name}")
print(f"Created at: {charge.created_at}")
Expand All @@ -69,17 +69,18 @@ def main():
if refunds:
print("\nRefunds:")
for refund in refunds:
print(f"- {refund.id}: R$ {refund.amount/100:.2f} ({refund.status})")
print(f"- {refund.id}: R$ {refund.amount / 100:.2f} ({refund.status})")
else:
print("\nNo refunds yet")

# Create a partial refund
refund = charge.refund(amount=3000) # Refund R$ 30,00
print("\nPartial Refund:")
print(f"ID: {refund.id}")
print(f"Amount: R$ {refund.amount/100:.2f}")
print(f"Amount: R$ {refund.amount / 100:.2f}")
print(f"Status: {refund.status}")
print(f"Created at: {refund.created_at}")


if __name__ == "__main__":
main()
main()
21 changes: 9 additions & 12 deletions examples/card_token_transaction_example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from barte import BarteClient

# Inicializa o cliente
client = BarteClient(
api_key="sua_api_key_aqui",
environment="sandbox"
)
client = BarteClient(api_key="sua_api_key_aqui", environment="sandbox")

try:
# Primeiro, criar um token de cartão (se ainda não tiver um)
Expand All @@ -13,35 +10,35 @@
"holder_name": "João da Silva",
"expiration_month": 12,
"expiration_year": 2025,
"cvv": "123"
"cvv": "123",
}

token_response = client.create_card_token(card_data)
token_id = token_response["id"]
print("Token do cartão criado:", token_id)

# Agora, realizar uma transação usando o token
transaction_data = {
"amount": 2500, # R$ 25,00
"description": "Compra com cartão tokenizado",
"customer": {
"name": "João da Silva",
"tax_id": "123.456.789-00",
"email": "joao@exemplo.com"
"email": "joao@exemplo.com",
},
"installments": 1,
"capture": True,
"statement_descriptor": "MINHA LOJA"
"statement_descriptor": "MINHA LOJA",
}

# Realizar a transação com o token
transaction = client.charge_with_card_token(token_id, transaction_data)
print("Transação realizada com sucesso:", transaction)

# Verificar o status da transação
charge_id = transaction["id"]
charge_details = client.get_charge(charge_id)
print("Detalhes da transação:", charge_details)

except Exception as e:
print("Erro:", str(e))
print("Erro:", str(e))
46 changes: 22 additions & 24 deletions examples/credit_card_advanced_example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from barte import BarteClient

# Inicializa o cliente
client = BarteClient(
api_key="sua_api_key_aqui",
environment="sandbox"
)
client = BarteClient(api_key="sua_api_key_aqui", environment="sandbox")

try:
# Primeiro, criar um token de cartão para usar nos exemplos
Expand All @@ -13,62 +10,63 @@
"holder_name": "João da Silva",
"expiration_month": 12,
"expiration_year": 2025,
"cvv": "123"
"cvv": "123",
}

token_response = client.create_card_token(card_data)
card_token = token_response["id"]

# 1. Exemplo de Cobrança Recorrente
recurring_data = {
"amount": 5990, # R$ 59,90
"description": "Plano Mensal Premium",
"customer": {
"name": "João da Silva",
"tax_id": "123.456.789-00",
"email": "joao@exemplo.com"
"email": "joao@exemplo.com",
},
"card_token": card_token,
"recurrence": {
"interval": "month",
"interval_count": 1
}
"recurrence": {"interval": "month", "interval_count": 1},
}

recurring_charge = client.create_recurring_charge(recurring_data)
print("\nCobrança Recorrente criada:", recurring_charge)

# 2. Exemplo de Cobrança Parcelada com Repasse de Taxas
installment_with_fee_data = {
"amount": 10000, # R$ 100,00
"description": "Compra Parcelada com Juros",
"customer": {
"name": "João da Silva",
"tax_id": "123.456.789-00",
"email": "joao@exemplo.com"
"email": "joao@exemplo.com",
},
"card_token": card_token,
"installments": 3
"installments": 3,
}

installment_charge_with_fee = client.create_installment_charge_with_fee(installment_with_fee_data)

installment_charge_with_fee = client.create_installment_charge_with_fee(
installment_with_fee_data
)
print("\nCobrança Parcelada com Repasse criada:", installment_charge_with_fee)

# 3. Exemplo de Cobrança Parcelada sem Repasse de Taxas
installment_no_fee_data = {
"amount": 10000, # R$ 100,00
"description": "Compra Parcelada sem Juros",
"customer": {
"name": "João da Silva",
"tax_id": "123.456.789-00",
"email": "joao@exemplo.com"
"email": "joao@exemplo.com",
},
"card_token": card_token,
"installments": 3
"installments": 3,
}

installment_charge_no_fee = client.create_installment_charge_no_fee(installment_no_fee_data)

installment_charge_no_fee = client.create_installment_charge_no_fee(
installment_no_fee_data
)
print("\nCobrança Parcelada sem Repasse criada:", installment_charge_no_fee)

except Exception as e:
print("Erro:", str(e))
print("Erro:", str(e))
17 changes: 8 additions & 9 deletions examples/pix_example.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from barte import BarteClient, PixCharge, PixQRCode
from datetime import datetime, timedelta


def main():
# Initialize the client
client = BarteClient(
api_key="your_api_key",
environment="sandbox" # Use "production" for production environment
environment="sandbox", # Use "production" for production environment
)

# Create a PIX charge with expiration
Expand All @@ -17,20 +18,17 @@ def main():
"customer": {
"name": "John Doe",
"tax_id": "123.456.789-00",
"email": "john@example.com"
"email": "john@example.com",
},
"expiration_date": expiration,
"metadata": {
"order_id": "123",
"product_id": "456"
}
"metadata": {"order_id": "123", "product_id": "456"},
}

# Create PIX charge
pix_charge: PixCharge = client.create_pix_charge(pix_data)
print("\nPIX Charge Created:")
print(f"ID: {pix_charge.id}")
print(f"Amount: R$ {pix_charge.amount/100:.2f}")
print(f"Amount: R$ {pix_charge.amount / 100:.2f}")
print(f"Status: {pix_charge.status}")
print(f"Customer: {pix_charge.customer.name}")
print(f"Created at: {pix_charge.created_at}")
Expand Down Expand Up @@ -59,7 +57,7 @@ def main():
charges = client.list_charges({"payment_method": "pix", "limit": 5})
for charge in charges:
if isinstance(charge, PixCharge):
print(f"- {charge.id}: R$ {charge.amount/100:.2f} ({charge.status})")
print(f"- {charge.id}: R$ {charge.amount / 100:.2f} ({charge.status})")

# Cancel the charge if still pending
if updated_charge.status == "pending":
Expand All @@ -68,5 +66,6 @@ def main():
else:
print(f"\nCharge cannot be canceled: {updated_charge.status}")


if __name__ == "__main__":
main()
main()
11 changes: 5 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import pytest
from barte import BarteClient


@pytest.fixture
def barte_client():
"""Fixture that provides a Barte client instance for testing"""
return BarteClient(
api_key="test_api_key_123",
environment="sandbox"
)
return BarteClient(api_key="test_api_key_123", environment="sandbox")


@pytest.fixture
def mock_response():
Expand All @@ -18,5 +17,5 @@ def mock_response():
"created_at": "2024-03-20T10:00:00Z",
"amount": 1000,
"currency": "BRL",
"description": "Test charge"
}
"description": "Test charge",
}
Loading

0 comments on commit 7607f9f

Please # to comment.