async def isLoggedIn(token: UserInfo = Depends(oauth2_scheme)): if token: id = verify_token(token) if id: return JSONResponse(status_code=status.HTTP_200_OK) else: raise credentials_exception else: raise credentials_exception
async def get_current_user( token: UserInfo = Depends(oauth2_scheme), db: AsyncIOMotorClient = Depends(get_database), ): if token: id = verify_token(token) else: raise credentials_exception if (user := await db[database_name] [user_collection].find_one({"_id": id}, { "_id": False, "hashed_password": False })) is not None: return user
async def portal( token: UserInfo = Depends(oauth2_scheme), db: AsyncIOMotorClient = Depends(get_database), ): customer_id = None if token: id = verify_token(token) user = await find_user(user_id=id, error_return_status=404, db=db) customer_id = user["customer"]["id"] else: raise credentials_exception if customer_id: session = stripe.billing_portal.Session.create( customer=customer_id, return_url=MAIN_URL, ) return {"url": session.url} else: return RedirectResponse(url=BillingURL.plans_page, status_code=302)
async def update_user( newUserData: UpdateUser, token: UserInfo = Depends(oauth2_scheme), db: AsyncIOMotorClient = Depends(get_database), ): if token: id = verify_token(token) else: raise credentials_exception if (updateUser := await db[database_name] [user_collection].find_one_and_update( {"_id": id}, { "$set": { "info.first_name": newUserData.first_name, "info.last_name": newUserData.last_name, } }, )) is not None: return JSONResponse(status_code=status.HTTP_200_OK)
async def get_rules( token, db: AsyncIOMotorClient = Depends(get_database), ): rules = None if not token: rules = DEFAULT_TIER.get_hidden_fields() # free users else: user_id = verify_token(token) if (user := await db[database_name][user_collection].find_one( {"_id": user_id}, {"subscription.price": True})) is not None: sub_tier = get_tier(user["subscription"]["price"]) if not sub_tier: raise HTTPException( status_code=404, detail= f"Subscription is invalid or not found for this user!", ) for tier in TIERS: if sub_tier.id == tier.id: rules = tier.get_hidden_fields() or None return rules
async def billing_session( plan: Item, token: UserInfo = Depends(oauth2_scheme), db: AsyncIOMotorClient = Depends(get_database), ): if token: id = verify_token(token) user = await find_user(user_id=id, error_return_status=404, db=db) email = user["info"]["email"] customer = user["customer"]["id"] subscription = user["subscription"]["plan"] else: raise credentials_exception if email: if get_tier(price_id=plan.id): try: if not customer: # new user no customer id session = stripe.checkout.Session.create( success_url=BillingURL.success, cancel_url=BillingURL.plans_page, payment_method_types=["card"], mode="subscription", # allow_promotion_codes= COUPON_ID if COUPON_ID else None, <TO-DO> add conditional customer_email=email, subscription_data={"trial_period_days": 3}, discounts=[{ "coupon": COUPON_ID }] if COUPON_ID else None, line_items=[{ "price": plan.id.value, # For metered billing, do not pass quantity "quantity": 1, }], ) elif customer: # user has customer session = stripe.checkout.Session.create( success_url=BillingURL.success, cancel_url=BillingURL.plans_page, payment_method_types=["card"], mode="subscription", allow_promotion_codes=True, customer=customer, line_items=[{ "price": plan.id.value, # For metered billing, do not pass quantity "quantity": 1, }], ) # elif customer and subscription: # return RedirectResponse(url="/billing/portal", status_code=302) except: raise payment_system_exception return {"url": session.url} else: raise HTTPException(status_code=404, detail="Plan not found") raise HTTPException(status_code=404, detail=f"User not found")