Spaces:
Paused

File size: 34,878 Bytes
22087d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
"""
Subscription mutation endpoints.

Note: For reading subscription state, use GET /billing/account-state instead.
These endpoints are for subscription mutations (create, cancel, reactivate, etc.).
"""
from fastapi import APIRouter, HTTPException, Depends, Query
from typing import Dict
from core.utils.auth_utils import verify_and_get_user_id_from_jwt
from core.utils.logger import logger
from ..shared.models import (
    CreateCheckoutSessionRequest,
    CreatePortalSessionRequest,
    CancelSubscriptionRequest,
    CreateInlineCheckoutRequest
)
from ..shared.config import get_tier_by_name
from ..subscriptions import subscription_service
import stripe
from core.utils.config import config
from ..shared.cache_utils import invalidate_account_state_cache, invalidate_all_billing_caches

stripe.api_key = config.STRIPE_SECRET_KEY

router = APIRouter(tags=["billing-subscriptions"])


@router.post("/create-checkout-session")
async def create_checkout_session(
    request: CreateCheckoutSessionRequest,
    account_id: str = Depends(verify_and_get_user_id_from_jwt)
) -> Dict:
    """Create a Stripe checkout session for subscription."""
    try:
        from ..subscriptions import free_tier_service
        tier = get_tier_by_name(request.tier_key)
        if not tier:
            raise HTTPException(status_code=400, detail="Invalid tier")
        
        if tier.name == 'free':
            result = await free_tier_service.auto_subscribe_to_free_tier(account_id)
            if result.get('success'):
                await invalidate_account_state_cache(account_id)
                return {
                    'checkout_url': request.success_url,
                    'message': 'Successfully subscribed to free tier'
                }
            else:
                raise HTTPException(status_code=400, detail=result.get('message', 'Failed to subscribe to free tier'))
        
        if request.commitment_type == 'yearly_commitment':
            price_ids = [pid for pid in tier.price_ids if 'yearly_commitment' in pid.lower()]
            if not price_ids:
                raise HTTPException(status_code=400, detail="Yearly commitment not available for this tier")
            price_id = price_ids[0]
        elif request.commitment_type == 'yearly':
            logger.debug(f"[YEARLY-BILLING] Selecting yearly price for tier: {tier.name}, available price_ids: {tier.price_ids}")
            price_id = None
            if tier.name == 'tier_2_20':
                price_id = config.STRIPE_TIER_2_20_YEARLY_ID
                logger.debug(f"[YEARLY-BILLING] Selected tier_2_20 yearly: {price_id}")
            elif tier.name == 'tier_6_50':
                price_id = config.STRIPE_TIER_6_50_YEARLY_ID
                logger.debug(f"[YEARLY-BILLING] Selected tier_6_50 yearly: {price_id}")
            elif tier.name == 'tier_25_200':
                price_id = config.STRIPE_TIER_25_200_YEARLY_ID
                logger.debug(f"[YEARLY-BILLING] Selected tier_25_200 yearly: {price_id}")
            else:
                logger.debug(f"[YEARLY-BILLING] Using fallback string matching for tier: {tier.name}")
                price_ids = [pid for pid in tier.price_ids if 'yearly' in pid.lower() and 'commitment' not in pid.lower()]
                logger.debug(f"[YEARLY-BILLING] Found yearly price_ids: {price_ids}")
                price_id = price_ids[0] if price_ids else tier.price_ids[0]
        else:
            price_ids = [pid for pid in tier.price_ids if 'yearly' not in pid.lower()]
            price_id = price_ids[0] if price_ids else tier.price_ids[0]
        
        logger.debug(f"[BILLING] Creating checkout session: account_id={account_id}, tier={tier.name}, commitment_type={request.commitment_type}, selected_price_id={price_id}")
        
        result = await subscription_service.create_checkout_session(
            account_id=account_id,
            price_id=price_id,
            success_url=request.success_url,
            cancel_url=request.cancel_url or request.success_url,
            commitment_type=request.commitment_type,
            locale=request.locale
        )
        
        # Invalidate cache if subscription was created/updated
        if result.get('status') in ['upgraded', 'updated', 'new']:
            await invalidate_account_state_cache(account_id)
        
        result['success'] = True
        if 'message' not in result:
            if 'checkout_url' in result:
                result['message'] = 'Embedded checkout session created'
            else:
                result['message'] = 'Subscription updated successfully'
        
        return result
        
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"[BILLING] Error creating checkout session: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/create-inline-checkout")
async def create_inline_checkout(
    request: CreateInlineCheckoutRequest,
    account_id: str = Depends(verify_and_get_user_id_from_jwt)
) -> Dict:
    """
    Create a subscription with incomplete payment for inline checkout.
    Returns client_secret for Stripe PaymentElement.
    """
    try:
        from ..subscriptions.handlers.customer import CustomerHandler

        tier = get_tier_by_name(request.tier_key)
        if not tier:
            raise HTTPException(status_code=400, detail="Invalid tier")

        if tier.name == 'free':
            raise HTTPException(status_code=400, detail="Cannot use inline checkout for free tier")

        # Get price ID based on billing period
        if request.billing_period == 'yearly':
            if tier.name == 'tier_2_20':
                price_id = config.STRIPE_TIER_2_20_YEARLY_ID
            elif tier.name == 'tier_6_50':
                price_id = config.STRIPE_TIER_6_50_YEARLY_ID
            elif tier.name == 'tier_25_200':
                price_id = config.STRIPE_TIER_25_200_YEARLY_ID
            else:
                price_ids = [pid for pid in tier.price_ids if 'yearly' in pid.lower() and 'commitment' not in pid.lower()]
                price_id = price_ids[0] if price_ids else tier.price_ids[0]
        else:
            price_ids = [pid for pid in tier.price_ids if 'yearly' not in pid.lower()]
            price_id = price_ids[0] if price_ids else tier.price_ids[0]

        # Get or create Stripe customer
        customer_id = await CustomerHandler.get_or_create_stripe_customer(account_id)

        # Check for existing active subscription
        existing_subs = await stripe.Subscription.list_async(
            customer=customer_id,
            status='active',
            limit=1
        )

        # Handle existing subscription (use dict-style access for Stripe objects)
        if existing_subs.data:
            existing_sub = existing_subs.data[0]
            existing_items = existing_sub['items']['data'] if existing_sub.get('items') else []
            if existing_items:
                existing_price = existing_items[0].get('price')
                unit_amount = existing_price.get('unit_amount', 0) if existing_price else 0

                if unit_amount == 0 or unit_amount is None:
                    # It's a free tier, cancel it and proceed with new subscription
                    await stripe.Subscription.cancel_async(existing_sub['id'])
                    logger.info(f"[INLINE CHECKOUT] Cancelled free tier subscription {existing_sub['id']}")
                else:
                    # User has paid subscription - check if this is an upgrade
                    current_tier_key = existing_sub.get('metadata', {}).get('tier_key')

                    if not current_tier_key:
                        # Fallback: get tier from credit_account table
                        from ..subscriptions.repositories.credit_account import CreditAccountRepository
                        credit_repo = CreditAccountRepository()
                        credit_account = await credit_repo.get_credit_account(account_id, 'tier')
                        current_tier_key = credit_account.get('tier') if credit_account else None
                        logger.info(f"[INLINE CHECKOUT] Got current tier from credit_account: {current_tier_key}")

                    current_tier = get_tier_by_name(current_tier_key) if current_tier_key else None

                    # Compare by monthly credits (higher = better tier)
                    current_credits = float(current_tier.monthly_credits) if current_tier else 0
                    new_credits = float(tier.monthly_credits)

                    logger.info(f"[INLINE CHECKOUT] Comparing tiers: current={current_tier_key} ({current_credits} credits) vs new={request.tier_key} ({new_credits} credits)")

                    if new_credits <= current_credits:
                        # Not an upgrade - block and redirect to settings
                        raise HTTPException(
                            status_code=400,
                            detail="To downgrade or change to same tier, go to Settings > Billing."
                        )

                    # It's an upgrade - use modify_subscription to upgrade in place
                    # This uses the existing payment method, no need for user to re-enter card
                    logger.info(f"[INLINE CHECKOUT] Upgrading subscription {existing_sub['id']} from {current_tier_key} to {request.tier_key}")

                    try:
                        from ..subscriptions.services.lifecycle_service import LifecycleService
                        from ..subscriptions.repositories.credit_account import CreditAccountRepository
                        from datetime import datetime, timezone

                        # Modify subscription in place (uses existing payment method)
                        # Default behavior charges the existing payment method immediately
                        updated_sub = await stripe.Subscription.modify_async(
                            existing_sub['id'],
                            items=[{
                                'id': existing_items[0]['id'],
                                'price': price_id,
                            }],
                            proration_behavior='always_invoice',
                            metadata={
                                'account_id': account_id,
                                'tier_key': request.tier_key,
                                'billing_period': request.billing_period,
                                'previous_tier': current_tier_key,
                                'source': 'inline_checkout_upgrade'
                            }
                        )

                        logger.info(f"[INLINE CHECKOUT] Subscription upgraded: {updated_sub['id']} status={updated_sub['status']}")

                        # Verify subscription is active before granting credits
                        if updated_sub['status'] != 'active':
                            logger.error(f"[INLINE CHECKOUT] Upgrade failed - subscription status: {updated_sub['status']}")
                            raise HTTPException(
                                status_code=402,
                                detail=f"Payment failed. Subscription status: {updated_sub['status']}. Please check your payment method."
                            )

                        # Grant credits for the upgrade
                        lifecycle_service = LifecycleService()
                        new_tier_info = {
                            'name': tier.name,
                            'credits': float(tier.monthly_credits)
                        }
                        billing_anchor = datetime.fromtimestamp(updated_sub['current_period_start'], tz=timezone.utc)
                        await lifecycle_service.grant_subscription_credits(account_id, new_tier_info, billing_anchor, is_tier_upgrade=True)

                        # Update credit account
                        plan_type = 'yearly' if request.billing_period == 'yearly' else 'monthly'
                        next_grant_date = lifecycle_service.calculate_next_credit_grant(
                            plan_type, billing_anchor, updated_sub['current_period_end']
                        )

                        credit_repo = CreditAccountRepository()
                        await credit_repo.update_credit_account(account_id, {
                            'tier': request.tier_key,
                            'plan_type': plan_type,
                            'stripe_subscription_id': updated_sub['id'],
                            'billing_cycle_anchor': billing_anchor.isoformat(),
                            'next_credit_grant': next_grant_date.isoformat(),
                            'last_grant_date': billing_anchor.isoformat()
                        })

                        # Invalidate caches
                        await invalidate_all_billing_caches(account_id)

                        return {
                            'subscription_id': updated_sub['id'],
                            'tier_key': request.tier_key,
                            'upgraded': True,
                            'previous_tier': current_tier_key,
                            'credits_granted': int(tier.monthly_credits),
                            'message': f'Upgraded to {tier.display_name}! Your existing payment method was charged.'
                        }

                    except stripe.error.CardError as e:
                        logger.error(f"[INLINE CHECKOUT] Card error during upgrade: {e}")
                        raise HTTPException(status_code=402, detail=f"Payment failed: {e.user_message}")
                    except Exception as e:
                        logger.error(f"[INLINE CHECKOUT] Error during upgrade: {e}")
                        raise HTTPException(status_code=500, detail=str(e))

        # Cancel any existing incomplete subscriptions to avoid clutter
        try:
            incomplete_subs = await stripe.Subscription.list_async(
                customer=customer_id,
                status='incomplete',
                limit=10
            )
            for sub in incomplete_subs.data:
                try:
                    await stripe.Subscription.cancel_async(sub['id'])
                    logger.info(f"[INLINE CHECKOUT] Cancelled incomplete subscription {sub['id']}")
                except Exception as e:
                    logger.warning(f"[INLINE CHECKOUT] Could not cancel incomplete sub {sub['id']}: {e}")
        except Exception as e:
            logger.warning(f"[INLINE CHECKOUT] Error cleaning up incomplete subscriptions: {e}")

        # Look up promo code if provided
        promotion_code_id = None
        if request.promo_code:
            try:
                promo_codes = await stripe.PromotionCode.list_async(
                    code=request.promo_code,
                    active=True,
                    limit=1
                )
                if promo_codes.data:
                    promotion_code_id = promo_codes.data[0]['id']
                    logger.info(f"[INLINE CHECKOUT] Found promo code {request.promo_code} -> {promotion_code_id}")
                else:
                    logger.warning(f"[INLINE CHECKOUT] Promo code {request.promo_code} not found or inactive")
                    raise HTTPException(status_code=400, detail=f"Promo code '{request.promo_code}' is invalid or expired")
            except HTTPException:
                raise
            except Exception as e:
                logger.warning(f"[INLINE CHECKOUT] Error looking up promo code: {e}")
                raise HTTPException(status_code=400, detail=f"Could not validate promo code '{request.promo_code}'")

        # Create subscription with incomplete payment
        metadata = {
            'account_id': account_id,
            'tier_key': request.tier_key,
            'billing_period': request.billing_period,
            'source': 'inline_checkout'
        }

        subscription_params = {
            'customer': customer_id,
            'items': [{'price': price_id}],
            'payment_behavior': 'default_incomplete',
            'payment_settings': {
                'save_default_payment_method': 'on_subscription',
                'payment_method_types': ['card'],  # Cards only, matching normal checkout
            },
            'expand': ['latest_invoice.payment_intent'],
            'metadata': metadata
        }

        # Apply promo code if found
        if promotion_code_id:
            subscription_params['promotion_code'] = promotion_code_id

        subscription = await stripe.Subscription.create_async(**subscription_params)

        # Use dict-style access for Stripe objects (consistent with codebase pattern)
        latest_invoice = subscription['latest_invoice']
        payment_intent = latest_invoice.get('payment_intent') if latest_invoice else None

        # If 100% discount (no payment needed), payment_intent is None
        if payment_intent is None:
            # Subscription is already active, no payment required
            logger.info(f"[INLINE CHECKOUT] No payment required (100% discount) - subscription {subscription['id']} is active")

            return {
                'subscription_id': subscription['id'],
                'tier_key': request.tier_key,
                'amount': 0,
                'currency': latest_invoice.get('currency', 'usd') if latest_invoice else 'usd',
                'no_payment_required': True
            }

        client_secret = payment_intent['client_secret']
        logger.info(f"[INLINE CHECKOUT] Created subscription {subscription['id']} for account {account_id}")

        return {
            'client_secret': client_secret,
            'subscription_id': subscription['id'],
            'tier_key': request.tier_key,
            'amount': latest_invoice['amount_due'],
            'currency': latest_invoice['currency']
        }

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"[BILLING] Error creating inline checkout: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/confirm-inline-checkout")
async def confirm_inline_checkout(
    request: dict,
    account_id: str = Depends(verify_and_get_user_id_from_jwt)
) -> Dict:
    """
    Confirm inline checkout after payment succeeds.
    Updates the tier in the database and grants credits immediately without waiting for webhook.
    """
    try:
        from ..subscriptions.repositories.credit_account import CreditAccountRepository
        from ..subscriptions.services.lifecycle_service import LifecycleService
        from ..shared.config import get_tier_by_name
        from datetime import datetime, timezone

        logger.info(f"[INLINE CHECKOUT] Received confirm request: {request}")

        subscription_id = request.get('subscription_id')
        tier_key = request.get('tier_key')
        payment_intent_id = request.get('payment_intent_id')

        logger.info(f"[INLINE CHECKOUT] Parsed: subscription_id={subscription_id}, tier_key={tier_key}, payment_intent_id={payment_intent_id}")

        if not subscription_id or not tier_key:
            raise HTTPException(status_code=400, detail="subscription_id and tier_key required")

        # If payment_intent_id provided, verify it directly (most reliable)
        payment_succeeded = False
        if payment_intent_id:
            try:
                pi = await stripe.PaymentIntent.retrieve_async(payment_intent_id)
                logger.info(f"[INLINE CHECKOUT] Direct PaymentIntent check: {pi['id']} status={pi['status']}")
                if pi['status'] == 'succeeded':
                    payment_succeeded = True
            except Exception as e:
                logger.warning(f"[INLINE CHECKOUT] Could not verify PaymentIntent {payment_intent_id}: {e}")

        # Fetch subscription for metadata verification and fallback payment check
        subscription = await stripe.Subscription.retrieve_async(
            subscription_id,
            expand=['latest_invoice.payment_intent']
        )

        # Verify subscription metadata matches (use dict-style access for Stripe objects)
        sub_metadata = subscription.get('metadata', {}) or {}
        sub_account_id = sub_metadata.get('account_id')
        if sub_account_id != account_id:
            logger.warning(f"[INLINE CHECKOUT] Account mismatch: subscription has {sub_account_id}, request from {account_id}")
            raise HTTPException(status_code=403, detail="Subscription does not belong to this account")

        # Fallback: check subscription status if direct PI check didn't confirm
        if not payment_succeeded:
            latest_invoice = subscription.get('latest_invoice')
            payment_intent = latest_invoice.get('payment_intent') if latest_invoice else None
            payment_succeeded = (
                subscription['status'] == 'active' or
                (payment_intent and payment_intent.get('status') == 'succeeded')
            )
            logger.info(f"[INLINE CHECKOUT] Subscription check: status={subscription['status']}, invoice PI status={payment_intent.get('status') if payment_intent else 'N/A'}")

        if not payment_succeeded:
            raise HTTPException(status_code=400, detail=f"Payment not confirmed. Subscription status: {subscription['status']}")

        # Cancel old subscription if this was an upgrade (only after payment confirmed)
        previous_subscription_id = sub_metadata.get('previous_subscription_id')
        if previous_subscription_id:
            try:
                await stripe.Subscription.cancel_async(previous_subscription_id)
                logger.info(f"[INLINE CHECKOUT] Cancelled previous subscription {previous_subscription_id} after upgrade payment confirmed")
            except Exception as e:
                # Log but don't fail - old subscription might already be cancelled
                logger.warning(f"[INLINE CHECKOUT] Could not cancel previous subscription {previous_subscription_id}: {e}")

        # Get tier info
        tier = get_tier_by_name(tier_key)
        if not tier:
            raise HTTPException(status_code=400, detail="Invalid tier")

        # Get billing period from metadata (Stripe objects use attribute access)
        billing_period = sub_metadata.get('billing_period', 'monthly')
        plan_type = 'yearly' if billing_period == 'yearly' else 'monthly'

        # Calculate billing anchor and next grant date (use dict-style access)
        billing_anchor = datetime.fromtimestamp(subscription['current_period_start'], tz=timezone.utc)

        # Grant credits for the new subscription
        lifecycle_service = LifecycleService()
        new_tier = {
            'name': tier.name,
            'credits': float(tier.monthly_credits)
        }

        logger.info(f"[INLINE CHECKOUT] Granting {tier.monthly_credits} credits for {tier.display_name}")
        await lifecycle_service.grant_subscription_credits(account_id, new_tier, billing_anchor, is_tier_upgrade=False)

        # Calculate next credit grant date (important for renewal system)
        next_grant_date = lifecycle_service.calculate_next_credit_grant(
            plan_type, billing_anchor, subscription['current_period_end']
        )

        # Update credit account with ALL required fields
        credit_repo = CreditAccountRepository()
        await credit_repo.update_credit_account(account_id, {
            'tier': tier_key,
            'plan_type': plan_type,
            'stripe_subscription_id': subscription_id,
            'billing_cycle_anchor': billing_anchor.isoformat(),
            'next_credit_grant': next_grant_date.isoformat(),
            'trial_status': 'none',
            'last_grant_date': billing_anchor.isoformat()
        })

        logger.info(f"[INLINE CHECKOUT] Confirmed subscription {subscription_id} for account {account_id}, tier={tier_key}, credits granted")

        # Invalidate ALL billing caches to ensure tier shows correctly immediately
        # This includes subscription_tier cache used by get_user_subscription_tier
        await invalidate_all_billing_caches(account_id)

        return {
            'success': True,
            'tier': tier_key,
            'credits_granted': float(tier.monthly_credits),
            'message': f'Successfully subscribed to {tier.display_name}'
        }

    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"[BILLING] Error confirming inline checkout: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/create-portal-session")
