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

@@ -4,9 +4,12 @@ from aws_lambda_powertools.utilities.data_classes import (
event_source,
)
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dateutils import now
from layercake.dynamodb import (
DynamoDBPersistenceLayer,
KeyPair,
)
from layercake.extra_types import CreditCard
from boto3clients import dynamodb_client
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)
@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']

View File

@@ -4,9 +4,10 @@ from aws_lambda_powertools.utilities.data_classes import (
event_source,
)
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dateutils import now
from layercake.dateutils import now, ttl
from layercake.dynamodb import (
DynamoDBPersistenceLayer,
KeyPair,
SortKey,
TransactKey,
)
@@ -41,7 +42,8 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
r = dyn.collection.get_items(
TransactKey(order_id)
+ 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,
)
@@ -59,21 +61,40 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
)
try:
dyn.put_item(
item={
'id': order_id,
'sk': 'INVOICE',
'payment_method': payment_method,
'secure_id': invoice['secure_id'],
'secure_url': invoice['secure_url'],
'created_at': now_,
# Uncomment this when adding for multiple payment providers
# 'payment_provider': 'iugu',
}
| ({'bank_slip': invoice['bank_slip']} if is_bank_slip else {})
| ({'pix': invoice['pix']} if is_pix else {}),
cond_expr='attribute_not_exists(sk)',
)
with dyn.transact_writer() as transact:
transact.put(
item={
'id': order_id,
'sk': 'INVOICE',
'payment_method': payment_method,
'secure_id': invoice['secure_id'],
'secure_url': invoice['secure_url'],
'created_at': now_,
# Uncomment this when adding for multiple payment providers
# 'payment_provider': 'iugu',
}
| ({'bank_slip': invoice['bank_slip']} if is_bank_slip else {})
| ({'pix': invoice['pix']} if is_pix else {}),
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:
pass