add support to milti transaction
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from enum import Enum
|
||||
from http import HTTPStatus
|
||||
from typing import Any
|
||||
from urllib.parse import parse_qsl
|
||||
@@ -7,6 +8,7 @@ from aws_lambda_powertools.event_handler.api_gateway import (
|
||||
APIGatewayHttpResolver,
|
||||
Response,
|
||||
)
|
||||
from aws_lambda_powertools.event_handler.exceptions import NotFoundError
|
||||
from aws_lambda_powertools.logging import correlation_paths
|
||||
from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||
from layercake.dateutils import now
|
||||
@@ -21,35 +23,73 @@ app = APIGatewayHttpResolver(enable_validation=True)
|
||||
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')
|
||||
@tracer.capture_method
|
||||
def postback(order_id: str):
|
||||
decoded_body = dict(parse_qsl(app.current_event.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)
|
||||
|
||||
try:
|
||||
dyn.update_item(
|
||||
with dyn.transact_writer() as transact:
|
||||
transact.update(
|
||||
key=KeyPair(order_id, '0'),
|
||||
update_expr='SET #status = :status, \
|
||||
#status_attr = :now, \
|
||||
updated_at = :now',
|
||||
cond_expr='attribute_exists(sk)',
|
||||
expr_attr_names={
|
||||
'#status': 'status',
|
||||
'#status_attr': status_attr.value,
|
||||
},
|
||||
expr_attr_values={
|
||||
':status': status,
|
||||
':now': now(),
|
||||
':now': now_,
|
||||
},
|
||||
exc_cls=OrderNotFoundError,
|
||||
)
|
||||
except Exception:
|
||||
return Response(status_code=HTTPStatus.NOT_FOUND)
|
||||
else:
|
||||
return Response(status_code=HTTPStatus.NO_CONTENT)
|
||||
|
||||
if status == 'EXTERNALLY_PAID':
|
||||
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)
|
||||
|
||||
|
||||
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_HTTP)
|
||||
|
||||
@@ -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']
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -71,6 +71,7 @@ class Order(BaseModel):
|
||||
address: Address
|
||||
items: tuple[Item, ...]
|
||||
payment_method: PaymentMethod
|
||||
discount: Decimal = Decimal('0')
|
||||
cpf: str | None = None
|
||||
cnpj: str | None = None
|
||||
|
||||
@@ -127,6 +128,7 @@ class Iugu:
|
||||
}
|
||||
for item in order.items
|
||||
]
|
||||
|
||||
payload = {
|
||||
'order_id': order.id,
|
||||
'external_reference': order.id,
|
||||
@@ -135,6 +137,7 @@ class Iugu:
|
||||
'email': order.email,
|
||||
'payable_with': order.payment_method.lower(),
|
||||
'notification_url': postback_url.geturl(),
|
||||
'discount_cents': int(order.discount * -100),
|
||||
'payer': {
|
||||
'name': order.name,
|
||||
'email': order.email,
|
||||
|
||||
Reference in New Issue
Block a user