diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9ebb219..ebfbb16 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -13,8 +13,8 @@ jobs: uses: astral-sh/setup-uv@v5 with: python-version: ${{ matrix.python-version }} - - name: ruff format --check - run: uv run ruff check + - name: ruff format + run: uv run ruff format --check - name: ruff check run: uv run ruff check @@ -110,4 +110,3 @@ jobs: gh release upload '${{ github.ref_name }}' dist/** --repo '${{ github.repository }}' - diff --git a/examples/basic_usage.py b/examples/basic_usage.py index d420c35..66b10cd 100644 --- a/examples/basic_usage.py +++ b/examples/basic_usage.py @@ -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 @@ -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}") @@ -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 @@ -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}") @@ -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() \ No newline at end of file + main() diff --git a/examples/card_token_example.py b/examples/card_token_example.py index cc3b319..19cc73b 100644 --- a/examples/card_token_example.py +++ b/examples/card_token_example.py @@ -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 @@ -13,7 +14,7 @@ def main(): "holder_name": "John Doe", "expiration_month": 12, "expiration_year": 2025, - "cvv": "123" + "cvv": "123", } # Tokenize the card @@ -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 = { @@ -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}") @@ -69,7 +69,7 @@ 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") @@ -77,9 +77,10 @@ def main(): 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() \ No newline at end of file + main() diff --git a/examples/card_token_transaction_example.py b/examples/card_token_transaction_example.py index 2f4b731..64adf75 100644 --- a/examples/card_token_transaction_example.py +++ b/examples/card_token_transaction_example.py @@ -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) @@ -13,13 +10,13 @@ "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 @@ -27,21 +24,21 @@ "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)) \ No newline at end of file + print("Erro:", str(e)) diff --git a/examples/credit_card_advanced_example.py b/examples/credit_card_advanced_example.py index 9d41106..7cb54bd 100644 --- a/examples/credit_card_advanced_example.py +++ b/examples/credit_card_advanced_example.py @@ -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 @@ -13,12 +10,12 @@ "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 @@ -26,18 +23,15 @@ "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 @@ -45,15 +39,17 @@ "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 @@ -61,14 +57,16 @@ "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)) \ No newline at end of file + print("Erro:", str(e)) diff --git a/examples/pix_example.py b/examples/pix_example.py index 2c27d6d..e0fdb26 100644 --- a/examples/pix_example.py +++ b/examples/pix_example.py @@ -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 @@ -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}") @@ -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": @@ -68,5 +66,6 @@ def main(): else: print(f"\nCharge cannot be canceled: {updated_charge.status}") + if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/tests/conftest.py b/tests/conftest.py index ee4c1a9..cd9f5cb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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(): @@ -18,5 +17,5 @@ def mock_response(): "created_at": "2024-03-20T10:00:00Z", "amount": 1000, "currency": "BRL", - "description": "Test charge" - } \ No newline at end of file + "description": "Test charge", + } diff --git a/tests/test_integration.py b/tests/test_integration.py index afc0505..5fd6f89 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -4,17 +4,14 @@ # Skip these tests if no API key is provided pytestmark = pytest.mark.skipif( - not os.getenv("BARTE_API_KEY"), - reason="No API key provided for integration tests" + not os.getenv("BARTE_API_KEY"), reason="No API key provided for integration tests" ) + class TestBarteIntegration: @pytest.fixture def client(self): - return BarteClient( - api_key=os.getenv("BARTE_API_KEY"), - environment="sandbox" - ) + return BarteClient(api_key=os.getenv("BARTE_API_KEY"), environment="sandbox") def test_create_and_fetch_charge(self, client): """Integration test for creating and fetching a charge""" @@ -26,13 +23,13 @@ def test_create_and_fetch_charge(self, client): "customer": { "name": "Integration Test", "tax_id": "123.456.789-00", - "email": "test@example.com" - } + "email": "test@example.com", + }, } - + charge = client.create_charge(charge_data) assert charge["status"] in ["pending", "processing"] - + # Fetch charge fetched_charge = client.get_charge(charge["id"]) - assert fetched_charge["id"] == charge["id"] \ No newline at end of file + assert fetched_charge["id"] == charge["id"]