add support to milti transaction

This commit is contained in:
2026-01-13 03:18:05 -03:00
parent ad23e9aa51
commit da10a36a1d
11 changed files with 287 additions and 56 deletions

View File

@@ -0,0 +1,48 @@
import requests
from aws_lambda_powertools.utilities.typing.lambda_context import LambdaContext
from layercake.dynamodb import DynamoDBPersistenceLayer
import events.payments.charge_credit_card as app
from ...test_iugu import MockResponse
event = {
'detail': {
'new_image': {
'credit_card': {
'number': '4242424242424242',
'cvv': '123',
'created_at': '2026-01-13T02:33:57.088176-03:00',
'exp_month': '03',
'exp_year': '2027',
'ttl': 1768282737,
'holder_name': 'Sergio R Siqueira',
},
'created_at': '2026-01-13T02:34:00.065619-03:00',
'invoice_id': '493dd32a-b646-40b2-a307-0fcb3d42d954-2097',
'installments': 12,
'ttl': 1768282740,
'id': '121c1140-779d-4664-8d99-4a006a22f547',
'sk': 'TRANSACTION',
}
}
}
def test_charge_credit_card(
monkeypatch,
dynamodb_seeds,
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
lambda_context: LambdaContext,
):
monkeypatch.setattr(
requests,
'post',
lambda url, *args, **kwargs: MockResponse(
'tests/samples/iugu_payment_token.json'
if 'payment_token' in url
else 'tests/samples/iugu_charge_paid.json'
),
)
assert app.lambda_handler(event, lambda_context) # type: ignore

View File

@@ -2,7 +2,7 @@ from decimal import Decimal
import requests
from aws_lambda_powertools.utilities.typing.lambda_context import LambdaContext
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, SortKey, TransactKey
import events.payments.create_invoice as app
@@ -89,3 +89,37 @@ def test_create_bank_slip_pix(
invoice['pix']['qrcode_text']
== 'http://faturas.iugu.com/iugu_pix/970cb579-c396-4e59-a323-ce61ae04f7bc-196c/test/pay'
)
def test_create_credit_card(
monkeypatch,
dynamodb_seeds,
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
lambda_context: LambdaContext,
):
monkeypatch.setattr(
requests,
'post',
lambda *args, **kwargs: MockResponse(
'tests/samples/iugu_invoice_credit_card.json'
),
)
assert app.lambda_handler(_event('CREDIT_CARD'), lambda_context) # type: ignore
invoice = dynamodb_persistence_layer.collection.get_items(
TransactKey(order_id)
+ SortKey('0')
+ SortKey('INVOICE')
+ SortKey('TRANSACTION')
)
print(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'
# )

View File

@@ -16,11 +16,12 @@
// Seeds for Iugu
// file: tests/test_app.py
// file: tests/events/payments/test_charge_credit_card.py
// 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"}
{"id": "121c1140-779d-4664-8d99-4a006a22f547", "sk": "CREDIT_CARD#PAYMENT_INTENT", "credit_card": {"number": "4111111111111111","cvv": "123","created_at": "2026-01-13T02:33:57.088176-03:00","exp_month": "03","exp_year": "2027","ttl": 1768282737,"holder_name": "Sergio R Siqueira"}}
// User data
{"id": "5OxmMjL-ujoR5IMGegQz", "sk": "0", "name": "Sérgio R Siqueira"}

View File

@@ -35,6 +35,7 @@ event = {
'unit_price': 100,
},
],
'discount': -10,
}
@@ -53,27 +54,6 @@ class MockResponse:
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,
@@ -97,6 +77,27 @@ def test_create_invoice_bank_slip(monkeypatch):
)
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_payment_token(monkeypatch):
monkeypatch.setattr(
requests,