Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import logging
|
| 2 |
-
from fastapi import FastAPI, UploadFile, File, HTTPException, Form
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from fastapi.responses import FileResponse, JSONResponse
|
| 5 |
import requests
|
|
@@ -12,6 +12,7 @@ from typing import List
|
|
| 12 |
import io
|
| 13 |
import razorpay
|
| 14 |
from razorpay.errors import SignatureVerificationError
|
|
|
|
| 15 |
|
| 16 |
# Configure logging
|
| 17 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -23,7 +24,7 @@ app = FastAPI(title="Kling AI Multi-Image Generator API with Razorpay")
|
|
| 23 |
# Enable CORS for the frontend
|
| 24 |
app.add_middleware(
|
| 25 |
CORSMiddleware,
|
| 26 |
-
allow_origins=["https://hivili.web.app"], #
|
| 27 |
allow_credentials=True,
|
| 28 |
allow_methods=["*"],
|
| 29 |
allow_headers=["*"],
|
|
@@ -36,10 +37,15 @@ API_BASE_URL = "https://api-singapore.klingai.com"
|
|
| 36 |
CREATE_TASK_ENDPOINT = f"{API_BASE_URL}/v1/images/multi-image2image"
|
| 37 |
|
| 38 |
# ===== RAZORPAY CONFIGURATION =====
|
| 39 |
-
RAZORPAY_KEY_ID = os.getenv("RAZORPAY_KEY_ID")
|
| 40 |
-
RAZORPAY_KEY_SECRET = os.getenv("RAZORPAY_KEY_SECRET")
|
| 41 |
razorpay_client = razorpay.Client(auth=(RAZORPAY_KEY_ID, RAZORPAY_KEY_SECRET))
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# ===== AUTHENTICATION =====
|
| 44 |
def generate_jwt_token():
|
| 45 |
"""Generate JWT token for API authentication"""
|
|
@@ -118,12 +124,12 @@ def check_task_status(task_id: str):
|
|
| 118 |
raise HTTPException(status_code=500, detail=f"Status check failed: {str(e)}")
|
| 119 |
|
| 120 |
# ===== RAZORPAY FUNCTIONS =====
|
| 121 |
-
def create_razorpay_order(amount: int
|
| 122 |
"""Create a Razorpay order"""
|
| 123 |
try:
|
| 124 |
order_data = {
|
| 125 |
-
"amount": amount * 100, #
|
| 126 |
-
"currency":
|
| 127 |
"payment_capture": 1 # Auto-capture payment
|
| 128 |
}
|
| 129 |
order = razorpay_client.order.create(data=order_data)
|
|
@@ -214,16 +220,15 @@ async def generate_image_endpoint(
|
|
| 214 |
logger.error(f"Error in /generate: {str(e)}")
|
| 215 |
raise HTTPException(status_code=500, detail=str(e))
|
| 216 |
|
| 217 |
-
@app.post("/create-order")
|
| 218 |
-
async def
|
| 219 |
-
"""Create a Razorpay order
|
| 220 |
if not RAZORPAY_KEY_ID or not RAZORPAY_KEY_SECRET:
|
| 221 |
raise HTTPException(status_code=500, detail="Razorpay configuration missing")
|
| 222 |
-
|
| 223 |
try:
|
| 224 |
order = create_razorpay_order(amount)
|
| 225 |
return JSONResponse(content={
|
| 226 |
-
"
|
| 227 |
"amount": order["amount"],
|
| 228 |
"currency": order["currency"],
|
| 229 |
"key_id": RAZORPAY_KEY_ID
|
|
@@ -232,21 +237,22 @@ async def create_order(amount: int = Form(...)):
|
|
| 232 |
logger.error(f"Error creating order: {str(e)}")
|
| 233 |
raise HTTPException(status_code=500, detail=str(e))
|
| 234 |
|
| 235 |
-
@app.post("/verify-payment")
|
| 236 |
-
async def
|
| 237 |
razorpay_order_id: str = Form(...),
|
| 238 |
razorpay_payment_id: str = Form(...),
|
| 239 |
-
razorpay_signature: str = Form(...)
|
|
|
|
| 240 |
):
|
| 241 |
"""Verify Razorpay payment signature"""
|
| 242 |
try:
|
| 243 |
is_valid = verify_payment_signature(razorpay_order_id, razorpay_payment_id, razorpay_signature)
|
| 244 |
if is_valid:
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
return JSONResponse(content={"
|
| 248 |
else:
|
| 249 |
-
|
| 250 |
except Exception as e:
|
| 251 |
logger.error(f"Error verifying payment: {str(e)}")
|
| 252 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 1 |
import logging
|
| 2 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException, Form
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from fastapi.responses import FileResponse, JSONResponse
|
| 5 |
import requests
|
|
|
|
| 12 |
import io
|
| 13 |
import razorpay
|
| 14 |
from razorpay.errors import SignatureVerificationError
|
| 15 |
+
from supabase import create_client, Client
|
| 16 |
|
| 17 |
# Configure logging
|
| 18 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 24 |
# Enable CORS for the frontend
|
| 25 |
app.add_middleware(
|
| 26 |
CORSMiddleware,
|
| 27 |
+
allow_origins=["https://hivili.web.app", "https://your-lovable-app.lovable.dev"], # Replace with your Lovable.dev URL
|
| 28 |
allow_credentials=True,
|
| 29 |
allow_methods=["*"],
|
| 30 |
allow_headers=["*"],
|
|
|
|
| 37 |
CREATE_TASK_ENDPOINT = f"{API_BASE_URL}/v1/images/multi-image2image"
|
| 38 |
|
| 39 |
# ===== RAZORPAY CONFIGURATION =====
|
| 40 |
+
RAZORPAY_KEY_ID = os.getenv("RAZORPAY_KEY_ID")
|
| 41 |
+
RAZORPAY_KEY_SECRET = os.getenv("RAZORPAY_KEY_SECRET")
|
| 42 |
razorpay_client = razorpay.Client(auth=(RAZORPAY_KEY_ID, RAZORPAY_KEY_SECRET))
|
| 43 |
|
| 44 |
+
# ===== SUPABASE CONFIGURATION =====
|
| 45 |
+
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 46 |
+
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
|
| 47 |
+
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY) if SUPABASE_URL and SUPABASE_KEY else None
|
| 48 |
+
|
| 49 |
# ===== AUTHENTICATION =====
|
| 50 |
def generate_jwt_token():
|
| 51 |
"""Generate JWT token for API authentication"""
|
|
|
|
| 124 |
raise HTTPException(status_code=500, detail=f"Status check failed: {str(e)}")
|
| 125 |
|
| 126 |
# ===== RAZORPAY FUNCTIONS =====
|
| 127 |
+
def create_razorpay_order(amount: int):
|
| 128 |
"""Create a Razorpay order"""
|
| 129 |
try:
|
| 130 |
order_data = {
|
| 131 |
+
"amount": amount * 100, # Convert INR to paise
|
| 132 |
+
"currency": "INR",
|
| 133 |
"payment_capture": 1 # Auto-capture payment
|
| 134 |
}
|
| 135 |
order = razorpay_client.order.create(data=order_data)
|
|
|
|
| 220 |
logger.error(f"Error in /generate: {str(e)}")
|
| 221 |
raise HTTPException(status_code=500, detail=str(e))
|
| 222 |
|
| 223 |
+
@app.post("/create-razorpay-order")
|
| 224 |
+
async def create_razorpay_order(amount: int = Form(...)):
|
| 225 |
+
"""Create a Razorpay order"""
|
| 226 |
if not RAZORPAY_KEY_ID or not RAZORPAY_KEY_SECRET:
|
| 227 |
raise HTTPException(status_code=500, detail="Razorpay configuration missing")
|
|
|
|
| 228 |
try:
|
| 229 |
order = create_razorpay_order(amount)
|
| 230 |
return JSONResponse(content={
|
| 231 |
+
"id": order["id"],
|
| 232 |
"amount": order["amount"],
|
| 233 |
"currency": order["currency"],
|
| 234 |
"key_id": RAZORPAY_KEY_ID
|
|
|
|
| 237 |
logger.error(f"Error creating order: {str(e)}")
|
| 238 |
raise HTTPException(status_code=500, detail=str(e))
|
| 239 |
|
| 240 |
+
@app.post("/verify-razorpay-payment")
|
| 241 |
+
async def verify_razorpay_payment(
|
| 242 |
razorpay_order_id: str = Form(...),
|
| 243 |
razorpay_payment_id: str = Form(...),
|
| 244 |
+
razorpay_signature: str = Form(...),
|
| 245 |
+
user_id: str = Form(None) # Optional for Supabase
|
| 246 |
):
|
| 247 |
"""Verify Razorpay payment signature"""
|
| 248 |
try:
|
| 249 |
is_valid = verify_payment_signature(razorpay_order_id, razorpay_payment_id, razorpay_signature)
|
| 250 |
if is_valid:
|
| 251 |
+
if user_id and supabase:
|
| 252 |
+
supabase.table("users").update({"is_premium": True}).eq("user_id", user_id).execute()
|
| 253 |
+
return JSONResponse(content={"success": True})
|
| 254 |
else:
|
| 255 |
+
return JSONResponse(content={"success": False}, status_code=400)
|
| 256 |
except Exception as e:
|
| 257 |
logger.error(f"Error verifying payment: {str(e)}")
|
| 258 |
raise HTTPException(status_code=500, detail=str(e))
|