add iugu postback

This commit is contained in:
2026-01-12 23:30:52 -03:00
parent 29b8305713
commit ad23e9aa51
25 changed files with 191 additions and 29 deletions

View File

@@ -1,5 +1,6 @@
from http import HTTPStatus
from typing import Any
from urllib.parse import parse_qsl
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler.api_gateway import (
@@ -8,7 +9,8 @@ from aws_lambda_powertools.event_handler.api_gateway import (
)
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dynamodb import DynamoDBPersistenceLayer
from layercake.dateutils import now
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
from boto3clients import dynamodb_client
from config import ORDER_TABLE
@@ -19,10 +21,35 @@ app = APIGatewayHttpResolver(enable_validation=True)
dyn = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
@app.post('/postback/<order_id>')
@app.post('/<order_id>/postback')
@tracer.capture_method
def postback(order_id: str):
return Response(status_code=HTTPStatus.NO_CONTENT)
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':
return Response(status_code=HTTPStatus.NO_CONTENT)
try:
dyn.update_item(
key=KeyPair(order_id, '0'),
update_expr='SET #status = :status, \
updated_at = :now',
cond_expr='attribute_exists(sk)',
expr_attr_names={
'#status': 'status',
},
expr_attr_values={
':status': status,
':now': now(),
},
)
except Exception:
return Response(status_code=HTTPStatus.NOT_FOUND)
else:
return Response(status_code=HTTPStatus.NO_CONTENT)
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_HTTP)

View File

@@ -7,7 +7,8 @@ ENROLLMENT_TABLE: str = os.getenv('ENROLLMENT_TABLE') # type: ignore
IUGU_ACCOUNT_ID: str = 'AF01CF1B3451459F92666F10589278EE'
IUGU_API_TOKEN: str = os.getenv('IUGU_API_TOKEN') # type: ignore
IUGU_TEST_MODE: bool = os.getenv('AWS_LAMBDA_FUNCTION_NAME') is None
# IUGU_TEST_MODE: bool = os.getenv('AWS_LAMBDA_FUNCTION_NAME') is None
IUGU_TEST_MODE: bool = True
IUGU_POSTBACK_URL = 'https://zjg09ppxq8.execute-api.sa-east-1.amazonaws.com'
HTTP_CONNECT_TIMEOUT = int(os.environ.get('HTTP_CONNECT_TIMEOUT', 1))

View File

@@ -55,7 +55,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
items=r.get('items', []),
**new_image,
),
postback_url=f'{IUGU_POSTBACK_URL}/postback/{order_id}',
postback_url=f'{IUGU_POSTBACK_URL}/{order_id}/postback',
)
try: