Spaces:
Running
Running
Update subscriptions.py
Browse files- subscriptions.py +31 -2
subscriptions.py
CHANGED
|
@@ -12,7 +12,7 @@ SUPABASE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
|
|
| 12 |
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 13 |
POSTGRE_SECRET = os.getenv("POSTGRE_SECRET")
|
| 14 |
|
| 15 |
-
conn = psycopg.connect(POSTGRE_SECRET, row_factory=dict_row)
|
| 16 |
|
| 17 |
|
| 18 |
async def get_conn():
|
|
@@ -87,7 +87,36 @@ async def fetch_subscription(jwt: str):
|
|
| 87 |
except psycopg.OperationalError:
|
| 88 |
connection = psycopg.connect(POSTGRE_SECRET, row_factory=dict_row)
|
| 89 |
with connection.cursor() as cur:
|
| 90 |
-
cur.execute("""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
rows = cur.fetchall()
|
| 92 |
|
| 93 |
if not rows:
|
|
|
|
| 12 |
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 13 |
POSTGRE_SECRET = os.getenv("POSTGRE_SECRET")
|
| 14 |
|
| 15 |
+
conn = psycopg.connect(POSTGRE_SECRET, row_factory=dict_row, sslmode="verify-full", sslrootcert="prod-ca-2021.crt")
|
| 16 |
|
| 17 |
|
| 18 |
async def get_conn():
|
|
|
|
| 87 |
except psycopg.OperationalError:
|
| 88 |
connection = psycopg.connect(POSTGRE_SECRET, row_factory=dict_row)
|
| 89 |
with connection.cursor() as cur:
|
| 90 |
+
cur.execute("""
|
| 91 |
+
with cust as (
|
| 92 |
+
select id
|
| 93 |
+
from stripe.customers
|
| 94 |
+
where email = %s
|
| 95 |
+
),
|
| 96 |
+
subs as (
|
| 97 |
+
select
|
| 98 |
+
s.id as subscription_id,
|
| 99 |
+
s.status,
|
| 100 |
+
s.current_period_end,
|
| 101 |
+
s.items->'data'->0->'price'->>'id' as price_id
|
| 102 |
+
from stripe.subscriptions s
|
| 103 |
+
join cust on s.customer = cust.id
|
| 104 |
+
where s.status in ('active', 'trialing', 'past_due')
|
| 105 |
+
)
|
| 106 |
+
select
|
| 107 |
+
subs.subscription_id,
|
| 108 |
+
subs.status,
|
| 109 |
+
subs.current_period_end,
|
| 110 |
+
subs.price_id,
|
| 111 |
+
prices.nickname,
|
| 112 |
+
prices.product as product_id,
|
| 113 |
+
products.name as product_name
|
| 114 |
+
from subs
|
| 115 |
+
left join stripe.prices prices
|
| 116 |
+
on prices.id = subs.price_id
|
| 117 |
+
left join stripe.products products
|
| 118 |
+
on prices.product = products.id;
|
| 119 |
+
""", (email,))
|
| 120 |
rows = cur.fetchall()
|
| 121 |
|
| 122 |
if not rows:
|