Spaces:
Running
Running
File size: 1,642 Bytes
6b3ee8d | 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 | from supabase import create_client, Client
SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
async def fetch_subscription(email: str):
# Fetch user
user_res = supabase.table("auth.users").select("*").eq("email", email).execute()
if not user_res.data:
return None
user = user_res.data[0]
# Fetch Stripe customer
cust_res = supabase.table("stripe.customers").select("*").eq("email", email).execute()
if not cust_res.data:
return {
"email": email,
"signed_up": user["created_at"],
"subscription": None
}
customer = cust_res.data[0]
# Fetch subscriptions
sub_res = (
supabase.table("stripe.subscriptions")
.select("*")
.eq("customer", customer["id"])
.in_("status", ["active", "trialing", "past_due"])
.execute()
)
if not sub_res.data:
return {
"email": email,
"signed_up": user["created_at"],
"subscription": None
}
subs = []
for s in sub_res.data:
price_id = (
s["items"]["data"][0]["price"]["id"]
if s.get("items") and s["items"].get("data")
else None
)
subs.append({
"status": s["status"],
"subscription_id": s["id"],
"current_period_end": s["current_period_end"],
"price_id": price_id
})
return {
"email": email,
"signed_up": user["created_at"],
"subscription": subs
}
|