diff --git a/app/api/routes/wallet.py b/app/api/routes/wallet.py index 3b49c22..7bb89f5 100644 --- a/app/api/routes/wallet.py +++ b/app/api/routes/wallet.py @@ -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( diff --git a/app/api/routes/webhooks.py b/app/api/routes/webhooks.py index 8a4d071..cd54c81 100644 --- a/app/api/routes/webhooks.py +++ b/app/api/routes/webhooks.py @@ -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, @@ -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":