From 3de982792e5eedc25301136875acb191f40db45b Mon Sep 17 00:00:00 2001 From: Zamil Majdy Date: Mon, 3 Feb 2025 11:58:48 +0100 Subject: [PATCH] fix(backend): Fix broken top-up flow (#9391) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/Significant-Gravitas/AutoGPT/pull/9296 caused these errors: image image ### Changes 🏗️ Removed customer email & return_url. ### Checklist 📋 #### For code changes: - [ ] I have clearly listed my changes in the PR description - [ ] I have made a test plan - [ ] I have tested my changes according to the test plan: - [ ] ...
Example test plan - [ ] Create from scratch and execute an agent with at least 3 blocks - [ ] Import an agent from file upload, and confirm it executes correctly - [ ] Upload agent to marketplace - [ ] Import an agent from marketplace and confirm it executes correctly - [ ] Edit an agent from monitor, and confirm it executes correctly
#### For configuration changes: - [ ] `.env.example` is updated or already compatible with my changes - [ ] `docker-compose.yml` is updated or already compatible with my changes - [ ] I have included a list of my configuration changes in the PR description (under **Changes**)
Examples of configuration changes - Changing ports - Adding new services that need to communicate with each other - Secrets or environment variable changes - New or infrastructure changes such as databases
--- autogpt_platform/backend/backend/data/credit.py | 10 +--------- autogpt_platform/backend/backend/data/user.py | 6 ++++-- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/autogpt_platform/backend/backend/data/credit.py b/autogpt_platform/backend/backend/data/credit.py index d59864271..7b91fef0b 100644 --- a/autogpt_platform/backend/backend/data/credit.py +++ b/autogpt_platform/backend/backend/data/credit.py @@ -399,16 +399,12 @@ class UserCredit(UserCreditBase): f"Top up amount must be at least 500 credits and multiple of 100 but is {amount}" ) - if not (user := await get_user_by_id(user_id)): - raise ValueError(f"User not found: {user_id}") - # Create checkout session # https://docs.stripe.com/checkout/quickstart?client=react # unit_amount param is always in the smallest currency unit (so cents for usd) # which is equal to amount of credits checkout_session = stripe.checkout.Session.create( customer=await get_stripe_customer_id(user_id), - customer_email=user.email, line_items=[ { "price_data": { @@ -422,13 +418,13 @@ class UserCredit(UserCreditBase): } ], mode="payment", + ui_mode="hosted", payment_intent_data={"setup_future_usage": "off_session"}, saved_payment_method_options={"payment_method_save": "enabled"}, success_url=settings.config.platform_base_url + "/marketplace/credits?topup=success", cancel_url=settings.config.platform_base_url + "/marketplace/credits?topup=cancel", - return_url=settings.config.platform_base_url + "/marketplace/credits", ) await self._add_transaction( @@ -602,8 +598,6 @@ def get_block_costs() -> dict[str, list[BlockCost]]: async def get_stripe_customer_id(user_id: str) -> str: user = await get_user_by_id(user_id) - if not user: - raise ValueError(f"User not found: {user_id}") if user.stripeCustomerId: return user.stripeCustomerId @@ -624,8 +618,6 @@ async def set_auto_top_up(user_id: str, config: AutoTopUpConfig): async def get_auto_top_up(user_id: str) -> AutoTopUpConfig: user = await get_user_by_id(user_id) - if not user: - raise ValueError("Invalid user ID") if not user.topUpConfig: return AutoTopUpConfig(threshold=0, amount=0) diff --git a/autogpt_platform/backend/backend/data/user.py b/autogpt_platform/backend/backend/data/user.py index 8602d0f3b..d7d6e87d0 100644 --- a/autogpt_platform/backend/backend/data/user.py +++ b/autogpt_platform/backend/backend/data/user.py @@ -34,9 +34,11 @@ async def get_or_create_user(user_data: dict) -> User: return User.model_validate(user) -async def get_user_by_id(user_id: str) -> Optional[User]: +async def get_user_by_id(user_id: str) -> User: user = await prisma.user.find_unique(where={"id": user_id}) - return User.model_validate(user) if user else None + if not user: + raise ValueError(f"User not found with ID: {user_id}") + return User.model_validate(user) async def create_default_user() -> Optional[User]: