add support to milti transaction
This commit is contained in:
@@ -40,7 +40,7 @@ class PaymentMethod(str, Enum):
|
|||||||
PIX = 'PIX'
|
PIX = 'PIX'
|
||||||
CREDIT_CARD = 'CREDIT_CARD'
|
CREDIT_CARD = 'CREDIT_CARD'
|
||||||
BANK_SLIP = 'BANK_SLIP'
|
BANK_SLIP = 'BANK_SLIP'
|
||||||
MANUAL = 'MANUAL'
|
# MANUAL = 'MANUAL'
|
||||||
|
|
||||||
|
|
||||||
class User(BaseModel):
|
class User(BaseModel):
|
||||||
@@ -213,8 +213,8 @@ def checkout(payload: Checkout):
|
|||||||
)
|
)
|
||||||
transact.put(
|
transact.put(
|
||||||
item={
|
item={
|
||||||
'id': 'TRANSACTION',
|
'id': order_id,
|
||||||
'sk': order_id,
|
'sk': 'CREDIT_CARD#PAYMENT_INTENT',
|
||||||
'ttl': ttl(start_dt=now_, minutes=5),
|
'ttl': ttl(start_dt=now_, minutes=5),
|
||||||
'created_at': now_,
|
'created_at': now_,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from enum import Enum
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from urllib.parse import parse_qsl
|
from urllib.parse import parse_qsl
|
||||||
@@ -7,6 +8,7 @@ from aws_lambda_powertools.event_handler.api_gateway import (
|
|||||||
APIGatewayHttpResolver,
|
APIGatewayHttpResolver,
|
||||||
Response,
|
Response,
|
||||||
)
|
)
|
||||||
|
from aws_lambda_powertools.event_handler.exceptions import NotFoundError
|
||||||
from aws_lambda_powertools.logging import correlation_paths
|
from aws_lambda_powertools.logging import correlation_paths
|
||||||
from aws_lambda_powertools.utilities.typing import LambdaContext
|
from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||||
from layercake.dateutils import now
|
from layercake.dateutils import now
|
||||||
@@ -21,34 +23,72 @@ app = APIGatewayHttpResolver(enable_validation=True)
|
|||||||
dyn = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
|
dyn = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
|
||||||
|
|
||||||
|
|
||||||
|
class OrderNotFoundError(NotFoundError): ...
|
||||||
|
|
||||||
|
|
||||||
|
class InvoiceNotFoundError(NotFoundError): ...
|
||||||
|
|
||||||
|
|
||||||
|
class StatusAttr(Enum):
|
||||||
|
PAID = 'paid_at'
|
||||||
|
CANCELED = 'canceled_at'
|
||||||
|
REFUNDED = 'refunded_at'
|
||||||
|
EXPIRED = 'expired_at'
|
||||||
|
EXTERNALLY_PAID = 'paid_at'
|
||||||
|
|
||||||
|
|
||||||
|
def _status_attr(status: str) -> StatusAttr | None:
|
||||||
|
try:
|
||||||
|
return StatusAttr[status]
|
||||||
|
except KeyError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
@app.post('/<order_id>/postback')
|
@app.post('/<order_id>/postback')
|
||||||
@tracer.capture_method
|
@tracer.capture_method
|
||||||
def postback(order_id: str):
|
def postback(order_id: str):
|
||||||
decoded_body = dict(parse_qsl(app.current_event.decoded_body))
|
decoded_body = dict(parse_qsl(app.current_event.decoded_body))
|
||||||
logger.info('IUGU Postback', decoded_body=decoded_body)
|
logger.info('IUGU Postback', decoded_body=decoded_body)
|
||||||
event = decoded_body['event']
|
|
||||||
status = decoded_body['data[status]'].upper()
|
|
||||||
|
|
||||||
if event != 'invoice.status_changed':
|
now_ = now()
|
||||||
|
event = decoded_body['event']
|
||||||
|
status = decoded_body.get('data[status]', '').upper()
|
||||||
|
status_attr = _status_attr(status)
|
||||||
|
|
||||||
|
if event != 'invoice.status_changed' or not status_attr:
|
||||||
return Response(status_code=HTTPStatus.NO_CONTENT)
|
return Response(status_code=HTTPStatus.NO_CONTENT)
|
||||||
|
|
||||||
try:
|
with dyn.transact_writer() as transact:
|
||||||
dyn.update_item(
|
transact.update(
|
||||||
key=KeyPair(order_id, '0'),
|
key=KeyPair(order_id, '0'),
|
||||||
update_expr='SET #status = :status, \
|
update_expr='SET #status = :status, \
|
||||||
|
#status_attr = :now, \
|
||||||
updated_at = :now',
|
updated_at = :now',
|
||||||
cond_expr='attribute_exists(sk)',
|
cond_expr='attribute_exists(sk)',
|
||||||
expr_attr_names={
|
expr_attr_names={
|
||||||
'#status': 'status',
|
'#status': 'status',
|
||||||
|
'#status_attr': status_attr.value,
|
||||||
},
|
},
|
||||||
expr_attr_values={
|
expr_attr_values={
|
||||||
':status': status,
|
':status': status,
|
||||||
':now': now(),
|
':now': now_,
|
||||||
},
|
},
|
||||||
|
exc_cls=OrderNotFoundError,
|
||||||
)
|
)
|
||||||
except Exception:
|
|
||||||
return Response(status_code=HTTPStatus.NOT_FOUND)
|
if status == 'EXTERNALLY_PAID':
|
||||||
else:
|
transact.update(
|
||||||
|
key=KeyPair(order_id, 'INVOICE'),
|
||||||
|
cond_expr='attribute_exists(sk)',
|
||||||
|
update_expr='SET externally_paid = :true, \
|
||||||
|
updated_at = :now',
|
||||||
|
expr_attr_values={
|
||||||
|
':true': True,
|
||||||
|
':now': now_,
|
||||||
|
},
|
||||||
|
exc_cls=InvoiceNotFoundError,
|
||||||
|
)
|
||||||
|
|
||||||
return Response(status_code=HTTPStatus.NO_CONTENT)
|
return Response(status_code=HTTPStatus.NO_CONTENT)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,12 @@ from aws_lambda_powertools.utilities.data_classes import (
|
|||||||
event_source,
|
event_source,
|
||||||
)
|
)
|
||||||
from aws_lambda_powertools.utilities.typing import LambdaContext
|
from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||||
|
from layercake.dateutils import now
|
||||||
from layercake.dynamodb import (
|
from layercake.dynamodb import (
|
||||||
DynamoDBPersistenceLayer,
|
DynamoDBPersistenceLayer,
|
||||||
|
KeyPair,
|
||||||
)
|
)
|
||||||
|
from layercake.extra_types import CreditCard
|
||||||
|
|
||||||
from boto3clients import dynamodb_client
|
from boto3clients import dynamodb_client
|
||||||
from config import IUGU_ACCOUNT_ID, IUGU_API_TOKEN, IUGU_TEST_MODE, ORDER_TABLE
|
from config import IUGU_ACCOUNT_ID, IUGU_API_TOKEN, IUGU_TEST_MODE, ORDER_TABLE
|
||||||
@@ -25,4 +28,72 @@ iugu = Iugu(
|
|||||||
|
|
||||||
@event_source(data_class=EventBridgeEvent)
|
@event_source(data_class=EventBridgeEvent)
|
||||||
@logger.inject_lambda_context
|
@logger.inject_lambda_context
|
||||||
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool: ...
|
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||||
|
new_image = event.detail['new_image']
|
||||||
|
order_id = new_image['id']
|
||||||
|
invoice_id = new_image['invoice_id']
|
||||||
|
installments = new_image['installments']
|
||||||
|
credit_card = CreditCard(**new_image['credit_card'])
|
||||||
|
now_ = now()
|
||||||
|
|
||||||
|
token = iugu.payment_token(credit_card)
|
||||||
|
charge = iugu.charge(
|
||||||
|
invoice_id=invoice_id,
|
||||||
|
token=token['id'],
|
||||||
|
installments=installments,
|
||||||
|
)
|
||||||
|
|
||||||
|
with dyn.transact_writer() as transact:
|
||||||
|
transact.delete(key=KeyPair(order_id, 'TRANSACTION'))
|
||||||
|
transact.update(
|
||||||
|
key=KeyPair(order_id, 'TRANSACTION#STATS'),
|
||||||
|
update_expr='SET #count = if_not_exists(#count, :zero) + :one, \
|
||||||
|
updated_at = :now',
|
||||||
|
expr_attr_names={
|
||||||
|
'#count': 'payment_attempts',
|
||||||
|
},
|
||||||
|
expr_attr_values={
|
||||||
|
':zero': 0,
|
||||||
|
':one': 1,
|
||||||
|
':now': now(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if charge['success'] is True:
|
||||||
|
transact.update(
|
||||||
|
key=KeyPair(order_id, '0'),
|
||||||
|
update_expr='SET #status = :status, \
|
||||||
|
paid_at = :now, \
|
||||||
|
updated_at = :now',
|
||||||
|
expr_attr_names={
|
||||||
|
'#status': 'status',
|
||||||
|
},
|
||||||
|
expr_attr_values={
|
||||||
|
':status': 'PAID',
|
||||||
|
':now': now_,
|
||||||
|
},
|
||||||
|
cond_expr='attribute_exists(sk)',
|
||||||
|
)
|
||||||
|
transact.put(
|
||||||
|
item={
|
||||||
|
'id': order_id,
|
||||||
|
'sk': f'TRANSACTION#ATTEMPTS#{now_.isoformat()}',
|
||||||
|
'brand': credit_card.brand,
|
||||||
|
'last4': credit_card.last4,
|
||||||
|
'status': 'SUCCEEDED',
|
||||||
|
'transaction': charge,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
transact.put(
|
||||||
|
item={
|
||||||
|
'id': order_id,
|
||||||
|
'sk': f'TRANSACTION#ATTEMPTS#{now_.isoformat()}',
|
||||||
|
'brand': credit_card.brand,
|
||||||
|
'last4': credit_card.last4,
|
||||||
|
'status': 'FAILED',
|
||||||
|
'transaction': charge,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
return charge['success']
|
||||||
|
|||||||
@@ -4,9 +4,10 @@ from aws_lambda_powertools.utilities.data_classes import (
|
|||||||
event_source,
|
event_source,
|
||||||
)
|
)
|
||||||
from aws_lambda_powertools.utilities.typing import LambdaContext
|
from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||||
from layercake.dateutils import now
|
from layercake.dateutils import now, ttl
|
||||||
from layercake.dynamodb import (
|
from layercake.dynamodb import (
|
||||||
DynamoDBPersistenceLayer,
|
DynamoDBPersistenceLayer,
|
||||||
|
KeyPair,
|
||||||
SortKey,
|
SortKey,
|
||||||
TransactKey,
|
TransactKey,
|
||||||
)
|
)
|
||||||
@@ -41,7 +42,8 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
r = dyn.collection.get_items(
|
r = dyn.collection.get_items(
|
||||||
TransactKey(order_id)
|
TransactKey(order_id)
|
||||||
+ SortKey('ADDRESS', rename_key='address')
|
+ SortKey('ADDRESS', rename_key='address')
|
||||||
+ SortKey('ITEMS', path_spec='items', rename_key='items'),
|
+ SortKey('ITEMS', path_spec='items', rename_key='items')
|
||||||
|
+ SortKey('CREDIT_CARD#PAYMENT_INTENT', rename_key='credit_card'),
|
||||||
flatten_top=False,
|
flatten_top=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -59,7 +61,8 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dyn.put_item(
|
with dyn.transact_writer() as transact:
|
||||||
|
transact.put(
|
||||||
item={
|
item={
|
||||||
'id': order_id,
|
'id': order_id,
|
||||||
'sk': 'INVOICE',
|
'sk': 'INVOICE',
|
||||||
@@ -74,6 +77,24 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
| ({'pix': invoice['pix']} if is_pix else {}),
|
| ({'pix': invoice['pix']} if is_pix else {}),
|
||||||
cond_expr='attribute_not_exists(sk)',
|
cond_expr='attribute_not_exists(sk)',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if 'credit_card' in r:
|
||||||
|
transact.delete(
|
||||||
|
key=KeyPair(order_id, 'CREDIT_CARD#PAYMENT_INTENT'),
|
||||||
|
)
|
||||||
|
transact.put(
|
||||||
|
item={
|
||||||
|
'id': order_id,
|
||||||
|
'sk': 'TRANSACTION',
|
||||||
|
'invoice_id': invoice['secure_id'],
|
||||||
|
'credit_card': r['credit_card'],
|
||||||
|
'installments': int(new_image.get('installments', 1)),
|
||||||
|
'ttl': ttl(start_dt=now_, minutes=5),
|
||||||
|
'created_at': now_,
|
||||||
|
},
|
||||||
|
cond_expr='attribute_not_exists(sk)',
|
||||||
|
)
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ class Order(BaseModel):
|
|||||||
address: Address
|
address: Address
|
||||||
items: tuple[Item, ...]
|
items: tuple[Item, ...]
|
||||||
payment_method: PaymentMethod
|
payment_method: PaymentMethod
|
||||||
|
discount: Decimal = Decimal('0')
|
||||||
cpf: str | None = None
|
cpf: str | None = None
|
||||||
cnpj: str | None = None
|
cnpj: str | None = None
|
||||||
|
|
||||||
@@ -127,6 +128,7 @@ class Iugu:
|
|||||||
}
|
}
|
||||||
for item in order.items
|
for item in order.items
|
||||||
]
|
]
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
'order_id': order.id,
|
'order_id': order.id,
|
||||||
'external_reference': order.id,
|
'external_reference': order.id,
|
||||||
@@ -135,6 +137,7 @@ class Iugu:
|
|||||||
'email': order.email,
|
'email': order.email,
|
||||||
'payable_with': order.payment_method.lower(),
|
'payable_with': order.payment_method.lower(),
|
||||||
'notification_url': postback_url.geturl(),
|
'notification_url': postback_url.geturl(),
|
||||||
|
'discount_cents': int(order.discount * -100),
|
||||||
'payer': {
|
'payer': {
|
||||||
'name': order.name,
|
'name': order.name,
|
||||||
'email': order.email,
|
'email': order.email,
|
||||||
|
|||||||
@@ -116,8 +116,20 @@ Resources:
|
|||||||
detail-type: [INSERT]
|
detail-type: [INSERT]
|
||||||
detail:
|
detail:
|
||||||
new_image:
|
new_image:
|
||||||
sk: ['INVOICE']
|
sk: ['TRANSACTION']
|
||||||
payment_method: ['CREDIT_CARD']
|
invoice_id:
|
||||||
|
- exists: true
|
||||||
|
credit_card:
|
||||||
|
holder_name:
|
||||||
|
- exists: true
|
||||||
|
number:
|
||||||
|
- exists: true
|
||||||
|
exp_month:
|
||||||
|
- exists: true
|
||||||
|
exp_year:
|
||||||
|
- exists: true
|
||||||
|
cvv:
|
||||||
|
- exists: true
|
||||||
|
|
||||||
EventBillingAppendEnrollmentFunction:
|
EventBillingAppendEnrollmentFunction:
|
||||||
Type: AWS::Serverless::Function
|
Type: AWS::Serverless::Function
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -2,7 +2,7 @@ from decimal import Decimal
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
from aws_lambda_powertools.utilities.typing.lambda_context import LambdaContext
|
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
|
import events.payments.create_invoice as app
|
||||||
|
|
||||||
@@ -89,3 +89,37 @@ def test_create_bank_slip_pix(
|
|||||||
invoice['pix']['qrcode_text']
|
invoice['pix']['qrcode_text']
|
||||||
== 'http://faturas.iugu.com/iugu_pix/970cb579-c396-4e59-a323-ce61ae04f7bc-196c/test/pay'
|
== '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'
|
||||||
|
# )
|
||||||
|
|||||||
@@ -16,11 +16,12 @@
|
|||||||
|
|
||||||
// Seeds for Iugu
|
// Seeds for Iugu
|
||||||
// file: tests/test_app.py
|
// file: tests/test_app.py
|
||||||
|
// file: tests/events/payments/test_charge_credit_card.py
|
||||||
// file: tests/events/payments/test_create_invoice.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": "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": "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": "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
|
// User data
|
||||||
{"id": "5OxmMjL-ujoR5IMGegQz", "sk": "0", "name": "Sérgio R Siqueira"}
|
{"id": "5OxmMjL-ujoR5IMGegQz", "sk": "0", "name": "Sérgio R Siqueira"}
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ event = {
|
|||||||
'unit_price': 100,
|
'unit_price': 100,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
'discount': -10,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -53,27 +54,6 @@ class MockResponse:
|
|||||||
def raise_for_status(): ...
|
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):
|
def test_create_invoice_bank_slip(monkeypatch):
|
||||||
monkeypatch.setattr(
|
monkeypatch.setattr(
|
||||||
requests,
|
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):
|
def test_payment_token(monkeypatch):
|
||||||
monkeypatch.setattr(
|
monkeypatch.setattr(
|
||||||
requests,
|
requests,
|
||||||
|
|||||||
Reference in New Issue
Block a user