119 lines
3.8 KiB
Python
119 lines
3.8 KiB
Python
from aws_lambda_powertools import Logger
|
|
from aws_lambda_powertools.utilities.data_classes import (
|
|
EventBridgeEvent,
|
|
event_source,
|
|
)
|
|
from aws_lambda_powertools.utilities.typing import LambdaContext
|
|
from layercake.dateutils import now, ttl
|
|
from layercake.dynamodb import (
|
|
DynamoDBPersistenceLayer,
|
|
KeyPair,
|
|
SortKey,
|
|
TransactKey,
|
|
)
|
|
|
|
from boto3clients import dynamodb_client
|
|
from config import (
|
|
IUGU_ACCOUNT_ID,
|
|
IUGU_API_TOKEN,
|
|
IUGU_POSTBACK_URL,
|
|
IUGU_TEST_MODE,
|
|
ORDER_TABLE,
|
|
)
|
|
from iugu import Credentials, Iugu, Order
|
|
|
|
logger = Logger(__name__)
|
|
dyn = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
|
|
|
|
|
|
@event_source(data_class=EventBridgeEvent)
|
|
@logger.inject_lambda_context
|
|
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|
new_image = event.detail['new_image']
|
|
now_ = now()
|
|
order_id = new_image['id']
|
|
r = dyn.collection.get_items(
|
|
TransactKey(order_id)
|
|
+ SortKey('ADDRESS', rename_key='address')
|
|
+ SortKey('ITEMS', path_spec='items', rename_key='items')
|
|
+ SortKey('CREDIT_CARD#PAYMENT_INTENT', rename_key='credit_card')
|
|
+ SortKey(
|
|
'METADATA#TEST_MODE',
|
|
rename_key='iugu_api_token',
|
|
path_spec='iugu_api_token',
|
|
),
|
|
flatten_top=False,
|
|
)
|
|
|
|
api_token = r.get('iugu_api_token')
|
|
test_mode = (api_token is not None) or IUGU_TEST_MODE
|
|
credentials = Credentials(
|
|
IUGU_ACCOUNT_ID,
|
|
# Note: `api_token` can be set from the database
|
|
api_token or IUGU_API_TOKEN,
|
|
test_mode=test_mode,
|
|
)
|
|
|
|
payment_method = new_image['payment_method']
|
|
is_pix = payment_method == 'PIX'
|
|
is_bank_slip = payment_method == 'BANK_SLIP'
|
|
|
|
iugu = Iugu(credentials)
|
|
invoice = iugu.create_invoice(
|
|
order=Order(
|
|
address=r.get('address', {}),
|
|
items=r.get('items', []),
|
|
**new_image,
|
|
),
|
|
postback_url=f'{IUGU_POSTBACK_URL}/{order_id}/postback',
|
|
)
|
|
|
|
try:
|
|
with dyn.transact_writer() as transact:
|
|
transact.put(
|
|
item={
|
|
'id': order_id,
|
|
'sk': 'INVOICE',
|
|
'payment_method': payment_method,
|
|
'invoice_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.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)',
|
|
)
|
|
transact.delete(
|
|
key=KeyPair(order_id, 'CREDIT_CARD#PAYMENT_INTENT'),
|
|
)
|
|
|
|
if test_mode:
|
|
transact.put(
|
|
item={
|
|
'id': order_id,
|
|
'sk': 'SCHEDULE#SELF_DESTRUCTION',
|
|
'ttl': ttl(start_dt=now_, days=14),
|
|
'created_at': now_,
|
|
}
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
return True
|