wip iugu
This commit is contained in:
@@ -1,21 +1,91 @@
|
||||
from decimal import Decimal
|
||||
|
||||
import requests
|
||||
from aws_lambda_powertools.utilities.typing.lambda_context import LambdaContext
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
||||
|
||||
import events.payments.create_invoice as app
|
||||
|
||||
from ...test_iugu import MockResponse
|
||||
|
||||
def test_create_invoice(
|
||||
dynamodb_seeds,
|
||||
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||
lambda_context: LambdaContext,
|
||||
):
|
||||
event = {
|
||||
order_id = '121c1140-779d-4664-8d99-4a006a22f547'
|
||||
|
||||
|
||||
def _event(payment_method: str) -> dict:
|
||||
return {
|
||||
'detail': {
|
||||
'new_image': {
|
||||
'id': '',
|
||||
'sk': '0'
|
||||
'id': order_id,
|
||||
'sk': '0',
|
||||
'total': Decimal(' 267.3'),
|
||||
'name': 'Beta Educação',
|
||||
'payment_method': payment_method,
|
||||
'create_date': '2026-01-07T19:07:49.272967-03:00',
|
||||
'due_date': '2026-01-12T00:35:44.897447-03:00',
|
||||
'coupon': '10OFF',
|
||||
'discount': Decimal('-29.7'),
|
||||
'updated_at': '2026-01-07T19:07:51.512605-03:00',
|
||||
'tenant_id': 'cJtK9SsnJhKPyxESe7g3DG',
|
||||
'email': 'org+15608435000190@users.noreply.saladeaula.digital',
|
||||
'org_id': 'cJtK9SsnJhKPyxESe7g3DG',
|
||||
'cnpj': '15608435000190',
|
||||
'status': 'PENDING',
|
||||
'subtotal': Decimal('297'),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert app.lambda_handler(event, lambda_context) # type: ignore
|
||||
|
||||
def test_create_bank_slip_invoice(
|
||||
monkeypatch,
|
||||
dynamodb_seeds,
|
||||
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||
lambda_context: LambdaContext,
|
||||
):
|
||||
monkeypatch.setattr(
|
||||
requests,
|
||||
'post',
|
||||
lambda *args, **kwargs: MockResponse(
|
||||
'tests/samples/iugu_invoice_bank_slip.json'
|
||||
),
|
||||
)
|
||||
|
||||
assert app.lambda_handler(_event('BANK_SLIP'), lambda_context) # type: ignore
|
||||
|
||||
invoice = dynamodb_persistence_layer.get_item(KeyPair(order_id, 'INVOICE'))
|
||||
|
||||
assert (
|
||||
invoice['secure_url']
|
||||
== 'https://checkout.iugu.com/invoices/16f7aa3d-2e0b-41e9-987b-1dc95b957456-d7a2'
|
||||
)
|
||||
assert invoice['secure_id'] == '16f7aa3d-2e0b-41e9-987b-1dc95b957456-d7a2'
|
||||
assert (
|
||||
invoice['bank_slip']['bank_slip_url']
|
||||
== 'https://boletos.iugu.com/v1/public/invoice/16f7aa3d-2e0b-41e9-987b-1dc95b957456-d7a2/bank_slip'
|
||||
)
|
||||
|
||||
|
||||
def test_create_bank_slip_pix(
|
||||
monkeypatch,
|
||||
dynamodb_seeds,
|
||||
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||
lambda_context: LambdaContext,
|
||||
):
|
||||
monkeypatch.setattr(
|
||||
requests,
|
||||
'post',
|
||||
lambda *args, **kwargs: MockResponse('tests/samples/iugu_invoice_pix.json'),
|
||||
)
|
||||
|
||||
assert app.lambda_handler(_event('PIX'), lambda_context) # type: ignore
|
||||
|
||||
invoice = dynamodb_persistence_layer.get_item(KeyPair(order_id, 'INVOICE'))
|
||||
|
||||
assert (
|
||||
invoice['pix']['qrcode_text']
|
||||
== 'http://faturas.iugu.com/iugu_pix/970cb579-c396-4e59-a323-ce61ae04f7bc-196c/test/pay'
|
||||
)
|
||||
assert (
|
||||
invoice['pix']['qrcode_text']
|
||||
== 'http://faturas.iugu.com/iugu_pix/970cb579-c396-4e59-a323-ce61ae04f7bc-196c/test/pay'
|
||||
)
|
||||
|
||||
@@ -134,16 +134,7 @@
|
||||
"bank_slip_error_message": null,
|
||||
"recipient_cpf_cnpj": "15111975000164"
|
||||
},
|
||||
"pix": {
|
||||
"qrcode": "https://faturas.iugu.com/qr_code/16f7aa3d-2e0b-41e9-987b-1dc95b957456-d7a2",
|
||||
"qrcode_text": "00020101021226840014br.gov.bcb.pix2562qr.iugu.com/public/payload/v2/16F7AA3D2E0B41E9987B1DC95B957456520400005303986540530.005802BR5925IUGU INSTITUICAO DE PAGAM6009SAO PAULO62070503***630446D8",
|
||||
"status": "qr_code_created",
|
||||
"payer_cpf_cnpj": null,
|
||||
"payer_name": null,
|
||||
"end_to_end_id": null,
|
||||
"end_to_end_refund_id": null,
|
||||
"account_number_last_digits": null
|
||||
},
|
||||
"pix": null,
|
||||
"items": [
|
||||
{
|
||||
"id": "0F30A8B1ED244325BBC6C8A1AE34AE18",
|
||||
|
||||
13
orders-events/tests/samples/iugu_payment_token.json
Normal file
13
orders-events/tests/samples/iugu_payment_token.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"id": "ae5204f4-663b-451f-ad65-7c512badb84e",
|
||||
"method": "credit_card",
|
||||
"extra_info": {
|
||||
"bin": "411111",
|
||||
"year": 2029,
|
||||
"month": 12,
|
||||
"brand": "VISA",
|
||||
"holder_name": "S\u00e9rgio R Siqueira",
|
||||
"display_number": "XXXX-XXXX-XXXX-1111"
|
||||
},
|
||||
"test": true
|
||||
}
|
||||
@@ -14,12 +14,20 @@
|
||||
{"id": "2849f1d5-f4f1-411e-8497-ec3a40afc0ab", "sk": "ITEMS", "items": [ { "name": "CIPA Grau de Risco 1", "id": "3c27ea9c-9464-46a1-9717-8c1441793186", "quantity": 1, "unit_price": 99 }, { "name": "CIPA Grau de Risco 2", "id": "99bb3b60-4ded-4a8e-937c-ba2d78ec6454", "quantity": 1, "unit_price": 99 } ], "created_at": "2026-01-07T19:09:54.193859-03:00"}
|
||||
{"id": "2849f1d5-f4f1-411e-8497-ec3a40afc0ab", "sk": "ADDRESS", "city": "São José", "postcode": "88101001", "state": "SC", "created_at": "2026-01-07T19:09:54.193859-03:00", "address1": "Avenida Presidente Kennedy" "address2": "", "neighborhood": "Campinas"}
|
||||
|
||||
// Seeds for Iugu
|
||||
// file: tests/events/payments/test_create_invoice.py
|
||||
{"id": "121c1140-779d-4664-8d99-4a006a22f547", "sk": "0", "total": "267.3", "name": "Beta Educação", "payment_method": "BANK_SLIP", "create_date": "2026-01-07T19:07:49.272967-03:00", "due_date": "2026-01-12T00:35:44.897447-03:00", "coupon": "10OFF", "discount": "-29.7", "updated_at": "2026-01-07T19:07:51.512605-03:00", "tenant_id": "cJtK9SsnJhKPyxESe7g3DG", "email": "org+15608435000190@users.noreply.saladeaula.digital", "org_id": "cJtK9SsnJhKPyxESe7g3DG", "cnpj": "15608435000190", "status": "PENDING", "subtotal": 297}
|
||||
{"id": "121c1140-779d-4664-8d99-4a006a22f547", "sk": "ADDRESS", "city": "São José", "neighborhood": "Campinas", "address2": "", "postcode": "88101001", "state": "SC", "address1": "Avenida Presidente Kennedy", "created_at": "2026-01-07T19:07:49.272967-03:00"}
|
||||
{"id": "121c1140-779d-4664-8d99-4a006a22f547", "sk": "ITEMS", "items": [{"name": "CIPA Grau de Risco 2", "id": "99bb3b60-4ded-4a8e-937c-ba2d78ec6454", "quantity": 3, "unit_price": 99}], "created_at": "2026-01-07T19:07:49.272967-03:00"}
|
||||
|
||||
|
||||
// User data
|
||||
{"id": "5OxmMjL-ujoR5IMGegQz", "sk": "0", "name": "Sérgio R Siqueira"}
|
||||
{"id": "cnpj", "sk": "15608435000190", "user_id": "cJtK9SsnJhKPyxESe7g3DG"}
|
||||
{"id": "cpf", "sk": "07879819908", "user_id": "5OxmMjL-ujoR5IMGegQz"}
|
||||
{"id": "email", "sk": "sergio@somosbeta.com.br", "user_id": "5OxmMjL-ujoR5IMGegQz"}
|
||||
|
||||
// @DEPRECATED
|
||||
// Slots
|
||||
{"id": "vacancies#cJtK9SsnJhKPyxESe7g3DG", "sk": "9omWNKymwU5U4aeun6mWzZ#1"}
|
||||
{"id": "vacancies#cJtK9SsnJhKPyxESe7g3DG", "sk": "9omWNKymwU5U4aeun6mWzZ#2"}
|
||||
|
||||
@@ -1,19 +1,42 @@
|
||||
import json
|
||||
import os
|
||||
from uuid import uuid4
|
||||
|
||||
import requests
|
||||
from layercake.extra_types import CreditCard
|
||||
|
||||
from iugu import Credentials, Iugu, Order, Status, Token
|
||||
from iugu import Credentials, Iugu, Order
|
||||
|
||||
iugu = Iugu(
|
||||
Credentials(
|
||||
'AF01CF1B3451459F92666F10589278EE',
|
||||
os.getenv('IUGU_API_TOKEN'),
|
||||
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:
|
||||
@@ -31,22 +54,24 @@ class MockResponse:
|
||||
|
||||
|
||||
def test_create_invoice_pix(monkeypatch):
|
||||
# monkeypatch.setattr(
|
||||
# requests,
|
||||
# 'post',
|
||||
# lambda *args, **kwargs: MockResponse('tests/samples/iugu_invoice_pix.json'),
|
||||
# )
|
||||
monkeypatch.setattr(
|
||||
requests,
|
||||
'post',
|
||||
lambda *args, **kwargs: MockResponse('tests/samples/iugu_invoice_pix.json'),
|
||||
)
|
||||
|
||||
order = Order(**event)
|
||||
order = Order(
|
||||
id=str(uuid4()),
|
||||
payment_method='PIX', # type: ignore
|
||||
**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'
|
||||
# )
|
||||
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):
|
||||
@@ -58,11 +83,16 @@ def test_create_invoice_bank_slip(monkeypatch):
|
||||
),
|
||||
)
|
||||
|
||||
order = Order(**event | {'payment_method': 'BANK_SLIP'})
|
||||
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.pdf
|
||||
invoice['bank_slip']['bank_slip_url']
|
||||
== 'https://boletos.iugu.com/v1/public/invoice/16f7aa3d-2e0b-41e9-987b-1dc95b957456-d7a2/bank_slip'
|
||||
)
|
||||
|
||||
@@ -71,7 +101,7 @@ def test_payment_token(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
requests,
|
||||
'post',
|
||||
lambda *args, **kwargs: MockResponse(),
|
||||
lambda *args, **kwargs: MockResponse('tests/samples/iugu_payment_token.json'),
|
||||
)
|
||||
|
||||
credit_card = CreditCard(
|
||||
@@ -82,7 +112,7 @@ def test_payment_token(monkeypatch):
|
||||
exp_year='2029',
|
||||
)
|
||||
token = iugu.payment_token(credit_card)
|
||||
assert isinstance(token, Token)
|
||||
assert token['id'] == 'ae5204f4-663b-451f-ad65-7c512badb84e'
|
||||
|
||||
|
||||
def test_charge_paid(monkeypatch):
|
||||
@@ -93,10 +123,10 @@ def test_charge_paid(monkeypatch):
|
||||
)
|
||||
|
||||
charge = iugu.charge(
|
||||
invoice_id='970cb579-c396-4e59-a323-ce61ae04f7bc-196c',
|
||||
token=Token('testing'),
|
||||
invoice_id='f92efd60-e6a9-45cf-bd8e-6b15a3d5c3ab-173c',
|
||||
token='testing',
|
||||
)
|
||||
assert charge.status == Status.PAID
|
||||
assert charge['success'] is True
|
||||
|
||||
|
||||
def test_charge_declined(monkeypatch):
|
||||
@@ -108,10 +138,10 @@ def test_charge_declined(monkeypatch):
|
||||
|
||||
charge = iugu.charge(
|
||||
invoice_id='970cb579-c396-4e59-a323-ce61ae04f7bc-196c',
|
||||
token=Token('testing'),
|
||||
token='testing',
|
||||
)
|
||||
assert charge.status == Status.DECLINED
|
||||
assert charge.response['status'] == 'unauthorized'
|
||||
|
||||
assert charge['success'] is False
|
||||
|
||||
|
||||
def test_get_invoice(monkeypatch):
|
||||
@@ -123,28 +153,3 @@ def test_get_invoice(monkeypatch):
|
||||
|
||||
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,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user