Spaces:
Paused
Paused
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| import os | |
| import requests | |
| app = FastAPI() | |
| # Environment secrets | |
| CHARGILY_API_KEY = os.getenv("CHARGILY_API_KEY") | |
| SUPABASE_URL = os.getenv("SUPABASE_URL") | |
| SUPABASE_SERVICE_ROLE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY") | |
| class PaymentRequest(BaseModel): | |
| user_id: str | |
| amount: int | |
| currency: str = "DZD" | |
| plan: str = "paid" | |
| def create_payment(req: PaymentRequest): | |
| try: | |
| payload = { | |
| "amount": req.amount, | |
| "currency": req.currency, | |
| "description": f"User {req.user_id} purchase", | |
| "metadata": {"user_id": req.user_id, "plan": req.plan}, | |
| "return_url": "https://aisool.com/payment-success" | |
| } | |
| headers = { | |
| "Authorization": f"Bearer {CHARGILY_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| r = requests.post("https://api.chargily.com/v1/payment-links", json=payload, headers=headers) | |
| if not r.ok: | |
| raise HTTPException(status_code=500, detail=r.text) | |
| # Return only the payment_url | |
| payment_link = r.json().get("payment_url") | |
| return {"payment_url": payment_link} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) |