add test mode to checkout

This commit is contained in:
2026-01-20 10:41:32 -03:00
parent fde5c31ffc
commit 49e0c3333b
13 changed files with 847 additions and 493 deletions

View File

@@ -47,6 +47,12 @@ def _status_attr(status: str) -> StatusAttr | None:
return None
def _friendly_status(s: str) -> str:
if 'status' == 'EXTERNALLY_PAID':
return 'PAID'
return s
@app.post('/<order_id>/postback')
@tracer.capture_method
def postback(order_id: str):
@@ -73,7 +79,7 @@ def postback(order_id: str):
'#status_attr': status_attr.value,
},
expr_attr_values={
':status': status,
':status': _friendly_status(status),
':now': now_,
},
exc_cls=OrderNotFoundError,

View File

@@ -7,8 +7,7 @@ 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 = True
IUGU_TEST_MODE: bool = os.getenv('AWS_LAMBDA_FUNCTION_NAME') is None
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

@@ -8,6 +8,7 @@ from layercake.dateutils import now
from layercake.dynamodb import (
DynamoDBPersistenceLayer,
KeyPair,
SortKey,
)
from layercake.extra_types import CreditCard
@@ -17,13 +18,6 @@ from iugu import Credentials, Iugu
logger = Logger(__name__)
dyn = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
iugu = Iugu(
Credentials(
IUGU_ACCOUNT_ID,
IUGU_API_TOKEN,
test_mode=IUGU_TEST_MODE,
)
)
@event_source(data_class=EventBridgeEvent)
@@ -36,6 +30,26 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
credit_card = CreditCard(**new_image['credit_card'])
now_ = now()
api_token = dyn.collection.get_item(
KeyPair(
pk=order_id,
sk=SortKey(
'METADATA#TEST_MODE',
path_spec='iugu_api_token',
),
),
default=None,
raise_on_error=False,
)
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,
)
iugu = Iugu(credentials)
token = iugu.payment_token(credit_card)
charge = iugu.charge(
invoice_id=invoice_id,

View File

@@ -24,13 +24,6 @@ from iugu import Credentials, Iugu, Order
logger = Logger(__name__)
dyn = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
iugu = Iugu(
Credentials(
IUGU_ACCOUNT_ID,
IUGU_API_TOKEN,
test_mode=IUGU_TEST_MODE,
)
)
@event_source(data_class=EventBridgeEvent)
@@ -43,14 +36,29 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
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('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', {}),
@@ -95,6 +103,15 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
cond_expr='attribute_not_exists(sk)',
)
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

View File

@@ -237,6 +237,7 @@ class Iugu:
)
r.raise_for_status()
except requests.HTTPError as err:
logger.info('Response', err.response)
logger.exception(err)
raise
else: