fix interest
This commit is contained in:
@@ -12,7 +12,7 @@ from aws_lambda_powertools.event_handler.exceptions import (
|
||||
NotFoundError,
|
||||
)
|
||||
from layercake.dateutils import now, ttl
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, SortKey
|
||||
from layercake.extra_types import CnpjStr, CpfStr, CreditCard, NameStr
|
||||
from pydantic import (
|
||||
UUID4,
|
||||
@@ -26,7 +26,7 @@ from pydantic import (
|
||||
|
||||
from api_gateway import JSONResponse
|
||||
from boto3clients import dynamodb_client
|
||||
from config import ORDER_TABLE
|
||||
from config import DUE_DAYS, ORDER_TABLE
|
||||
from routes.enrollments.enroll import Enrollment
|
||||
|
||||
router = Router()
|
||||
@@ -135,24 +135,29 @@ class Checkout(BaseModel):
|
||||
def checkout(payload: Checkout):
|
||||
now_ = now()
|
||||
order_id = payload.id
|
||||
org_id = payload.org_id
|
||||
address = payload.address
|
||||
credit_card = payload.credit_card
|
||||
items = payload.items
|
||||
enrollments = payload.enrollments
|
||||
coupon = payload.coupon
|
||||
subtotal = _sum_items(items)
|
||||
payment_method = payload.payment_method
|
||||
installments = payload.installments
|
||||
subtotal = _sum_items(items)
|
||||
due_date = (
|
||||
_calc_due_date(now_, 3)
|
||||
_calc_due_date(now_, _get_due_days(org_id) if org_id else DUE_DAYS)
|
||||
if payment_method == 'BANK_SLIP'
|
||||
else now_ + timedelta(hours=1)
|
||||
)
|
||||
discount = (
|
||||
_apply_discount(subtotal, coupon.amount, coupon.type) * -1
|
||||
if coupon
|
||||
else Decimal('0')
|
||||
_apply_discount(subtotal, coupon.amount, coupon.type) * -1 if coupon else 0
|
||||
)
|
||||
total = subtotal + discount if subtotal > 0 else 0
|
||||
interest_amount = (
|
||||
_calc_interest(total, installments) - total
|
||||
if payment_method == 'CREDIT_CARD' and installments
|
||||
else 0
|
||||
)
|
||||
total = subtotal + discount if subtotal > Decimal('0') else Decimal('0')
|
||||
|
||||
with dyn.transact_writer() as transact:
|
||||
transact.put(
|
||||
@@ -161,14 +166,21 @@ def checkout(payload: Checkout):
|
||||
'sk': '0',
|
||||
'status': 'PENDING',
|
||||
'subtotal': subtotal,
|
||||
'total': total,
|
||||
'total': total + interest_amount,
|
||||
'discount': discount,
|
||||
'due_date': due_date,
|
||||
# Post-migration (orders): rename `create_date` to `created_at`
|
||||
'create_date': now_,
|
||||
}
|
||||
| ({'coupon': coupon.code} if coupon else {})
|
||||
| ({'installments': payload.installments} if payload.installments else {})
|
||||
| (
|
||||
{
|
||||
'installments': payload.installments,
|
||||
'interest_amount': interest_amount,
|
||||
}
|
||||
if payload.installments
|
||||
else {}
|
||||
)
|
||||
| payload.model_dump()
|
||||
)
|
||||
|
||||
@@ -293,3 +305,17 @@ def _calc_due_date(
|
||||
business_days -= 1
|
||||
|
||||
return current_dt
|
||||
|
||||
|
||||
def _get_due_days(
|
||||
org_id: str | UUID4,
|
||||
default: int = DUE_DAYS,
|
||||
) -> int:
|
||||
return dyn.collection.get_item(
|
||||
KeyPair(
|
||||
pk=str(org_id),
|
||||
sk=SortKey('METADATA#PAYMENT_POLICY', path_spec='due_days'),
|
||||
),
|
||||
raise_on_error=False,
|
||||
default=default,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user