156 lines
3.8 KiB
Python
156 lines
3.8 KiB
Python
import json
|
|
import os
|
|
from uuid import uuid4
|
|
|
|
import requests
|
|
from layercake.extra_types import CreditCard
|
|
|
|
from iugu import Credentials, Iugu, Order
|
|
|
|
iugu = Iugu(
|
|
Credentials(
|
|
'AF01CF1B3451459F92666F10589278EE',
|
|
os.getenv('IUGU_API_TOKEN'), # type: ignore
|
|
test_mode=True,
|
|
),
|
|
)
|
|
|
|
event = {
|
|
'name': 'Sérgio Siqueira',
|
|
'email': 'sergio@somosbeta.com.br',
|
|
'due_date': '2026-11-12',
|
|
'cpf': '07879819908',
|
|
'address': {
|
|
'postcode': '82100410',
|
|
'address1': 'Rua Manoel José Pereira',
|
|
'address2': '202',
|
|
'neighborhood': 'Pilarzinho',
|
|
'city': 'Curitiba',
|
|
'state': 'PR',
|
|
},
|
|
'items': [
|
|
{
|
|
'id': '1',
|
|
'name': 'Pen',
|
|
'unit_price': 100,
|
|
},
|
|
],
|
|
}
|
|
|
|
|
|
class MockResponse:
|
|
def __init__(self, jsonfile: str | None = None) -> None:
|
|
self.jsonfile = jsonfile
|
|
|
|
def json(self):
|
|
if not self.jsonfile:
|
|
return {'id': 'testing'}
|
|
|
|
with open(self.jsonfile) as f:
|
|
return json.loads(f.read())
|
|
|
|
@staticmethod
|
|
def raise_for_status(): ...
|
|
|
|
|
|
def test_create_invoice_pix(monkeypatch):
|
|
monkeypatch.setattr(
|
|
requests,
|
|
'post',
|
|
lambda *args, **kwargs: MockResponse('tests/samples/iugu_invoice_pix.json'),
|
|
)
|
|
|
|
order = Order(
|
|
id=str(uuid4()),
|
|
payment_method='PIX', # type: ignore
|
|
**event,
|
|
)
|
|
invoice = iugu.create_invoice(order, postback_url='http://localhost')
|
|
|
|
assert invoice['id'] == '970CB579C3964E59A323CE61AE04F7BC'
|
|
assert (
|
|
invoice['pix']['qrcode_text']
|
|
== 'http://faturas.iugu.com/iugu_pix/970cb579-c396-4e59-a323-ce61ae04f7bc-196c/test/pay'
|
|
)
|
|
|
|
|
|
def test_create_invoice_bank_slip(monkeypatch):
|
|
monkeypatch.setattr(
|
|
requests,
|
|
'post',
|
|
lambda *args, **kwargs: MockResponse(
|
|
'tests/samples/iugu_invoice_bank_slip.json'
|
|
),
|
|
)
|
|
|
|
order = Order(
|
|
id=str(uuid4()),
|
|
payment_method='BANK_SLIP', # type: ignore
|
|
**event,
|
|
)
|
|
invoice = iugu.create_invoice(order, postback_url='http://localhost')
|
|
|
|
assert invoice['id'] == '16F7AA3D2E0B41E9987B1DC95B957456'
|
|
assert (
|
|
invoice['bank_slip']['bank_slip_url']
|
|
== 'https://boletos.iugu.com/v1/public/invoice/16f7aa3d-2e0b-41e9-987b-1dc95b957456-d7a2/bank_slip'
|
|
)
|
|
|
|
|
|
def test_payment_token(monkeypatch):
|
|
monkeypatch.setattr(
|
|
requests,
|
|
'post',
|
|
lambda *args, **kwargs: MockResponse('tests/samples/iugu_payment_token.json'),
|
|
)
|
|
|
|
credit_card = CreditCard(
|
|
holder_name='Sérgio R Siqueira',
|
|
number='4111111111111111', # type: ignore
|
|
cvv='123',
|
|
exp_month='12',
|
|
exp_year='2029',
|
|
)
|
|
token = iugu.payment_token(credit_card)
|
|
assert token['id'] == 'ae5204f4-663b-451f-ad65-7c512badb84e'
|
|
|
|
|
|
def test_charge_paid(monkeypatch):
|
|
monkeypatch.setattr(
|
|
requests,
|
|
'post',
|
|
lambda *args, **kwargs: MockResponse('tests/samples/iugu_charge_paid.json'),
|
|
)
|
|
|
|
charge = iugu.charge(
|
|
invoice_id='f92efd60-e6a9-45cf-bd8e-6b15a3d5c3ab-173c',
|
|
token='testing',
|
|
)
|
|
assert charge['success'] is True
|
|
|
|
|
|
def test_charge_declined(monkeypatch):
|
|
monkeypatch.setattr(
|
|
requests,
|
|
'post',
|
|
lambda *args, **kwargs: MockResponse('tests/samples/iugu_charge_declined.json'),
|
|
)
|
|
|
|
charge = iugu.charge(
|
|
invoice_id='970cb579-c396-4e59-a323-ce61ae04f7bc-196c',
|
|
token='testing',
|
|
)
|
|
|
|
assert charge['success'] is False
|
|
|
|
|
|
def test_get_invoice(monkeypatch):
|
|
monkeypatch.setattr(
|
|
requests,
|
|
'get',
|
|
lambda *args, **kwargs: MockResponse('tests/samples/iugu_invoice_pix.json'),
|
|
)
|
|
|
|
invoice = iugu.get_invoice('970cb579-c396-4e59-a323-ce61ae04f7bc-196c')
|
|
assert isinstance(invoice, dict)
|