Pular para conteúdo

Inicio

Descrição

Payment Emulation é uma biblioteca desenvolvida para ser usada em projetos Django para emular pagamentos com cartões bancários. Ela é ideal para ser usada em seus projetos para implementar um método de pagamento.

Requisitos

Payment Emulation requer o seguinte:

  • Django (>=5.1)
  • Python (>=3.10)

Instalação

1. Instalar a biblioteca Payment Emulation:

pip install payment-emulation

2. Adicione em INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'payment_emulation.payment',
]

3. Realize as migrações:

python manage.py migrate

Seeds pré-preparadas

Após executar o comando migrate, três registros de seed (dados iniciais) serão criados nos modelos Account (Conta) e Card (Cartão) da biblioteca. Esses dados são pré-configurados para simular cenários específicos de transações e podem ser recuperados através do método get_seeds().

Cada chave no dicionário retornado corresponde a um tipo de transação:

  • PROBATUS → Transação bem-sucedida (success).

  • REPROBI → Transação recusada (failure).

  • PENDENTE → Transação pendente (pending).

Veja como obter esses dados.

1
2
3
4
5
from payment_emulation.payment.paymentSDK import PaymentSDK


response = PaymentSDK.get_seeds()
print(response['PROBATUS'])
output:
{
    'account': {
        'cpf': '45230544015', 'account_holder_name': 'PROBATUS',
        'account_number': '830991818343', 'balance': Decimal('99999.00'), 
        'status': 'active'
    }, 
    'card': {
        'card_holder_name': 'PROBATUS', 'card_number': '4763871810133150',
        'validity': '12/29', 'cvv': '342', 'card_flag': 'VISA
    }
}

Exemplo

Vamos dar uma olhada em um exemplo rápido de como realizar uma transação bem-sucedida usando a biblioteca Payment Emulation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from payment_emulation.payment.paymentSDK import PaymentSDK


items = [
    {'id': 1, 'title': 'T-shirt', 'quantity': 3, 'unit_price': 49.99},
    {'id': 2, 'title': 'shoe', 'quantity': 1, 'unit_price': 149.99},
    {'id': 3, 'title': 'sweater', 'quantity': 2, 'unit_price': 80},
]

seeds = PaymentSDK.get_seeds()
probatus_seed = seeds['PROBATUS']
card = probatus_seed['card']

sdk = PaymentSDK(items)

response = sdk.payment(
    cpf=probatus_seed['account']['cpf'],
    card_namber=card['card_number'],
    validity=card['validity'],
    cvv=card['cvv'],
    holder=card['card_holder_name']
)

print(response)
output:
{
    "transaction": "success", 
    "items": [
        {"id": 1, "title": "T-shirt", "quantity": 3, "unit_price": 49.99}, 
        {"id": 2, "title": "shoe", "quantity": 1, "unit_price": 149.99}, 
        {"id": 3, "title": "sweater", "quantity": 2, "unit_price": 80}
    ], 
    "redirect_urls": null, 
    "address": null, 
    "payer": null,
    "amount": 459.96, 
    "created_at": "2024-12-31T10:38:12.465986-03:00"
}

License

MIT License

Copyright (c) 2024 Valdean José

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.