add test mode to checkout
This commit is contained in:
@@ -4,7 +4,7 @@ from decimal import Decimal
|
||||
from enum import Enum
|
||||
from functools import reduce
|
||||
from http import HTTPStatus
|
||||
from typing import Any, Literal
|
||||
from typing import Any, Literal, NotRequired, TypedDict, cast
|
||||
from uuid import uuid4
|
||||
|
||||
from aws_lambda_powertools import Logger
|
||||
@@ -13,7 +13,7 @@ from aws_lambda_powertools.event_handler.exceptions import (
|
||||
NotFoundError,
|
||||
)
|
||||
from layercake.dateutils import now, ttl
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, SortKey
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, SortKey, TransactKey
|
||||
from layercake.extra_types import CnpjStr, CpfStr, CreditCard, NameStr
|
||||
from pydantic import (
|
||||
UUID4,
|
||||
@@ -136,8 +136,8 @@ class Checkout(BaseModel):
|
||||
@router.post('/')
|
||||
def checkout(payload: Checkout):
|
||||
now_ = now()
|
||||
settings = _get_settings(str(payload.org_id or payload.user_id))
|
||||
order_id = payload.id
|
||||
org_id = payload.org_id
|
||||
address = payload.address
|
||||
credit_card = payload.credit_card
|
||||
created_by = payload.created_by
|
||||
@@ -148,7 +148,7 @@ def checkout(payload: Checkout):
|
||||
installments = payload.installments
|
||||
subtotal = _sum_items(items)
|
||||
due_date = (
|
||||
_calc_due_date(now_, _get_due_days(org_id) if org_id else DUE_DAYS)
|
||||
_calc_due_date(now_, settings['due_days'])
|
||||
if payment_method == 'BANK_SLIP'
|
||||
else now_ + timedelta(hours=1)
|
||||
)
|
||||
@@ -267,6 +267,16 @@ def checkout(payload: Checkout):
|
||||
| enrollment.model_dump(exclude={'id'})
|
||||
)
|
||||
|
||||
if 'iugu_api_token' in settings:
|
||||
transact.put(
|
||||
item={
|
||||
'id': order_id,
|
||||
'sk': 'METADATA#TEST_MODE',
|
||||
'iugu_api_token': settings['iugu_api_token'],
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
|
||||
return JSONResponse(
|
||||
body={'id': order_id},
|
||||
status_code=HTTPStatus.CREATED,
|
||||
@@ -319,18 +329,32 @@ def _calc_due_date(
|
||||
return due_date
|
||||
|
||||
|
||||
def _get_due_days(
|
||||
org_id: str | UUID4,
|
||||
default: int = DUE_DAYS,
|
||||
) -> int:
|
||||
return int(
|
||||
dyn.collection.get_item(
|
||||
KeyPair(
|
||||
pk=str(org_id),
|
||||
sk=SortKey('METADATA#BILLING', path_spec='due_days'),
|
||||
table_name=USER_TABLE,
|
||||
),
|
||||
raise_on_error=False,
|
||||
default=default,
|
||||
Settings = TypedDict(
|
||||
'Settings',
|
||||
{
|
||||
'due_days': int,
|
||||
'iugu_api_token': NotRequired[str],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _get_settings(id: str) -> Settings:
|
||||
r = dyn.collection.get_items(
|
||||
TransactKey(id, table_name=USER_TABLE)
|
||||
+ SortKey(
|
||||
sk='METADATA#BILLING',
|
||||
path_spec='due_days',
|
||||
rename_key='due_days',
|
||||
)
|
||||
+ SortKey(
|
||||
'METADATA#TEST_MODE',
|
||||
path_spec='iugu_api_token',
|
||||
rename_key='iugu_api_token',
|
||||
),
|
||||
flatten_top=False,
|
||||
)
|
||||
|
||||
if 'due_days' not in r:
|
||||
r['due_days'] = DUE_DAYS
|
||||
|
||||
return cast(Settings, r)
|
||||
|
||||
@@ -82,7 +82,7 @@ def test_checkout_coupon(
|
||||
'id': '99bb3b60-4ded-4a8e-937c-ba2d78ec6454',
|
||||
'access_period': 365,
|
||||
},
|
||||
'scheduled_for': '2026-01-20',
|
||||
'scheduled_for': '2040-01-20',
|
||||
'id': '1f0931ad-7dd4-4ca1-bce2-a2e89efa5b56',
|
||||
'user': {
|
||||
'name': 'Maitê L Siqueira',
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
// Seeds for Org
|
||||
{"id": "f6000f79-6e5c-49a0-952f-3bda330ef278", "sk": "0", "name": "Banco do Brasil", "cnpj": "00000000000191"}
|
||||
{"id": "f6000f79-6e5c-49a0-952f-3bda330ef278", "sk": "admins#15bacf02-1535-4bee-9022-19d106fd7518", "name": "Chester Bennington", "email": "chester@linkinpark.com"}
|
||||
{"id": "f6000f79-6e5c-49a0-952f-3bda330ef278", "sk": "METADATA#BILLING", "due_days": "23"}
|
||||
{"id": "f6000f79-6e5c-49a0-952f-3bda330ef278", "sk": "METADATA#TEST_MODE", "iugu_api_token": "123"}
|
||||
{"id": "orgmembers#f6000f79-6e5c-49a0-952f-3bda330ef278", "sk": "15bacf02-1535-4bee-9022-19d106fd7518"}
|
||||
|
||||
// Seeds for Org
|
||||
|
||||
Reference in New Issue
Block a user