|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Exemplo de uso da biblioteca pypix com Python 3.9+ |
| 5 | +Demonstra as funcionalidades principais da biblioteca com recursos modernos do Python |
| 6 | +""" |
| 7 | +from pypix import Pix |
| 8 | +from dataclasses import dataclass |
| 9 | +from typing import Optional |
| 10 | + |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class PixPayment: |
| 14 | + """Classe de exemplo usando dataclass do Python 3.7+""" |
| 15 | + key: str |
| 16 | + description: Optional[str] = None |
| 17 | + amount: Optional[float] = None |
| 18 | + merchant_name: str = "" |
| 19 | + merchant_city: str = "" |
| 20 | + txid: Optional[str] = None |
| 21 | + |
| 22 | + |
| 23 | +def create_pix_from_dataclass(payment: PixPayment) -> Pix: |
| 24 | + """Cria um objeto Pix a partir de um dataclass""" |
| 25 | + pix = Pix() |
| 26 | + pix.set_pixkey(payment.key) |
| 27 | + |
| 28 | + if payment.description: |
| 29 | + pix.set_description(payment.description) |
| 30 | + |
| 31 | + if payment.amount is not None: |
| 32 | + pix.set_amount(payment.amount) |
| 33 | + |
| 34 | + pix.set_merchant_name(payment.merchant_name) |
| 35 | + pix.set_merchant_city(payment.merchant_city) |
| 36 | + |
| 37 | + if payment.txid: |
| 38 | + pix.set_txid(payment.txid) |
| 39 | + |
| 40 | + return pix |
| 41 | + |
| 42 | + |
| 43 | +def main(): |
| 44 | + """Função principal""" |
| 45 | + # Exemplo usando dataclass (recurso do Python 3.7+) |
| 46 | + payment = PixPayment( |
| 47 | + key="10000000000", |
| 48 | + description="Pagamento via Python 3.9+", |
| 49 | + amount=150.75, |
| 50 | + merchant_name="EMPRESA MODERNA LTDA", |
| 51 | + merchant_city="SAO PAULO", |
| 52 | + txid="Cafe" |
| 53 | + ) |
| 54 | + |
| 55 | + pix = create_pix_from_dataclass(payment) |
| 56 | + print("QR Code Pix (usando dataclass):") |
| 57 | + print(pix) |
| 58 | + print("\n") |
| 59 | + |
| 60 | + # Exemplo com type hints (melhorado no Python 3.9+) |
| 61 | + print("Exemplo com caracteres especiais (serão sanitizados):") |
| 62 | + pix2 = Pix() |
| 63 | + pix2.set_pixkey("12345678910") |
| 64 | + pix2.set_description("Café & Açaí") # Caracteres especiais serão sanitizados |
| 65 | + pix2.set_amount(25.99) |
| 66 | + pix2.set_merchant_name("João da Silva") |
| 67 | + pix2.set_merchant_city("FLORIANÓPOLIS") |
| 68 | + pix2.set_txid("cafe2023") |
| 69 | + print(pix2) |
| 70 | + |
| 71 | + # Demonstração de arredondamento correto |
| 72 | + print("\nDemonstração de arredondamento:") |
| 73 | + pix3 = Pix() |
| 74 | + pix3.set_pixkey("11122233344") |
| 75 | + pix3.set_merchant_name("TESTE ARREDONDAMENTO") |
| 76 | + pix3.set_merchant_city("BRASILIA") |
| 77 | + |
| 78 | + # Valores com diferentes casas decimais |
| 79 | + values = [10.555, 99.995, 0.001, 0.009] |
| 80 | + for value in values: |
| 81 | + pix3.set_amount(value) |
| 82 | + print(f"Original: {value} -> Formatado: {pix3.amount}") |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + main() |
0 commit comments