anyway/backend/src/payments/payment_routes.py
Helldragon67 3a9ef4e7d3
Some checks failed
Run testing on the backend code / Build (pull_request) Has been cancelled
Build and deploy the backend to staging / Deploy to staging (pull_request) Has been cancelled
Build and deploy the backend to staging / Build and push image (pull_request) Has been cancelled
Run linting on the backend code / Build (pull_request) Has been cancelled
starting to implement paywall logic
2025-02-11 17:19:03 +01:00

50 lines
1.5 KiB
Python

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))