Coverage for backend / app / payments / checkout.py: 100%
12 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-17 21:34 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-17 21:34 +0000
1"""Stripe checkout module"""
3from app.config import settings
4from app.payments import stripe, logger
7async def build_checkout_params(customer_id: str) -> dict:
8 """Build checkout session parameters based on customer history.
9 :param customer_id: Stripe customer ID
10 :return: Dictionary of checkout session parameters"""
12 # Check if customer had any previous subscriptions (including cancelled)
13 subscriptions = await stripe.Subscription.list_async(customer=customer_id, status="all", limit=1)
15 checkout_params = {
16 "customer": customer_id,
17 "line_items": [
18 {
19 "price": settings.stripe_toast_price_id,
20 "quantity": 1,
21 }
22 ],
23 "mode": "subscription",
24 "locale": "auto",
25 "ui_mode": "hosted",
26 "allow_promotion_codes": True,
27 "success_url": f"{settings.frontend_url}/settings/premium?success=true",
28 "cancel_url": f"{settings.frontend_url}/settings/premium?canceled=true",
29 "subscription_data": {},
30 }
32 # Previous subscriber - no trial, payment required
33 if subscriptions.data:
34 checkout_params["payment_method_collection"] = "always"
35 logger.info(f"No trial for returning customer {customer_id} - payment required")
37 # New customer - 14-day trial, payment optional
38 else:
39 checkout_params["payment_method_collection"] = "if_required"
40 checkout_params["subscription_data"] = {
41 "trial_period_days": 14,
42 "trial_settings": {
43 "end_behavior": {
44 "missing_payment_method": "cancel",
45 },
46 },
47 }
48 logger.info(f"Offering 14-day trial to new customer {customer_id}")
50 return checkout_params