async def create_portal_session(
    request: CreatePortalSessionRequest,
    account_id: str = Depends(verify_and_get_user_id_from_jwt)
) -> Dict:
    """Create a Stripe customer portal session."""
    try:
        from ..subscriptions.handlers.portal import PortalHandler
        result = await PortalHandler.create_portal_session(account_id, request.return_url)
        return result
    except Exception as e:
        logger.error(f"[BILLING] Error creating portal session: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/sync-subscription")
async def sync_subscription(
    account_id: str = Depends(verify_and_get_user_id_from_jwt)
) -> Dict:
    """Sync subscription data from Stripe."""
    try:
        result = await subscription_service.sync_subscription(account_id)
        await invalidate_account_state_cache(account_id)
        return result
    except Exception as e:
        logger.error(f"[BILLING] Error syncing subscription: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/cancel-subscription")
async def cancel_subscription(
    request: CancelSubscriptionRequest,
    account_id: str = Depends(verify_and_get_user_id_from_jwt)
) -> Dict:
    """Cancel the current subscription."""
    try:
        result = await subscription_service.cancel_subscription(account_id, request.feedback)
        await invalidate_account_state_cache(account_id)
        return result
    except Exception as e:
        logger.error(f"[BILLING] Error cancelling subscription: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/reactivate-subscription")
