feat: add platform marketing homepage with signup flow
Implement complete marketing homepage for Wizamart targeting Letzshop vendors in Luxembourg. Includes: - Marketing homepage with hero, pricing tiers, and add-ons - 4-step signup wizard with Stripe card collection (30-day trial) - Letzshop vendor lookup for shop claiming - Platform API endpoints for pricing, vendors, and signup - Stripe SetupIntent integration for trial with card upfront - Database fields for Letzshop vendor identity tracking 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,7 +10,7 @@ This module provides:
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.api.v1 import admin, shop, vendor
|
||||
from app.api.v1 import admin, platform, shop, vendor
|
||||
from app.api.v1.shared import language, webhooks
|
||||
|
||||
api_router = APIRouter()
|
||||
@@ -36,6 +36,13 @@ api_router.include_router(vendor.router, prefix="/v1/vendor", tags=["vendor"])
|
||||
|
||||
api_router.include_router(shop.router, prefix="/v1/shop", tags=["shop"])
|
||||
|
||||
# ============================================================================
|
||||
# PLATFORM ROUTES (Public marketing and signup)
|
||||
# Prefix: /api/v1/platform
|
||||
# ============================================================================
|
||||
|
||||
api_router.include_router(platform.router, prefix="/v1/platform", tags=["platform"])
|
||||
|
||||
# ============================================================================
|
||||
# SHARED ROUTES (Cross-context utilities)
|
||||
# Prefix: /api/v1
|
||||
|
||||
22
app/api/v1/platform/__init__.py
Normal file
22
app/api/v1/platform/__init__.py
Normal file
@@ -0,0 +1,22 @@
|
||||
# app/api/v1/platform/__init__.py
|
||||
"""
|
||||
Platform public API endpoints.
|
||||
|
||||
These endpoints are publicly accessible (no authentication required)
|
||||
and serve the marketing homepage, pricing pages, and signup flows.
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.api.v1.platform import pricing, letzshop_vendors, signup
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# Public pricing and tier info
|
||||
router.include_router(pricing.router, tags=["platform-pricing"])
|
||||
|
||||
# Letzshop vendor lookup
|
||||
router.include_router(letzshop_vendors.router, tags=["platform-vendors"])
|
||||
|
||||
# Signup flow
|
||||
router.include_router(signup.router, tags=["platform-signup"])
|
||||
222
app/api/v1/platform/letzshop_vendors.py
Normal file
222
app/api/v1/platform/letzshop_vendors.py
Normal file
@@ -0,0 +1,222 @@
|
||||
# app/api/v1/platform/letzshop_vendors.py
|
||||
"""
|
||||
Letzshop vendor lookup API endpoints.
|
||||
|
||||
Allows potential vendors to find themselves in the Letzshop marketplace
|
||||
and claim their shop during signup.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import re
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from pydantic import BaseModel, HttpUrl
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from models.database.vendor import Vendor
|
||||
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Response Schemas
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class LetzshopVendorInfo(BaseModel):
|
||||
"""Letzshop vendor information for display."""
|
||||
|
||||
letzshop_id: str | None = None
|
||||
slug: str
|
||||
name: str
|
||||
description: str | None = None
|
||||
logo_url: str | None = None
|
||||
category: str | None = None
|
||||
city: str | None = None
|
||||
letzshop_url: str
|
||||
is_claimed: bool = False
|
||||
|
||||
|
||||
class LetzshopVendorListResponse(BaseModel):
|
||||
"""Paginated list of Letzshop vendors."""
|
||||
|
||||
vendors: list[LetzshopVendorInfo]
|
||||
total: int
|
||||
page: int
|
||||
limit: int
|
||||
has_more: bool
|
||||
|
||||
|
||||
class LetzshopLookupRequest(BaseModel):
|
||||
"""Request to lookup a Letzshop vendor by URL."""
|
||||
|
||||
url: str # e.g., https://letzshop.lu/vendors/my-shop or just "my-shop"
|
||||
|
||||
|
||||
class LetzshopLookupResponse(BaseModel):
|
||||
"""Response from Letzshop vendor lookup."""
|
||||
|
||||
found: bool
|
||||
vendor: LetzshopVendorInfo | None = None
|
||||
error: str | None = None
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Helper Functions
|
||||
# =============================================================================
|
||||
|
||||
|
||||
def extract_slug_from_url(url_or_slug: str) -> str:
|
||||
"""
|
||||
Extract vendor slug from Letzshop URL or return as-is if already a slug.
|
||||
|
||||
Handles:
|
||||
- https://letzshop.lu/vendors/my-shop
|
||||
- https://letzshop.lu/en/vendors/my-shop
|
||||
- letzshop.lu/vendors/my-shop
|
||||
- my-shop
|
||||
"""
|
||||
# Clean up the input
|
||||
url_or_slug = url_or_slug.strip()
|
||||
|
||||
# If it looks like a URL, extract the slug
|
||||
if "letzshop" in url_or_slug.lower() or "/" in url_or_slug:
|
||||
# Remove protocol if present
|
||||
url_or_slug = re.sub(r"^https?://", "", url_or_slug)
|
||||
|
||||
# Match pattern like letzshop.lu/[lang/]vendors/SLUG[/...]
|
||||
match = re.search(r"letzshop\.lu/(?:[a-z]{2}/)?vendors?/([^/?#]+)", url_or_slug, re.IGNORECASE)
|
||||
if match:
|
||||
return match.group(1).lower()
|
||||
|
||||
# If just a path like vendors/my-shop
|
||||
match = re.search(r"vendors?/([^/?#]+)", url_or_slug)
|
||||
if match:
|
||||
return match.group(1).lower()
|
||||
|
||||
# Return as-is (assume it's already a slug)
|
||||
return url_or_slug.lower()
|
||||
|
||||
|
||||
def check_if_claimed(db: Session, letzshop_slug: str) -> bool:
|
||||
"""Check if a Letzshop vendor is already claimed."""
|
||||
return db.query(Vendor).filter(
|
||||
Vendor.letzshop_vendor_slug == letzshop_slug,
|
||||
Vendor.is_active == True,
|
||||
).first() is not None
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Endpoints
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@router.get("/letzshop-vendors", response_model=LetzshopVendorListResponse)
|
||||
async def list_letzshop_vendors(
|
||||
search: Annotated[str | None, Query(description="Search by name")] = None,
|
||||
category: Annotated[str | None, Query(description="Filter by category")] = None,
|
||||
city: Annotated[str | None, Query(description="Filter by city")] = None,
|
||||
page: Annotated[int, Query(ge=1)] = 1,
|
||||
limit: Annotated[int, Query(ge=1, le=50)] = 20,
|
||||
db: Session = Depends(get_db),
|
||||
) -> LetzshopVendorListResponse:
|
||||
"""
|
||||
List Letzshop vendors (placeholder - will fetch from cache/API).
|
||||
|
||||
In production, this would fetch from a cached vendor list
|
||||
that is periodically synced from Letzshop's public directory.
|
||||
"""
|
||||
# TODO: Implement actual Letzshop vendor listing
|
||||
# For now, return placeholder data to allow UI development
|
||||
|
||||
# This is placeholder data - in production, we would:
|
||||
# 1. Query our cached letzshop_vendor_cache table
|
||||
# 2. Or fetch from Letzshop's public API if available
|
||||
|
||||
# Return empty list for now - the actual data will come from Phase 4
|
||||
return LetzshopVendorListResponse(
|
||||
vendors=[],
|
||||
total=0,
|
||||
page=page,
|
||||
limit=limit,
|
||||
has_more=False,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/letzshop-vendors/lookup", response_model=LetzshopLookupResponse)
|
||||
async def lookup_letzshop_vendor(
|
||||
request: LetzshopLookupRequest,
|
||||
db: Session = Depends(get_db),
|
||||
) -> LetzshopLookupResponse:
|
||||
"""
|
||||
Lookup a Letzshop vendor by URL or slug.
|
||||
|
||||
This endpoint:
|
||||
1. Extracts the slug from the provided URL
|
||||
2. Attempts to fetch vendor info from Letzshop
|
||||
3. Checks if the vendor is already claimed on our platform
|
||||
4. Returns vendor info for signup pre-fill
|
||||
"""
|
||||
try:
|
||||
slug = extract_slug_from_url(request.url)
|
||||
|
||||
if not slug:
|
||||
return LetzshopLookupResponse(
|
||||
found=False,
|
||||
error="Could not extract vendor slug from URL",
|
||||
)
|
||||
|
||||
# Check if already claimed
|
||||
is_claimed = check_if_claimed(db, slug)
|
||||
|
||||
# TODO: Fetch actual vendor info from Letzshop (Phase 4)
|
||||
# For now, return basic info based on the slug
|
||||
letzshop_url = f"https://letzshop.lu/vendors/{slug}"
|
||||
|
||||
vendor_info = LetzshopVendorInfo(
|
||||
slug=slug,
|
||||
name=slug.replace("-", " ").title(), # Placeholder name
|
||||
letzshop_url=letzshop_url,
|
||||
is_claimed=is_claimed,
|
||||
)
|
||||
|
||||
return LetzshopLookupResponse(
|
||||
found=True,
|
||||
vendor=vendor_info,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error looking up Letzshop vendor: {e}")
|
||||
return LetzshopLookupResponse(
|
||||
found=False,
|
||||
error="Failed to lookup vendor",
|
||||
)
|
||||
|
||||
|
||||
@router.get("/letzshop-vendors/{slug}", response_model=LetzshopVendorInfo)
|
||||
async def get_letzshop_vendor(
|
||||
slug: str,
|
||||
db: Session = Depends(get_db),
|
||||
) -> LetzshopVendorInfo:
|
||||
"""
|
||||
Get a specific Letzshop vendor by slug.
|
||||
|
||||
Returns 404 if vendor not found.
|
||||
"""
|
||||
slug = slug.lower()
|
||||
is_claimed = check_if_claimed(db, slug)
|
||||
|
||||
# TODO: Fetch actual vendor info from cache/API (Phase 4)
|
||||
# For now, return placeholder based on slug
|
||||
|
||||
letzshop_url = f"https://letzshop.lu/vendors/{slug}"
|
||||
|
||||
return LetzshopVendorInfo(
|
||||
slug=slug,
|
||||
name=slug.replace("-", " ").title(),
|
||||
letzshop_url=letzshop_url,
|
||||
is_claimed=is_claimed,
|
||||
)
|
||||
282
app/api/v1/platform/pricing.py
Normal file
282
app/api/v1/platform/pricing.py
Normal file
@@ -0,0 +1,282 @@
|
||||
# app/api/v1/platform/pricing.py
|
||||
"""
|
||||
Public pricing API endpoints.
|
||||
|
||||
Provides subscription tier and add-on product information
|
||||
for the marketing homepage and signup flow.
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from models.database.subscription import (
|
||||
AddOnProduct,
|
||||
BillingPeriod,
|
||||
SubscriptionTier,
|
||||
TIER_LIMITS,
|
||||
TierCode,
|
||||
)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Response Schemas
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TierFeature(BaseModel):
|
||||
"""Feature included in a tier."""
|
||||
|
||||
code: str
|
||||
name: str
|
||||
description: str | None = None
|
||||
|
||||
|
||||
class TierResponse(BaseModel):
|
||||
"""Subscription tier details for public display."""
|
||||
|
||||
code: str
|
||||
name: str
|
||||
description: str | None
|
||||
price_monthly: float # Price in euros
|
||||
price_annual: float | None # Price in euros (null for enterprise)
|
||||
price_monthly_cents: int
|
||||
price_annual_cents: int | None
|
||||
orders_per_month: int | None # None = unlimited
|
||||
products_limit: int | None # None = unlimited
|
||||
team_members: int | None # None = unlimited
|
||||
order_history_months: int | None # None = unlimited
|
||||
features: list[str]
|
||||
is_popular: bool = False # Highlight as recommended
|
||||
is_enterprise: bool = False # Contact sales
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class AddOnResponse(BaseModel):
|
||||
"""Add-on product details for public display."""
|
||||
|
||||
code: str
|
||||
name: str
|
||||
description: str | None
|
||||
category: str
|
||||
price: float # Price in euros
|
||||
price_cents: int
|
||||
billing_period: str
|
||||
quantity_unit: str | None
|
||||
quantity_value: int | None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class PricingResponse(BaseModel):
|
||||
"""Complete pricing information."""
|
||||
|
||||
tiers: list[TierResponse]
|
||||
addons: list[AddOnResponse]
|
||||
trial_days: int
|
||||
annual_discount_months: int # e.g., 2 = "2 months free"
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Feature Descriptions
|
||||
# =============================================================================
|
||||
|
||||
FEATURE_DESCRIPTIONS = {
|
||||
"letzshop_sync": "Letzshop Order Sync",
|
||||
"inventory_basic": "Basic Inventory Management",
|
||||
"inventory_locations": "Warehouse Locations",
|
||||
"inventory_purchase_orders": "Purchase Orders",
|
||||
"invoice_lu": "Luxembourg VAT Invoicing",
|
||||
"invoice_eu_vat": "EU VAT Invoicing",
|
||||
"invoice_bulk": "Bulk Invoicing",
|
||||
"customer_view": "Customer List",
|
||||
"customer_export": "Customer Export",
|
||||
"analytics_dashboard": "Analytics Dashboard",
|
||||
"accounting_export": "Accounting Export",
|
||||
"api_access": "API Access",
|
||||
"automation_rules": "Automation Rules",
|
||||
"team_roles": "Team Roles & Permissions",
|
||||
"white_label": "White-Label Option",
|
||||
"multi_vendor": "Multi-Vendor Support",
|
||||
"custom_integrations": "Custom Integrations",
|
||||
"sla_guarantee": "SLA Guarantee",
|
||||
"dedicated_support": "Dedicated Account Manager",
|
||||
}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Endpoints
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@router.get("/tiers", response_model=list[TierResponse])
|
||||
def get_tiers(db: Session = Depends(get_db)) -> list[TierResponse]:
|
||||
"""
|
||||
Get all public subscription tiers.
|
||||
|
||||
Returns tiers from database if available, falls back to hardcoded TIER_LIMITS.
|
||||
"""
|
||||
# Try to get from database first
|
||||
db_tiers = (
|
||||
db.query(SubscriptionTier)
|
||||
.filter(
|
||||
SubscriptionTier.is_active == True,
|
||||
SubscriptionTier.is_public == True,
|
||||
)
|
||||
.order_by(SubscriptionTier.display_order)
|
||||
.all()
|
||||
)
|
||||
|
||||
if db_tiers:
|
||||
return [
|
||||
TierResponse(
|
||||
code=tier.code,
|
||||
name=tier.name,
|
||||
description=tier.description,
|
||||
price_monthly=tier.price_monthly_cents / 100,
|
||||
price_annual=(tier.price_annual_cents / 100) if tier.price_annual_cents else None,
|
||||
price_monthly_cents=tier.price_monthly_cents,
|
||||
price_annual_cents=tier.price_annual_cents,
|
||||
orders_per_month=tier.orders_per_month,
|
||||
products_limit=tier.products_limit,
|
||||
team_members=tier.team_members,
|
||||
order_history_months=tier.order_history_months,
|
||||
features=tier.features or [],
|
||||
is_popular=tier.code == TierCode.PROFESSIONAL.value,
|
||||
is_enterprise=tier.code == TierCode.ENTERPRISE.value,
|
||||
)
|
||||
for tier in db_tiers
|
||||
]
|
||||
|
||||
# Fallback to hardcoded tiers
|
||||
tiers = []
|
||||
for tier_code, limits in TIER_LIMITS.items():
|
||||
tiers.append(
|
||||
TierResponse(
|
||||
code=tier_code.value,
|
||||
name=limits["name"],
|
||||
description=None,
|
||||
price_monthly=limits["price_monthly_cents"] / 100,
|
||||
price_annual=(limits["price_annual_cents"] / 100) if limits.get("price_annual_cents") else None,
|
||||
price_monthly_cents=limits["price_monthly_cents"],
|
||||
price_annual_cents=limits.get("price_annual_cents"),
|
||||
orders_per_month=limits.get("orders_per_month"),
|
||||
products_limit=limits.get("products_limit"),
|
||||
team_members=limits.get("team_members"),
|
||||
order_history_months=limits.get("order_history_months"),
|
||||
features=limits.get("features", []),
|
||||
is_popular=tier_code == TierCode.PROFESSIONAL,
|
||||
is_enterprise=tier_code == TierCode.ENTERPRISE,
|
||||
)
|
||||
)
|
||||
|
||||
return tiers
|
||||
|
||||
|
||||
@router.get("/tiers/{tier_code}", response_model=TierResponse)
|
||||
def get_tier(tier_code: str, db: Session = Depends(get_db)) -> TierResponse:
|
||||
"""Get a specific tier by code."""
|
||||
# Try database first
|
||||
tier = (
|
||||
db.query(SubscriptionTier)
|
||||
.filter(
|
||||
SubscriptionTier.code == tier_code,
|
||||
SubscriptionTier.is_active == True,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
if tier:
|
||||
return TierResponse(
|
||||
code=tier.code,
|
||||
name=tier.name,
|
||||
description=tier.description,
|
||||
price_monthly=tier.price_monthly_cents / 100,
|
||||
price_annual=(tier.price_annual_cents / 100) if tier.price_annual_cents else None,
|
||||
price_monthly_cents=tier.price_monthly_cents,
|
||||
price_annual_cents=tier.price_annual_cents,
|
||||
orders_per_month=tier.orders_per_month,
|
||||
products_limit=tier.products_limit,
|
||||
team_members=tier.team_members,
|
||||
order_history_months=tier.order_history_months,
|
||||
features=tier.features or [],
|
||||
is_popular=tier.code == TierCode.PROFESSIONAL.value,
|
||||
is_enterprise=tier.code == TierCode.ENTERPRISE.value,
|
||||
)
|
||||
|
||||
# Fallback to hardcoded
|
||||
try:
|
||||
tier_enum = TierCode(tier_code)
|
||||
limits = TIER_LIMITS[tier_enum]
|
||||
return TierResponse(
|
||||
code=tier_enum.value,
|
||||
name=limits["name"],
|
||||
description=None,
|
||||
price_monthly=limits["price_monthly_cents"] / 100,
|
||||
price_annual=(limits["price_annual_cents"] / 100) if limits.get("price_annual_cents") else None,
|
||||
price_monthly_cents=limits["price_monthly_cents"],
|
||||
price_annual_cents=limits.get("price_annual_cents"),
|
||||
orders_per_month=limits.get("orders_per_month"),
|
||||
products_limit=limits.get("products_limit"),
|
||||
team_members=limits.get("team_members"),
|
||||
order_history_months=limits.get("order_history_months"),
|
||||
features=limits.get("features", []),
|
||||
is_popular=tier_enum == TierCode.PROFESSIONAL,
|
||||
is_enterprise=tier_enum == TierCode.ENTERPRISE,
|
||||
)
|
||||
except ValueError:
|
||||
from fastapi import HTTPException
|
||||
raise HTTPException(status_code=404, detail=f"Tier '{tier_code}' not found")
|
||||
|
||||
|
||||
@router.get("/addons", response_model=list[AddOnResponse])
|
||||
def get_addons(db: Session = Depends(get_db)) -> list[AddOnResponse]:
|
||||
"""
|
||||
Get all available add-on products.
|
||||
|
||||
Returns add-ons from database, or empty list if none configured.
|
||||
"""
|
||||
addons = (
|
||||
db.query(AddOnProduct)
|
||||
.filter(AddOnProduct.is_active == True)
|
||||
.order_by(AddOnProduct.category, AddOnProduct.display_order)
|
||||
.all()
|
||||
)
|
||||
|
||||
return [
|
||||
AddOnResponse(
|
||||
code=addon.code,
|
||||
name=addon.name,
|
||||
description=addon.description,
|
||||
category=addon.category,
|
||||
price=addon.price_cents / 100,
|
||||
price_cents=addon.price_cents,
|
||||
billing_period=addon.billing_period,
|
||||
quantity_unit=addon.quantity_unit,
|
||||
quantity_value=addon.quantity_value,
|
||||
)
|
||||
for addon in addons
|
||||
]
|
||||
|
||||
|
||||
@router.get("/pricing", response_model=PricingResponse)
|
||||
def get_pricing(db: Session = Depends(get_db)) -> PricingResponse:
|
||||
"""
|
||||
Get complete pricing information (tiers + add-ons).
|
||||
|
||||
This is the main endpoint for the pricing page.
|
||||
"""
|
||||
from app.core.config import settings
|
||||
|
||||
return PricingResponse(
|
||||
tiers=get_tiers(db),
|
||||
addons=get_addons(db),
|
||||
trial_days=settings.stripe_trial_days,
|
||||
annual_discount_months=2, # "2 months free" with annual billing
|
||||
)
|
||||
541
app/api/v1/platform/signup.py
Normal file
541
app/api/v1/platform/signup.py
Normal file
@@ -0,0 +1,541 @@
|
||||
# app/api/v1/platform/signup.py
|
||||
"""
|
||||
Platform signup API endpoints.
|
||||
|
||||
Handles the multi-step signup flow:
|
||||
1. Start signup (select tier)
|
||||
2. Claim Letzshop vendor (optional)
|
||||
3. Create account
|
||||
4. Setup payment (collect card via SetupIntent)
|
||||
5. Complete signup (create subscription with trial)
|
||||
"""
|
||||
|
||||
import logging
|
||||
import secrets
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel, EmailStr
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.database import get_db
|
||||
from app.services.stripe_service import stripe_service
|
||||
from models.database.subscription import (
|
||||
SubscriptionStatus,
|
||||
TierCode,
|
||||
VendorSubscription,
|
||||
)
|
||||
from models.database.vendor import Vendor, VendorUser, VendorUserType
|
||||
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# In-memory signup session storage (for simplicity)
|
||||
# In production, use Redis or database table
|
||||
# =============================================================================
|
||||
|
||||
_signup_sessions: dict[str, dict] = {}
|
||||
|
||||
|
||||
def create_session_id() -> str:
|
||||
"""Generate a secure session ID."""
|
||||
return secrets.token_urlsafe(32)
|
||||
|
||||
|
||||
def get_session(session_id: str) -> dict | None:
|
||||
"""Get a signup session by ID."""
|
||||
return _signup_sessions.get(session_id)
|
||||
|
||||
|
||||
def save_session(session_id: str, data: dict) -> None:
|
||||
"""Save signup session data."""
|
||||
_signup_sessions[session_id] = {
|
||||
**data,
|
||||
"updated_at": datetime.now(UTC).isoformat(),
|
||||
}
|
||||
|
||||
|
||||
def delete_session(session_id: str) -> None:
|
||||
"""Delete a signup session."""
|
||||
_signup_sessions.pop(session_id, None)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Request/Response Schemas
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class SignupStartRequest(BaseModel):
|
||||
"""Start signup - select tier."""
|
||||
|
||||
tier_code: str
|
||||
is_annual: bool = False
|
||||
|
||||
|
||||
class SignupStartResponse(BaseModel):
|
||||
"""Response from signup start."""
|
||||
|
||||
session_id: str
|
||||
tier_code: str
|
||||
is_annual: bool
|
||||
|
||||
|
||||
class ClaimVendorRequest(BaseModel):
|
||||
"""Claim Letzshop vendor."""
|
||||
|
||||
session_id: str
|
||||
letzshop_slug: str
|
||||
letzshop_vendor_id: str | None = None
|
||||
|
||||
|
||||
class ClaimVendorResponse(BaseModel):
|
||||
"""Response from vendor claim."""
|
||||
|
||||
session_id: str
|
||||
letzshop_slug: str
|
||||
vendor_name: str | None
|
||||
|
||||
|
||||
class CreateAccountRequest(BaseModel):
|
||||
"""Create account."""
|
||||
|
||||
session_id: str
|
||||
email: EmailStr
|
||||
password: str
|
||||
first_name: str
|
||||
last_name: str
|
||||
company_name: str
|
||||
phone: str | None = None
|
||||
|
||||
|
||||
class CreateAccountResponse(BaseModel):
|
||||
"""Response from account creation."""
|
||||
|
||||
session_id: str
|
||||
user_id: int
|
||||
vendor_id: int
|
||||
stripe_customer_id: str
|
||||
|
||||
|
||||
class SetupPaymentRequest(BaseModel):
|
||||
"""Request payment setup."""
|
||||
|
||||
session_id: str
|
||||
|
||||
|
||||
class SetupPaymentResponse(BaseModel):
|
||||
"""Response with Stripe SetupIntent client secret."""
|
||||
|
||||
session_id: str
|
||||
client_secret: str
|
||||
stripe_customer_id: str
|
||||
|
||||
|
||||
class CompleteSignupRequest(BaseModel):
|
||||
"""Complete signup after card collection."""
|
||||
|
||||
session_id: str
|
||||
setup_intent_id: str
|
||||
|
||||
|
||||
class CompleteSignupResponse(BaseModel):
|
||||
"""Response from signup completion."""
|
||||
|
||||
success: bool
|
||||
vendor_code: str
|
||||
vendor_id: int
|
||||
redirect_url: str
|
||||
trial_ends_at: str
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Endpoints
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@router.post("/signup/start", response_model=SignupStartResponse)
|
||||
async def start_signup(
|
||||
request: SignupStartRequest,
|
||||
db: Session = Depends(get_db),
|
||||
) -> SignupStartResponse:
|
||||
"""
|
||||
Start the signup process.
|
||||
|
||||
Step 1: User selects a tier and billing period.
|
||||
Creates a signup session to track the flow.
|
||||
"""
|
||||
# Validate tier code
|
||||
try:
|
||||
tier = TierCode(request.tier_code)
|
||||
except ValueError:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Invalid tier code: {request.tier_code}",
|
||||
)
|
||||
|
||||
# Create session
|
||||
session_id = create_session_id()
|
||||
save_session(session_id, {
|
||||
"step": "tier_selected",
|
||||
"tier_code": tier.value,
|
||||
"is_annual": request.is_annual,
|
||||
"created_at": datetime.now(UTC).isoformat(),
|
||||
})
|
||||
|
||||
logger.info(f"Started signup session {session_id} for tier {tier.value}")
|
||||
|
||||
return SignupStartResponse(
|
||||
session_id=session_id,
|
||||
tier_code=tier.value,
|
||||
is_annual=request.is_annual,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/signup/claim-vendor", response_model=ClaimVendorResponse)
|
||||
async def claim_letzshop_vendor(
|
||||
request: ClaimVendorRequest,
|
||||
db: Session = Depends(get_db),
|
||||
) -> ClaimVendorResponse:
|
||||
"""
|
||||
Claim a Letzshop vendor.
|
||||
|
||||
Step 2 (optional): User claims their Letzshop shop.
|
||||
This pre-fills vendor info during account creation.
|
||||
"""
|
||||
session = get_session(request.session_id)
|
||||
if not session:
|
||||
raise HTTPException(status_code=404, detail="Signup session not found")
|
||||
|
||||
# Check if vendor is already claimed
|
||||
existing = db.query(Vendor).filter(
|
||||
Vendor.letzshop_vendor_slug == request.letzshop_slug,
|
||||
Vendor.is_active == True,
|
||||
).first()
|
||||
|
||||
if existing:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="This Letzshop vendor is already claimed",
|
||||
)
|
||||
|
||||
# Update session with vendor info
|
||||
session["letzshop_slug"] = request.letzshop_slug
|
||||
session["letzshop_vendor_id"] = request.letzshop_vendor_id
|
||||
session["step"] = "vendor_claimed"
|
||||
|
||||
# TODO: Fetch actual vendor name from Letzshop API
|
||||
vendor_name = request.letzshop_slug.replace("-", " ").title()
|
||||
session["vendor_name"] = vendor_name
|
||||
|
||||
save_session(request.session_id, session)
|
||||
|
||||
logger.info(f"Claimed vendor {request.letzshop_slug} for session {request.session_id}")
|
||||
|
||||
return ClaimVendorResponse(
|
||||
session_id=request.session_id,
|
||||
letzshop_slug=request.letzshop_slug,
|
||||
vendor_name=vendor_name,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/signup/create-account", response_model=CreateAccountResponse)
|
||||
async def create_account(
|
||||
request: CreateAccountRequest,
|
||||
db: Session = Depends(get_db),
|
||||
) -> CreateAccountResponse:
|
||||
"""
|
||||
Create user and vendor accounts.
|
||||
|
||||
Step 3: User provides account details.
|
||||
Creates User, Company, Vendor, and Stripe Customer.
|
||||
"""
|
||||
session = get_session(request.session_id)
|
||||
if not session:
|
||||
raise HTTPException(status_code=404, detail="Signup session not found")
|
||||
|
||||
# Check if email already exists
|
||||
from models.database.user import User
|
||||
|
||||
existing_user = db.query(User).filter(User.email == request.email).first()
|
||||
if existing_user:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="An account with this email already exists",
|
||||
)
|
||||
|
||||
try:
|
||||
# Create Company
|
||||
from models.database.company import Company
|
||||
|
||||
company = Company(
|
||||
name=request.company_name,
|
||||
contact_email=request.email,
|
||||
contact_phone=request.phone,
|
||||
)
|
||||
db.add(company)
|
||||
db.flush()
|
||||
|
||||
# Create User
|
||||
from app.core.security import get_password_hash
|
||||
|
||||
user = User(
|
||||
email=request.email,
|
||||
hashed_password=get_password_hash(request.password),
|
||||
first_name=request.first_name,
|
||||
last_name=request.last_name,
|
||||
is_active=True,
|
||||
)
|
||||
db.add(user)
|
||||
db.flush()
|
||||
|
||||
# Generate vendor code
|
||||
vendor_code = request.company_name.upper().replace(" ", "_")[:20]
|
||||
# Ensure unique
|
||||
base_code = vendor_code
|
||||
counter = 1
|
||||
while db.query(Vendor).filter(Vendor.vendor_code == vendor_code).first():
|
||||
vendor_code = f"{base_code}_{counter}"
|
||||
counter += 1
|
||||
|
||||
# Generate subdomain
|
||||
subdomain = request.company_name.lower().replace(" ", "-")
|
||||
subdomain = "".join(c for c in subdomain if c.isalnum() or c == "-")[:50]
|
||||
base_subdomain = subdomain
|
||||
counter = 1
|
||||
while db.query(Vendor).filter(Vendor.subdomain == subdomain).first():
|
||||
subdomain = f"{base_subdomain}-{counter}"
|
||||
counter += 1
|
||||
|
||||
# Create Vendor
|
||||
vendor = Vendor(
|
||||
company_id=company.id,
|
||||
vendor_code=vendor_code,
|
||||
subdomain=subdomain,
|
||||
name=request.company_name,
|
||||
contact_email=request.email,
|
||||
contact_phone=request.phone,
|
||||
is_active=True,
|
||||
letzshop_vendor_slug=session.get("letzshop_slug"),
|
||||
letzshop_vendor_id=session.get("letzshop_vendor_id"),
|
||||
)
|
||||
db.add(vendor)
|
||||
db.flush()
|
||||
|
||||
# Create VendorUser (owner)
|
||||
vendor_user = VendorUser(
|
||||
vendor_id=vendor.id,
|
||||
user_id=user.id,
|
||||
user_type=VendorUserType.OWNER.value,
|
||||
is_active=True,
|
||||
)
|
||||
db.add(vendor_user)
|
||||
|
||||
# Create Stripe Customer
|
||||
stripe_customer_id = stripe_service.create_customer(
|
||||
vendor=vendor,
|
||||
email=request.email,
|
||||
name=f"{request.first_name} {request.last_name}",
|
||||
metadata={
|
||||
"company_name": request.company_name,
|
||||
"tier": session.get("tier_code"),
|
||||
},
|
||||
)
|
||||
|
||||
# Create VendorSubscription (in trial status, without Stripe subscription yet)
|
||||
now = datetime.now(UTC)
|
||||
trial_end = now + timedelta(days=settings.stripe_trial_days)
|
||||
|
||||
subscription = VendorSubscription(
|
||||
vendor_id=vendor.id,
|
||||
tier=session.get("tier_code", TierCode.ESSENTIAL.value),
|
||||
status=SubscriptionStatus.TRIAL.value,
|
||||
period_start=now,
|
||||
period_end=trial_end,
|
||||
trial_ends_at=trial_end,
|
||||
is_annual=session.get("is_annual", False),
|
||||
stripe_customer_id=stripe_customer_id,
|
||||
)
|
||||
db.add(subscription)
|
||||
|
||||
db.commit()
|
||||
|
||||
# Update session
|
||||
session["user_id"] = user.id
|
||||
session["vendor_id"] = vendor.id
|
||||
session["vendor_code"] = vendor_code
|
||||
session["stripe_customer_id"] = stripe_customer_id
|
||||
session["step"] = "account_created"
|
||||
save_session(request.session_id, session)
|
||||
|
||||
logger.info(
|
||||
f"Created account for {request.email}: "
|
||||
f"user_id={user.id}, vendor_id={vendor.id}"
|
||||
)
|
||||
|
||||
return CreateAccountResponse(
|
||||
session_id=request.session_id,
|
||||
user_id=user.id,
|
||||
vendor_id=vendor.id,
|
||||
stripe_customer_id=stripe_customer_id,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Error creating account: {e}")
|
||||
raise HTTPException(status_code=500, detail="Failed to create account")
|
||||
|
||||
|
||||
@router.post("/signup/setup-payment", response_model=SetupPaymentResponse)
|
||||
async def setup_payment(
|
||||
request: SetupPaymentRequest,
|
||||
db: Session = Depends(get_db),
|
||||
) -> SetupPaymentResponse:
|
||||
"""
|
||||
Create Stripe SetupIntent for card collection.
|
||||
|
||||
Step 4: Collect card details without charging.
|
||||
The card will be charged after the trial period ends.
|
||||
"""
|
||||
session = get_session(request.session_id)
|
||||
if not session:
|
||||
raise HTTPException(status_code=404, detail="Signup session not found")
|
||||
|
||||
if "stripe_customer_id" not in session:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Account not created. Please complete step 3 first.",
|
||||
)
|
||||
|
||||
stripe_customer_id = session["stripe_customer_id"]
|
||||
|
||||
# Create SetupIntent
|
||||
setup_intent = stripe_service.create_setup_intent(
|
||||
customer_id=stripe_customer_id,
|
||||
metadata={
|
||||
"session_id": request.session_id,
|
||||
"vendor_id": str(session.get("vendor_id")),
|
||||
"tier": session.get("tier_code"),
|
||||
},
|
||||
)
|
||||
|
||||
# Update session
|
||||
session["setup_intent_id"] = setup_intent.id
|
||||
session["step"] = "payment_pending"
|
||||
save_session(request.session_id, session)
|
||||
|
||||
logger.info(f"Created SetupIntent {setup_intent.id} for session {request.session_id}")
|
||||
|
||||
return SetupPaymentResponse(
|
||||
session_id=request.session_id,
|
||||
client_secret=setup_intent.client_secret,
|
||||
stripe_customer_id=stripe_customer_id,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/signup/complete", response_model=CompleteSignupResponse)
|
||||
async def complete_signup(
|
||||
request: CompleteSignupRequest,
|
||||
db: Session = Depends(get_db),
|
||||
) -> CompleteSignupResponse:
|
||||
"""
|
||||
Complete signup after card collection.
|
||||
|
||||
Step 5: Verify SetupIntent, attach payment method, create subscription.
|
||||
"""
|
||||
session = get_session(request.session_id)
|
||||
if not session:
|
||||
raise HTTPException(status_code=404, detail="Signup session not found")
|
||||
|
||||
vendor_id = session.get("vendor_id")
|
||||
stripe_customer_id = session.get("stripe_customer_id")
|
||||
|
||||
if not vendor_id or not stripe_customer_id:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Incomplete signup. Please start again.",
|
||||
)
|
||||
|
||||
try:
|
||||
# Retrieve SetupIntent to get payment method
|
||||
setup_intent = stripe_service.get_setup_intent(request.setup_intent_id)
|
||||
|
||||
if setup_intent.status != "succeeded":
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Card setup not completed. Please try again.",
|
||||
)
|
||||
|
||||
payment_method_id = setup_intent.payment_method
|
||||
|
||||
# Attach payment method to customer
|
||||
stripe_service.attach_payment_method_to_customer(
|
||||
customer_id=stripe_customer_id,
|
||||
payment_method_id=payment_method_id,
|
||||
set_as_default=True,
|
||||
)
|
||||
|
||||
# Update subscription record with card collection time
|
||||
subscription = (
|
||||
db.query(VendorSubscription)
|
||||
.filter(VendorSubscription.vendor_id == vendor_id)
|
||||
.first()
|
||||
)
|
||||
|
||||
if subscription:
|
||||
subscription.card_collected_at = datetime.now(UTC)
|
||||
subscription.stripe_payment_method_id = payment_method_id
|
||||
|
||||
# TODO: Create actual Stripe subscription with trial
|
||||
# For now, just mark as trial with card collected
|
||||
|
||||
db.commit()
|
||||
|
||||
vendor = db.query(Vendor).filter(Vendor.id == vendor_id).first()
|
||||
vendor_code = vendor.vendor_code if vendor else session.get("vendor_code")
|
||||
|
||||
# Clean up session
|
||||
delete_session(request.session_id)
|
||||
|
||||
trial_ends_at = subscription.trial_ends_at if subscription else datetime.now(UTC) + timedelta(days=30)
|
||||
|
||||
logger.info(f"Completed signup for vendor {vendor_id}")
|
||||
|
||||
return CompleteSignupResponse(
|
||||
success=True,
|
||||
vendor_code=vendor_code,
|
||||
vendor_id=vendor_id,
|
||||
redirect_url=f"/vendors/{vendor_code}/dashboard",
|
||||
trial_ends_at=trial_ends_at.isoformat(),
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error completing signup: {e}")
|
||||
raise HTTPException(status_code=500, detail="Failed to complete signup")
|
||||
|
||||
|
||||
@router.get("/signup/session/{session_id}")
|
||||
async def get_signup_session(session_id: str) -> dict:
|
||||
"""
|
||||
Get signup session status.
|
||||
|
||||
Useful for resuming an incomplete signup.
|
||||
"""
|
||||
session = get_session(session_id)
|
||||
if not session:
|
||||
raise HTTPException(status_code=404, detail="Session not found")
|
||||
|
||||
# Return safe subset of session data
|
||||
return {
|
||||
"session_id": session_id,
|
||||
"step": session.get("step"),
|
||||
"tier_code": session.get("tier_code"),
|
||||
"is_annual": session.get("is_annual"),
|
||||
"letzshop_slug": session.get("letzshop_slug"),
|
||||
"vendor_name": session.get("vendor_name"),
|
||||
"created_at": session.get("created_at"),
|
||||
}
|
||||
Reference in New Issue
Block a user