Split cart into 4 microservices: relations, likes, orders, page-config→blog
Some checks failed
Build and Deploy / build-and-deploy (push) Has been cancelled
Some checks failed
Build and Deploy / build-and-deploy (push) Has been cancelled
Phase 1 - Relations service (internal): owns ContainerRelation, exposes get-children data + attach/detach-child actions. Retargeted events, blog, market callers from cart to relations. Phase 2 - Likes service (internal): unified Like model replaces ProductLike and PostLike with generic target_type/target_slug/target_id. Exposes is-liked, liked-slugs, liked-ids data + toggle action. Phase 3 - PageConfig → blog: moved ownership to blog with direct DB queries, removed proxy endpoints from cart. Phase 4 - Orders service (public): owns Order/OrderItem + SumUp checkout flow. Cart checkout now delegates to orders via create-order action. Webhook/return routes and reconciliation moved to orders. Phase 5 - Infrastructure: docker-compose, deploy.sh, Dockerfiles updated for all 3 new services. Added orders_url helper and factory model imports. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
# bp/cart/global_routes.py — Global cart routes (webhook, return, add)
|
||||
# bp/cart/global_routes.py — Global cart routes (add, quantity, delete, checkout)
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -6,7 +6,6 @@ from quart import Blueprint, g, request, render_template, redirect, url_for, mak
|
||||
from sqlalchemy import select
|
||||
|
||||
from shared.models.market import CartItem
|
||||
from shared.models.order import Order
|
||||
from shared.infrastructure.actions import call_action
|
||||
from .services import (
|
||||
current_cart_identity,
|
||||
@@ -16,20 +15,11 @@ from .services import (
|
||||
calendar_total,
|
||||
get_ticket_cart_entries,
|
||||
ticket_total,
|
||||
check_sumup_status,
|
||||
)
|
||||
from .services.checkout import (
|
||||
find_or_create_cart_item,
|
||||
create_order_from_cart,
|
||||
resolve_page_config,
|
||||
build_sumup_description,
|
||||
build_sumup_reference,
|
||||
build_webhook_url,
|
||||
validate_webhook_secret,
|
||||
get_order_with_details,
|
||||
)
|
||||
from shared.browser.app.payments.sumup import create_checkout as sumup_create_checkout
|
||||
from shared.browser.app.csrf import csrf_exempt
|
||||
|
||||
|
||||
def register(url_prefix: str) -> Blueprint:
|
||||
@@ -141,7 +131,7 @@ def register(url_prefix: str) -> Blueprint:
|
||||
|
||||
@bp.post("/checkout/")
|
||||
async def checkout():
|
||||
"""Legacy global checkout (for orphan items without page scope)."""
|
||||
"""Global checkout — delegates order creation to orders service."""
|
||||
cart = await get_cart(g.s)
|
||||
calendar_entries = await get_calendar_cart_entries(g.s)
|
||||
tickets = await get_ticket_cart_entries(g.s)
|
||||
@@ -168,151 +158,63 @@ def register(url_prefix: str) -> Blueprint:
|
||||
return await make_response(html, 400)
|
||||
|
||||
ident = current_cart_identity()
|
||||
order = await create_order_from_cart(
|
||||
g.s,
|
||||
cart,
|
||||
calendar_entries,
|
||||
ident.get("user_id"),
|
||||
ident.get("session_id"),
|
||||
product_total,
|
||||
calendar_amount,
|
||||
ticket_total=ticket_amount,
|
||||
)
|
||||
|
||||
# Serialize cart items for the orders service
|
||||
cart_items_data = []
|
||||
for ci in cart:
|
||||
cart_items_data.append({
|
||||
"product_id": ci.product_id,
|
||||
"product_title": ci.product_title,
|
||||
"product_slug": ci.product_slug,
|
||||
"product_image": ci.product_image,
|
||||
"product_regular_price": float(ci.product_regular_price) if ci.product_regular_price else None,
|
||||
"product_special_price": float(ci.product_special_price) if ci.product_special_price else None,
|
||||
"product_price_currency": ci.product_price_currency,
|
||||
"quantity": ci.quantity,
|
||||
})
|
||||
|
||||
# Serialize calendar entries and tickets
|
||||
cal_data = []
|
||||
for e in calendar_entries:
|
||||
cal_data.append({
|
||||
"id": e.id,
|
||||
"calendar_container_id": getattr(e, "calendar_container_id", None),
|
||||
})
|
||||
ticket_data = []
|
||||
for t in tickets:
|
||||
ticket_data.append({
|
||||
"id": t.id,
|
||||
"calendar_container_id": getattr(t, "calendar_container_id", None),
|
||||
})
|
||||
|
||||
page_post_id = None
|
||||
if page_config:
|
||||
order.page_config_id = page_config.id
|
||||
page_post_id = getattr(page_config, "container_id", None)
|
||||
|
||||
redirect_url = url_for("cart_global.checkout_return", order_id=order.id, _external=True)
|
||||
order.sumup_reference = build_sumup_reference(order.id, page_config=page_config)
|
||||
description = build_sumup_description(cart, order.id, ticket_count=len(tickets))
|
||||
result = await call_action("orders", "create-order", payload={
|
||||
"cart_items": cart_items_data,
|
||||
"calendar_entries": cal_data,
|
||||
"tickets": ticket_data,
|
||||
"user_id": ident.get("user_id"),
|
||||
"session_id": ident.get("session_id"),
|
||||
"product_total": float(product_total),
|
||||
"calendar_total": float(calendar_amount),
|
||||
"ticket_total": float(ticket_amount),
|
||||
"page_post_id": page_post_id,
|
||||
})
|
||||
|
||||
webhook_base_url = url_for("cart_global.checkout_webhook", order_id=order.id, _external=True)
|
||||
webhook_url = build_webhook_url(webhook_base_url)
|
||||
|
||||
checkout_data = await sumup_create_checkout(
|
||||
order,
|
||||
redirect_url=redirect_url,
|
||||
webhook_url=webhook_url,
|
||||
description=description,
|
||||
page_config=page_config,
|
||||
)
|
||||
order.sumup_checkout_id = checkout_data.get("id")
|
||||
order.sumup_status = checkout_data.get("status")
|
||||
order.description = checkout_data.get("description")
|
||||
|
||||
hosted_cfg = checkout_data.get("hosted_checkout") or {}
|
||||
hosted_url = hosted_cfg.get("hosted_checkout_url") or checkout_data.get("hosted_checkout_url")
|
||||
order.sumup_hosted_url = hosted_url
|
||||
|
||||
await g.s.flush()
|
||||
# Update redirect/webhook URLs with real order_id
|
||||
order_id = result["order_id"]
|
||||
hosted_url = result.get("sumup_hosted_url")
|
||||
|
||||
if not hosted_url:
|
||||
html = await render_template(
|
||||
"_types/cart/checkout_error.html",
|
||||
order=order,
|
||||
order=None,
|
||||
error="No hosted checkout URL returned from SumUp.",
|
||||
)
|
||||
return await make_response(html, 500)
|
||||
|
||||
return redirect(hosted_url)
|
||||
|
||||
@csrf_exempt
|
||||
@bp.post("/checkout/webhook/<int:order_id>/")
|
||||
async def checkout_webhook(order_id: int):
|
||||
"""Webhook endpoint for SumUp CHECKOUT_STATUS_CHANGED events."""
|
||||
if not validate_webhook_secret(request.args.get("token")):
|
||||
return "", 204
|
||||
|
||||
try:
|
||||
payload = await request.get_json()
|
||||
except Exception:
|
||||
payload = None
|
||||
|
||||
if not isinstance(payload, dict):
|
||||
return "", 204
|
||||
|
||||
if payload.get("event_type") != "CHECKOUT_STATUS_CHANGED":
|
||||
return "", 204
|
||||
|
||||
checkout_id = payload.get("id")
|
||||
if not checkout_id:
|
||||
return "", 204
|
||||
|
||||
result = await g.s.execute(select(Order).where(Order.id == order_id))
|
||||
order = result.scalar_one_or_none()
|
||||
if not order:
|
||||
return "", 204
|
||||
|
||||
if order.sumup_checkout_id and order.sumup_checkout_id != checkout_id:
|
||||
return "", 204
|
||||
|
||||
try:
|
||||
await check_sumup_status(g.s, order)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return "", 204
|
||||
|
||||
@bp.get("/checkout/return/<int:order_id>/")
|
||||
async def checkout_return(order_id: int):
|
||||
"""Handle the browser returning from SumUp after payment."""
|
||||
order = await get_order_with_details(g.s, order_id)
|
||||
|
||||
if not order:
|
||||
html = await render_template(
|
||||
"_types/cart/checkout_return.html",
|
||||
order=None,
|
||||
status="missing",
|
||||
calendar_entries=[],
|
||||
)
|
||||
return await make_response(html)
|
||||
|
||||
# Resolve page/market slugs so product links render correctly
|
||||
if order.page_config_id:
|
||||
from shared.infrastructure.data_client import fetch_data
|
||||
from shared.contracts.dtos import CalendarEntryDTO, TicketDTO, dto_from_dict
|
||||
raw_pc = await fetch_data("cart", "page-config-by-id",
|
||||
params={"id": order.page_config_id},
|
||||
required=False)
|
||||
post = await fetch_data("blog", "post-by-id",
|
||||
params={"id": raw_pc["container_id"]},
|
||||
required=False) if raw_pc else None
|
||||
if post:
|
||||
g.page_slug = post["slug"]
|
||||
# Fetch marketplace slug from market service
|
||||
mps = await fetch_data(
|
||||
"market", "marketplaces-for-container",
|
||||
params={"type": "page", "id": post["id"]},
|
||||
required=False,
|
||||
) or []
|
||||
if mps:
|
||||
g.market_slug = mps[0].get("slug")
|
||||
|
||||
if order.sumup_checkout_id:
|
||||
try:
|
||||
await check_sumup_status(g.s, order)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
status = (order.status or "pending").lower()
|
||||
|
||||
from shared.infrastructure.data_client import fetch_data
|
||||
from shared.contracts.dtos import CalendarEntryDTO, TicketDTO, dto_from_dict
|
||||
raw_entries = await fetch_data("events", "entries-for-order",
|
||||
params={"order_id": order.id}, required=False) or []
|
||||
calendar_entries = [dto_from_dict(CalendarEntryDTO, e) for e in raw_entries]
|
||||
raw_tickets = await fetch_data("events", "tickets-for-order",
|
||||
params={"order_id": order.id}, required=False) or []
|
||||
order_tickets = [dto_from_dict(TicketDTO, t) for t in raw_tickets]
|
||||
await g.s.flush()
|
||||
|
||||
html = await render_template(
|
||||
"_types/cart/checkout_return.html",
|
||||
order=order,
|
||||
status=status,
|
||||
calendar_entries=calendar_entries,
|
||||
order_tickets=order_tickets,
|
||||
)
|
||||
return await make_response(html)
|
||||
|
||||
return bp
|
||||
|
||||
@@ -5,8 +5,7 @@ from __future__ import annotations
|
||||
from quart import Blueprint, g, render_template, redirect, make_response, url_for
|
||||
|
||||
from shared.browser.app.utils.htmx import is_htmx_request
|
||||
from shared.browser.app.payments.sumup import create_checkout as sumup_create_checkout
|
||||
from shared.config import config
|
||||
from shared.infrastructure.actions import call_action
|
||||
from .services import (
|
||||
total,
|
||||
calendar_total,
|
||||
@@ -14,12 +13,6 @@ from .services import (
|
||||
)
|
||||
from .services.page_cart import get_cart_for_page, get_calendar_entries_for_page, get_tickets_for_page
|
||||
from .services.ticket_groups import group_tickets
|
||||
from .services.checkout import (
|
||||
create_order_from_cart,
|
||||
build_sumup_description,
|
||||
build_sumup_reference,
|
||||
build_webhook_url,
|
||||
)
|
||||
from .services import current_cart_identity
|
||||
|
||||
|
||||
@@ -56,7 +49,6 @@ def register(url_prefix: str) -> Blueprint:
|
||||
@bp.post("/checkout/")
|
||||
async def page_checkout():
|
||||
post = g.page_post
|
||||
page_config = getattr(g, "page_config", None)
|
||||
|
||||
cart = await get_cart_for_page(g.s, post.id)
|
||||
cal_entries = await get_calendar_entries_for_page(g.s, post.id)
|
||||
@@ -65,61 +57,51 @@ def register(url_prefix: str) -> Blueprint:
|
||||
if not cart and not cal_entries and not page_tickets:
|
||||
return redirect(url_for("page_cart.page_view"))
|
||||
|
||||
product_total = total(cart) or 0
|
||||
product_total_val = total(cart) or 0
|
||||
calendar_amount = calendar_total(cal_entries) or 0
|
||||
ticket_amount = ticket_total(page_tickets) or 0
|
||||
cart_total = product_total + calendar_amount + ticket_amount
|
||||
cart_total = product_total_val + calendar_amount + ticket_amount
|
||||
|
||||
if cart_total <= 0:
|
||||
return redirect(url_for("page_cart.page_view"))
|
||||
|
||||
# Create order scoped to this page
|
||||
ident = current_cart_identity()
|
||||
order = await create_order_from_cart(
|
||||
g.s,
|
||||
cart,
|
||||
cal_entries,
|
||||
ident.get("user_id"),
|
||||
ident.get("session_id"),
|
||||
product_total,
|
||||
calendar_amount,
|
||||
ticket_total=ticket_amount,
|
||||
page_post_id=post.id,
|
||||
)
|
||||
|
||||
# Set page_config on order
|
||||
if page_config:
|
||||
order.page_config_id = page_config.id
|
||||
# Serialize cart items for the orders service
|
||||
cart_items_data = []
|
||||
for ci in cart:
|
||||
cart_items_data.append({
|
||||
"product_id": ci.product_id,
|
||||
"product_title": ci.product_title,
|
||||
"product_slug": ci.product_slug,
|
||||
"product_image": ci.product_image,
|
||||
"product_regular_price": float(ci.product_regular_price) if ci.product_regular_price else None,
|
||||
"product_special_price": float(ci.product_special_price) if ci.product_special_price else None,
|
||||
"product_price_currency": ci.product_price_currency,
|
||||
"quantity": ci.quantity,
|
||||
})
|
||||
|
||||
# Build SumUp checkout details — webhook/return use global routes
|
||||
redirect_url = url_for("cart_global.checkout_return", order_id=order.id, _external=True)
|
||||
order.sumup_reference = build_sumup_reference(order.id, page_config=page_config)
|
||||
description = build_sumup_description(cart, order.id, ticket_count=len(page_tickets))
|
||||
cal_data = [{"id": e.id, "calendar_container_id": getattr(e, "calendar_container_id", None)} for e in cal_entries]
|
||||
ticket_data = [{"id": t.id, "calendar_container_id": getattr(t, "calendar_container_id", None)} for t in page_tickets]
|
||||
|
||||
webhook_base_url = url_for("cart_global.checkout_webhook", order_id=order.id, _external=True)
|
||||
webhook_url = build_webhook_url(webhook_base_url)
|
||||
result = await call_action("orders", "create-order", payload={
|
||||
"cart_items": cart_items_data,
|
||||
"calendar_entries": cal_data,
|
||||
"tickets": ticket_data,
|
||||
"user_id": ident.get("user_id"),
|
||||
"session_id": ident.get("session_id"),
|
||||
"product_total": float(product_total_val),
|
||||
"calendar_total": float(calendar_amount),
|
||||
"ticket_total": float(ticket_amount),
|
||||
"page_post_id": post.id,
|
||||
})
|
||||
|
||||
checkout_data = await sumup_create_checkout(
|
||||
order,
|
||||
redirect_url=redirect_url,
|
||||
webhook_url=webhook_url,
|
||||
description=description,
|
||||
page_config=page_config,
|
||||
)
|
||||
order.sumup_checkout_id = checkout_data.get("id")
|
||||
order.sumup_status = checkout_data.get("status")
|
||||
order.description = checkout_data.get("description")
|
||||
|
||||
hosted_cfg = checkout_data.get("hosted_checkout") or {}
|
||||
hosted_url = hosted_cfg.get("hosted_checkout_url") or checkout_data.get("hosted_checkout_url")
|
||||
order.sumup_hosted_url = hosted_url
|
||||
|
||||
await g.s.flush()
|
||||
hosted_url = result.get("sumup_hosted_url")
|
||||
|
||||
if not hosted_url:
|
||||
html = await render_template(
|
||||
"_types/cart/checkout_error.html",
|
||||
order=order,
|
||||
order=None,
|
||||
error="No hosted checkout URL returned from SumUp.",
|
||||
)
|
||||
return await make_response(html, 500)
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
from .get_cart import get_cart
|
||||
from .identity import current_cart_identity
|
||||
from .total import total
|
||||
from .clear_cart_for_order import clear_cart_for_order
|
||||
from .calendar_cart import get_calendar_cart_entries, calendar_total, get_ticket_cart_entries, ticket_total
|
||||
from .check_sumup_status import check_sumup_status
|
||||
from .page_cart import (
|
||||
get_cart_for_page,
|
||||
get_calendar_entries_for_page,
|
||||
|
||||
@@ -178,7 +178,7 @@ async def get_cart_grouped_by_page(session) -> list[dict]:
|
||||
p = dto_from_dict(PostDTO, raw_p)
|
||||
posts_by_id[p.id] = p
|
||||
|
||||
raw_pcs = await fetch_data("cart", "page-configs-batch",
|
||||
raw_pcs = await fetch_data("blog", "page-configs-batch",
|
||||
params={"container_type": "page",
|
||||
"ids": ",".join(str(i) for i in post_ids)},
|
||||
required=False) or []
|
||||
|
||||
Reference in New Issue
Block a user