from os import getenv from fastapi import APIRouter, HTTPException from .paypal import create_paypal_order, capture_paypal_order from supabase import create_client, Client from ..constants import SUPABASE_URL, SUPABASE_KEY # Initialize Supabase supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY) # Define router router = APIRouter() @router.post("/create-paypal-payment/") def create_payment(amount: float, currency: str = "USD"): """ Create a PayPal payment and return approval URL. """ try: payment = create_paypal_order(amount, currency) return {"approval_url": payment["approval_url"]} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @router.post("/capture-paypal-payment/") def capture_payment(order_id: str, user_id: str, country: str): """ Capture the PayPal payment and unlock the country in Supabase. """ try: # Capture payment capture_response = capture_paypal_order(order_id) if capture_response.get("status") == "COMPLETED": # Update Supabase to unlock the country for the user supabase.table("unlocked_countries").insert({ "user_id": user_id, "country": country, "paid": True, }).execute() return {"status": "Payment captured and country unlocked successfully"} else: raise HTTPException(status_code=400, detail="Payment not completed") except Exception as e: raise HTTPException(status_code=500, detail=str(e))