async def reactivate_subscription(
    account_id: str = Depends(verify_and_get_user_id_from_jwt)
) -> Dict:
    """Reactivate a cancelled subscription."""
    try:
        result = await subscription_service.reactivate_subscription(account_id)
        await invalidate_account_state_cache(account_id)
        return result
    except Exception as e:
        logger.error(f"[BILLING] Error reactivating subscription: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/schedule-downgrade") 
async def schedule_downgrade(
    request: dict,  
    account_id: str = Depends(verify_and_get_user_id_from_jwt)
) -> Dict:
    """Schedule a tier downgrade at the end of the current billing period."""
    try:
        target_tier_key = request.get('target_tier_key')
        commitment_type = request.get('commitment_type')
        
        if not target_tier_key:
            raise HTTPException(status_code=400, detail="target_tier_key is required")
        
        result = await subscription_service.schedule_tier_downgrade(
            account_id=account_id,
            target_tier_key=target_tier_key,
            commitment_type=commitment_type
        )
        
        await invalidate_account_state_cache(account_id)
        return result
    except Exception as e:
        logger.error(f"[BILLING] Error scheduling downgrade: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/cancel-scheduled-change")
async def cancel_scheduled_change(
    account_id: str = Depends(verify_and_get_user_id_from_jwt)
) -> Dict:
    """Cancel a scheduled tier change (e.g., cancel a pending downgrade)."""
    try:
        result = await subscription_service.cancel_scheduled_change(account_id)
        await invalidate_account_state_cache(account_id)
        return result
    except Exception as e:
        logger.error(f"[BILLING] Error cancelling scheduled change: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/proration-preview")
