Add Django backend, 1C integration, deploy config and API docs
Backend (Django 4.2 + DRF): - accounts: phone OTP login, JWT, profile, addresses - catalog: categories, products, images, filters, search, hits/new/related - orders: creation with Idempotency-Key, stock reservation, status history, cancel - loyalty: bonus accounts, promo codes, gift certificates - payments: payment records and YooKassa webhook handling - content: static pages, banners, site settings - integration: 1C exchange (catalog sync, order push via outbox, statuses, bonuses) - common: error envelope, pagination, request-id, integration auth, admin dashboard Infrastructure: Dockerfile, docker-compose (Postgres, Redis, Celery worker/beat, Nginx), requirements split by environment, OpenAPI contracts for site and 1C, architecture doc. Tests: 22 pytest cases covering catalog, orders, idempotency, OTP, integration.
This commit is contained in:
1 parent
5f8d37357d
commit
756f40353b
130 files changed
+10066
No files matched your search
@@ -0,0 +1,94 @@
|
||||
import logging
|
||||
from decimal import Decimal
|
||||
|
||||
from django.db import transaction
|
||||
from django.utils import timezone
|
||||
|
||||
from apps.orders.models import Order
|
||||
|
||||
from .models import Payment
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def create_payment(
|
||||
order: Order,
|
||||
method: str,
|
||||
amount: Decimal,
|
||||
*,
|
||||
provider: str = "yookassa",
|
||||
idempotency_key: str | None = None,
|
||||
) -> Payment:
|
||||
"""Creates a payment intent.
|
||||
|
||||
The provider call is intentionally left as a TODO: the exact ЮKassa
|
||||
integration (payment creation, confirmation URL, SBP QR) is implemented
|
||||
in the payments sprint. The local record is created first so that the
|
||||
order always has an auditable payment trail.
|
||||
"""
|
||||
if idempotency_key:
|
||||
existing = Payment.objects.filter(idempotency_key=idempotency_key).first()
|
||||
if existing is not None:
|
||||
return existing
|
||||
|
||||
payment = Payment.objects.create(
|
||||
order=order,
|
||||
method=method,
|
||||
amount=amount,
|
||||
provider=provider,
|
||||
idempotency_key=idempotency_key,
|
||||
)
|
||||
# TODO: call provider API and store provider_payment_id + confirmation_url.
|
||||
logger.info(
|
||||
"Payment intent created: order=%s method=%s amount=%s",
|
||||
order.number,
|
||||
method,
|
||||
amount,
|
||||
)
|
||||
return payment
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def handle_yookassa_webhook(payload: dict) -> Payment | None:
|
||||
"""Applies a ЮKassa webhook to the local payment/order state."""
|
||||
event = payload.get("event", "")
|
||||
obj = payload.get("object", {}) or {}
|
||||
provider_payment_id = obj.get("id")
|
||||
|
||||
payment = (
|
||||
Payment.objects.select_for_update()
|
||||
.filter(provider_payment_id=provider_payment_id)
|
||||
.first()
|
||||
)
|
||||
if payment is None:
|
||||
logger.warning("Unknown ЮKassa payment id: %s", provider_payment_id)
|
||||
return None
|
||||
|
||||
payment.payload = payload
|
||||
|
||||
if event == "payment.succeeded":
|
||||
payment.status = Payment.Status.SUCCEEDED
|
||||
payment.paid_at = timezone.now()
|
||||
payment.save(update_fields=["status", "paid_at", "payload", "updated_at"])
|
||||
|
||||
order = payment.order
|
||||
paid_total = sum(
|
||||
p.amount
|
||||
for p in order.payments.filter(status=Payment.Status.SUCCEEDED)
|
||||
)
|
||||
if paid_total >= order.total:
|
||||
order.payment_status = Order.PaymentStatus.PAID
|
||||
elif paid_total > 0:
|
||||
order.payment_status = Order.PaymentStatus.PARTIALLY_PAID
|
||||
order.save(update_fields=["payment_status", "updated_at"])
|
||||
|
||||
elif event == "payment.canceled":
|
||||
payment.status = Payment.Status.CANCELLED
|
||||
payment.save(update_fields=["status", "payload", "updated_at"])
|
||||
|
||||
elif event == "refund.succeeded":
|
||||
payment.status = Payment.Status.REFUNDED
|
||||
payment.save(update_fields=["status", "payload", "updated_at"])
|
||||
|
||||
return payment
|
||||
Reference in new issue
Block a user