Spaces:
Sleeping
Sleeping
File size: 3,746 Bytes
dfa3aa6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | # Add these endpoints to your FastAPI backend (main.py or similar)
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional
import time
app = FastAPI()
# In-memory wallet balance (replace with database in production)
wallet_data = {
"balance": 10000.00,
"transactions": []
}
class AddMoneyRequest(BaseModel):
payment_id: str
amount: float
class WithdrawRequest(BaseModel):
amount: float
@app.get("/api/wallet/balance")
async def get_wallet_balance():
"""Get current wallet balance"""
return {
"balance": wallet_data["balance"],
"status": "success"
}
@app.post("/api/wallet/add")
async def add_money(request: AddMoneyRequest):
"""
Add money to wallet after successful Razorpay payment
In production, you should:
1. Verify the payment with Razorpay API using payment_id
2. Check payment status and amount
3. Store transaction in database
"""
# TODO: Verify payment with Razorpay
# For now, we'll trust the frontend (NOT recommended for production)
if request.amount <= 0:
raise HTTPException(status_code=400, detail="Invalid amount")
# Add money to wallet
wallet_data["balance"] += request.amount
# Store transaction
transaction = {
"type": "credit",
"amount": request.amount,
"payment_id": request.payment_id,
"timestamp": time.time(),
"status": "success"
}
wallet_data["transactions"].append(transaction)
return {
"status": "success",
"message": f"₹{request.amount} added successfully",
"new_balance": wallet_data["balance"],
"transaction": transaction
}
@app.post("/api/wallet/withdraw")
async def withdraw_money(request: WithdrawRequest):
"""
Withdraw money from wallet
In production, you should:
1. Verify user authentication
2. Check withdrawal limits
3. Process actual bank transfer
4. Store transaction in database
"""
if request.amount <= 0:
raise HTTPException(status_code=400, detail="Invalid amount")
# Check sufficient balance
if wallet_data["balance"] < request.amount:
raise HTTPException(status_code=400, detail="Insufficient balance")
# Deduct money from wallet
wallet_data["balance"] -= request.amount
# Store transaction
transaction = {
"type": "debit",
"amount": request.amount,
"timestamp": time.time(),
"status": "success"
}
wallet_data["transactions"].append(transaction)
return {
"status": "success",
"message": f"₹{request.amount} withdrawn successfully",
"new_balance": wallet_data["balance"],
"transaction": transaction
}
@app.get("/api/wallet/transactions")
async def get_transactions():
"""Get all wallet transactions"""
return {
"transactions": wallet_data["transactions"],
"status": "success"
}
# Example of how to verify Razorpay payment (add this to your add_money endpoint)
"""
import razorpay
razorpay_client = razorpay.Client(auth=("YOUR_KEY_ID", "YOUR_KEY_SECRET"))
def verify_razorpay_payment(payment_id: str, amount: float):
try:
# Fetch payment details
payment = razorpay_client.payment.fetch(payment_id)
# Verify payment status
if payment['status'] != 'captured':
return False
# Verify amount (Razorpay stores in paise)
if payment['amount'] != int(amount * 100):
return False
return True
except Exception as e:
print(f"Payment verification failed: {e}")
return False
"""
|