async def preview_proration(
    new_price_id: str = Query(..., description="The price ID to change to"),
    account_id: str = Depends(verify_and_get_user_id_from_jwt)
) -> Dict:
    """Preview proration for a subscription change."""
    try:
        return {
            'preview': {
                'amount_due': 0,
                'proration_amount': 0,
                'next_invoice_total': 0
            },
            'new_price_id': new_price_id,
            'message': 'Proration preview generated'
        }
    except Exception as e:
        logger.error(f"[BILLING] Error previewing proration: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/checkout-session/{session_id}")
async def get_checkout_session(
    session_id: str,
    account_id: str = Depends(verify_and_get_user_id_from_jwt)
) -> Dict:
    """
    Retrieve checkout session details from Stripe.
    Used to get actual transaction amounts after checkout completes (for analytics).
    Returns amount_total, discount, coupon, promotion code, and balance_transaction_id (txn_xxx).
    """
    try:
        # Validate session_id format (starts with cs_)
        if not session_id.startswith('cs_'):
            raise HTTPException(status_code=400, detail="Invalid session ID format")
        
        # Retrieve checkout session with expanded discounts and promotion code
        # Note: session.discounts contains promotion_code, not total_details.breakdown
        session = await stripe.checkout.Session.retrieve_async(
            session_id,
            expand=['discounts.promotion_code']
        )
        
        # Extract amounts
        amount_total = session.amount_total or 0  # In cents
        amount_subtotal = session.amount_subtotal or 0  # In cents (before discounts/tax)
        amount_discount = 0
        amount_tax = 0
        coupon_id = None
        coupon_name = None
        promotion_code = None  # The customer-facing code (e.g., "HEHE2020")
        balance_transaction_id = None  # txn_xxx for linking to Stripe balance
        
        # Get discount and tax amounts from total_details
        if session.total_details:
            amount_discount = session.total_details.amount_discount or 0
            amount_tax = session.total_details.amount_tax or 0
        
        # Get promotion code and coupon info from session.discounts
        # This is the correct way to get the customer-facing promotion code
        if session.discounts and len(session.discounts) > 0:
            discount = session.discounts[0]
            
            # Get promotion code (customer-facing code like "HEHE2020")
            if hasattr(discount, 'promotion_code') and discount.promotion_code:
                promo = discount.promotion_code
                if hasattr(promo, 'code'):
                    promotion_code = promo.code
                # Get coupon info from the promotion code's coupon
                if hasattr(promo, 'coupon') and promo.coupon:
                    coupon_id = promo.coupon.id
                    coupon_name = promo.coupon.name
            # Fallback: get coupon directly if no promotion code
            elif hasattr(discount, 'coupon') and discount.coupon:
                coupon_id = discount.coupon.id
                coupon_name = discount.coupon.name
        
        # Get balance_transaction_id (txn_xxx) - the Stripe balance transaction ID
        # Path depends on checkout mode:
        # - subscription: session β†’ subscription β†’ latest_invoice β†’ charge β†’ balance_transaction
        # - payment: session β†’ payment_intent β†’ latest_charge β†’ balance_transaction
        try:
            if session.subscription:
                # Subscription mode: get via subscription's latest invoice
                sub = await stripe.Subscription.retrieve_async(
                    session.subscription,
                    expand=['latest_invoice.charge.balance_transaction']
                )
                if (sub.latest_invoice and 
                    hasattr(sub.latest_invoice, 'charge') and sub.latest_invoice.charge and
                    hasattr(sub.latest_invoice.charge, 'balance_transaction') and sub.latest_invoice.charge.balance_transaction):
                    bt = sub.latest_invoice.charge.balance_transaction
                    if hasattr(bt, 'id'):
                        balance_transaction_id = bt.id
            elif session.payment_intent:
                # Payment mode: get via payment intent's latest charge
                pi = await stripe.PaymentIntent.retrieve_async(
                    session.payment_intent,
                    expand=['latest_charge.balance_transaction']
                )
                if (pi.latest_charge and 
                    hasattr(pi.latest_charge, 'balance_transaction') and pi.latest_charge.balance_transaction):
                    bt = pi.latest_charge.balance_transaction
                    if hasattr(bt, 'id'):
                        balance_transaction_id = bt.id
        except Exception as e:
            # Log but don't fail - balance_transaction is optional for analytics
            logger.warning(f"[BILLING] Could not retrieve balance_transaction: {e}")
        
        return {
            'session_id': session_id,
            'amount_total': amount_total,           # Final amount in cents (after discounts and tax)
            'amount_subtotal': amount_subtotal,     # Amount before discounts/tax in cents
            'amount_discount': amount_discount,     # Discount amount in cents
            'amount_tax': amount_tax,               # Tax amount in cents
            'currency': session.currency or 'usd',
            'coupon_id': coupon_id,                 # Internal Stripe coupon ID
            'coupon_name': coupon_name,             # Coupon display name
            'promotion_code': promotion_code,       # Customer-facing code (e.g., "HEHE2020")
            'balance_transaction_id': balance_transaction_id,  # txn_xxx for Stripe balance
            'status': session.status,
            'payment_status': session.payment_status
        }
        
    except stripe.error.InvalidRequestError as e:
        logger.warning(f"[BILLING] Invalid checkout session request: {e}")
        raise HTTPException(status_code=404, detail="Checkout session not found")
    except Exception as e:
        logger.error(f"[BILLING] Error retrieving checkout session: {e}")
        raise HTTPException(status_code=500, detail=str(e))