151 lines
3.6 KiB
Python
151 lines
3.6 KiB
Python
import json
|
|
import os
|
|
|
|
import requests
|
|
from layercake.extra_types import CreditCard
|
|
|
|
from iugu import Credentials, Iugu, Order, Status, Token
|
|
|
|
iugu = Iugu(
|
|
Credentials(
|
|
'AF01CF1B3451459F92666F10589278EE',
|
|
os.getenv('IUGU_API_TOKEN'),
|
|
test_mode=True,
|
|
),
|
|
)
|
|
|
|
|
|
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(**event)
|
|
invoice = iugu.create_invoice(order, postback_url='http://localhost')
|
|
|
|
print(invoice)
|
|
|
|
# assert invoice.id == '970cb579-c396-4e59-a323-ce61ae04f7bc-196c'
|
|
# 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(**event | {'payment_method': 'BANK_SLIP'})
|
|
invoice = iugu.create_invoice(order, postback_url='http://localhost')
|
|
|
|
assert (
|
|
invoice.pdf
|
|
== '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(),
|
|
)
|
|
|
|
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 isinstance(token, Token)
|
|
|
|
|
|
def test_charge_paid(monkeypatch):
|
|
monkeypatch.setattr(
|
|
requests,
|
|
'post',
|
|
lambda *args, **kwargs: MockResponse('tests/samples/iugu_charge_paid.json'),
|
|
)
|
|
|
|
charge = iugu.charge(
|
|
invoice_id='970cb579-c396-4e59-a323-ce61ae04f7bc-196c',
|
|
token=Token('testing'),
|
|
)
|
|
assert charge.status == Status.PAID
|
|
|
|
|
|
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=Token('testing'),
|
|
)
|
|
assert charge.status == Status.DECLINED
|
|
assert charge.response['status'] == 'unauthorized'
|
|
|
|
|
|
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)
|
|
|
|
|
|
event = {
|
|
'id': 'testing',
|
|
'name': 'Sérgio Siqueira',
|
|
'email': 'sergio@somosbeta.com.br',
|
|
'due_date': '2026-11-12',
|
|
'cpf': '07879819908',
|
|
'payment_method': 'PIX',
|
|
'address': {
|
|
'postcode': '82100410',
|
|
'address1': 'Rua Manoel José Pereira',
|
|
'address2': '202',
|
|
'neighborhood': 'Pilarzinho',
|
|
'city': 'Curitiba',
|
|
'state': 'PR',
|
|
},
|
|
'items': [
|
|
{
|
|
'id': '1',
|
|
'name': 'Pen',
|
|
'unit_price': 100,
|
|
},
|
|
],
|
|
}
|