feat: add tests command
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
from __future__ import annotations
|
||||
from decimal import Decimal
|
||||
from ulid import ULID
|
||||
from src.application.abstractions import IUnitOfWork
|
||||
from src.application.contracts import IReceipt
|
||||
from src.application.domain.exceptions import ApplicationException
|
||||
from src.infrastructure.database.decorators import transactional
|
||||
from src.presentation.schemas.itpay_payment_models import ItpayPaymentData
|
||||
|
||||
|
||||
class CreatePaymentCloudkassirCommand:
|
||||
def __init__(self, *, unit_of_work: IUnitOfWork, receipt: IReceipt):
|
||||
self._unit_of_work = unit_of_work
|
||||
self._receipt = receipt
|
||||
|
||||
|
||||
@transactional
|
||||
async def __call__(self, payment: ItpayPaymentData) -> None:
|
||||
if str(payment.status).strip().lower() != 'completed':
|
||||
return
|
||||
metadata = payment.metadata or {}
|
||||
order_id = str(metadata.get('order_id') or '')
|
||||
user_id = str(metadata.get('user_id') or '')
|
||||
if not order_id:
|
||||
raise ApplicationException(status_code=400, message='Itpay webhook metadata missing order_id')
|
||||
if not user_id:
|
||||
raise ApplicationException(status_code=400, message='Itpay webhook metadata missing user_id')
|
||||
await self._unit_of_work.payment_repository.create_completed(
|
||||
user_id=user_id,
|
||||
order_id=order_id,
|
||||
itpay_payment_id=str(payment.id),
|
||||
itpay_paid_amount=str(payment.amount) if payment.amount is not None else None,
|
||||
transaction_id=str(payment.transaction_id) if payment.transaction_id is not None else None,
|
||||
paid_at=str(payment.paid) if payment.paid is not None else None,
|
||||
expired_date=str(payment.expired_date) if payment.expired_date is not None else None,
|
||||
)
|
||||
user = await self._unit_of_work.user_repository.get(user_id)
|
||||
if user is None:
|
||||
raise ApplicationException(status_code=404, message='User not found')
|
||||
email = str(user.email or '').strip()
|
||||
if not email:
|
||||
raise ApplicationException(status_code=400, message='User email missing')
|
||||
phone_raw = (user.phone or '').strip()
|
||||
phone = phone_raw if phone_raw else None
|
||||
total_amount: Decimal | None = None
|
||||
if payment.amount is not None:
|
||||
total_amount = Decimal(str(payment.amount)).quantize(Decimal('0.01'))
|
||||
if total_amount is None:
|
||||
raw_amt = metadata.get('amount')
|
||||
if raw_amt is not None:
|
||||
total_amount = Decimal(str(raw_amt)).quantize(Decimal('0.01'))
|
||||
if total_amount is None:
|
||||
raise ApplicationException(status_code=400, message='Itpay webhook payment amount missing for receipt')
|
||||
raw_sf = metadata.get('service_fee')
|
||||
if raw_sf is None:
|
||||
raise ApplicationException(status_code=400, message='Itpay webhook metadata missing service_fee for receipt')
|
||||
service_fee = Decimal(str(raw_sf)).quantize(Decimal('0.01'))
|
||||
principal_amount = (total_amount - service_fee).quantize(Decimal('0.01'))
|
||||
if principal_amount < 0:
|
||||
raise ApplicationException(status_code=400, message='Invalid receipt amounts: principal negative')
|
||||
await self._receipt.create_receipt(
|
||||
order_id=order_id,
|
||||
user_id=user_id,
|
||||
email=email,
|
||||
total_amount=total_amount,
|
||||
principal_amount=principal_amount,
|
||||
service_fee=service_fee,
|
||||
phone=phone,
|
||||
request_id=str(ULID()),
|
||||
)
|
||||
Reference in New Issue
Block a user