Manually sync the user's subscription status from Stripe. Call this when: - User returns from Stripe Customer Portal - User reports their subscription status is wrong - After any subscription change to ensure consistency Returns the synced subscription state.
(current_user: AuthUser)
| 1122 | summary="Sync subscription status from Stripe" |
| 1123 | ) |
| 1124 | async def sync_subscription_endpoint(current_user: AuthUser): |
| 1125 | """ |
| 1126 | Manually sync the user's subscription status from Stripe. |
| 1127 | |
| 1128 | Call this when: |
| 1129 | - User returns from Stripe Customer Portal |
| 1130 | - User reports their subscription status is wrong |
| 1131 | - After any subscription change to ensure consistency |
| 1132 | |
| 1133 | Returns the synced subscription state. |
| 1134 | """ |
| 1135 | # Find user's Stripe customer ID (prefer Auth0 metadata, fall back to email lookup) |
| 1136 | stripe_customer_id = None |
| 1137 | if hasattr(current_user, 'app_metadata') and isinstance(current_user.app_metadata, dict): |
| 1138 | stripe_customer_id = current_user.app_metadata.get("stripe_customer_id") |
| 1139 | |
| 1140 | if not stripe_customer_id and hasattr(current_user, 'email') and current_user.email: |
| 1141 | try: |
| 1142 | customers = await asyncio.to_thread(_list_personal_customers, current_user.email) |
| 1143 | if customers: |
| 1144 | stripe_customer_id = customers[0].id |
| 1145 | except Exception as e: |
| 1146 | logger.warning(f"Stripe customer lookup by email failed for {current_user.id}: {e}") |
| 1147 | |
| 1148 | if not stripe_customer_id: |
| 1149 | logger.info(f"User {current_user.id} requested sync but no Stripe customer found") |
| 1150 | return { |
| 1151 | "status": "success", |
| 1152 | "tier": "Free", |
| 1153 | "message": "No subscription found" |
| 1154 | } |
| 1155 | |
| 1156 | result = await sync_user_from_stripe(stripe_customer_id) |
| 1157 | return result |
| 1158 | |
| 1159 | |
| 1160 | @payments_router.post( |
nothing calls this directly
no test coverage detected