fix(backend): Fix broken top-up flow (#9391)

https://github.com/Significant-Gravitas/AutoGPT/pull/9296 caused these
errors:

<img width="440" alt="image"
src="https://github.com/user-attachments/assets/f100619b-1a4c-44fb-b961-e74210894a91"
/>

<img width="411" alt="image"
src="https://github.com/user-attachments/assets/0c1a8aff-b14f-4ea8-8ae9-b8928c8511cf"
/>



### 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:
  <!-- Put your test plan here: -->
  - [ ] ...

<details>
  <summary>Example test plan</summary>
  
  - [ ] 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
</details>

#### 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**)

<details>
  <summary>Examples of configuration changes</summary>

  - Changing ports
  - Adding new services that need to communicate with each other
  - Secrets or environment variable changes
  - New or infrastructure changes such as databases
</details>
pull/9392/head
Zamil Majdy 2025-02-03 11:58:48 +01:00 committed by GitHub
parent 74b8b45e0a
commit 3de982792e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 11 deletions

View File

@ -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)

View File

@ -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]: