Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions app/api/routes/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ async def get_wallet_balance(
return WalletResponse(balance=Decimal("0"), blocked=False, currency="USD", total_spent=Decimal("0"), total_earned=Decimal("0"))

result = await db.execute(select(func.sum(UsageTracker.cost)).where(UsageTracker.user_id == user.id, UsageTracker.updated_at.is_not(None), UsageTracker.billable))
total_spent = result.scalar_one_or_none() or "0"
total_spent = result.scalar_one_or_none() or Decimal("0")
result = await db.execute(select(func.sum(StripePayment.amount)).where(StripePayment.user_id == user.id, StripePayment.status == "completed"))
total_earned = result.scalar_one_or_none() or "0"
total_earned = result.scalar_one_or_none() or 0
# Convert cents to dollars for USD
total_earned = Decimal(total_earned / 100.0)

return WalletResponse(**wallet, total_spent=Decimal(total_spent), total_earned=Decimal(total_earned))
return WalletResponse(**wallet, total_spent=total_spent, total_earned=total_earned)

@router.get("/balance/clerk", response_model=WalletResponse)
async def get_wallet_balance_clerk(
Expand Down
10 changes: 8 additions & 2 deletions app/api/routes/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,13 @@ async def handle_payment_succeeded(event: dict, db: AsyncSession):

# update the corresponding StripePayment db record and return the user id
result = await db.execute(
update(StripePayment).where(StripePayment.id == session_id).values(
update(StripePayment)
.where(
StripePayment.id == session_id,
# Only update if the status is not completed
StripePayment.status != "completed",
)
.values(
status = status,
amount = amount,
currency = currency,
Expand All @@ -326,7 +332,7 @@ async def handle_payment_succeeded(event: dict, db: AsyncSession):
)
user_id = result.scalar_one_or_none()
if not user_id:
logger.warning(f"Stripe payment not found for id {id}")
logger.warning(f"Updated stripe payment not found for session: {session_id} with status {status} and payment_status {payment_status}")
return

if status != "completed":
Expand Down
Loading