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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
"""
PodcastItLater Billing Integration.
Stripe subscription management and usage enforcement.
"""
# : out podcastitlater-billing
# : dep stripe
# : dep pytest
# : dep pytest-mock
import Biz.PodcastItLater.Core as Core
import Omni.Log as Log
import os
import stripe
import typing
from datetime import datetime
from datetime import timezone
logger = Log.setup()
# Stripe configuration
stripe.api_key = os.getenv("STRIPE_SECRET_KEY", "")
STRIPE_WEBHOOK_SECRET = os.getenv("STRIPE_WEBHOOK_SECRET", "")
# Price IDs from Stripe dashboard
STRIPE_PRICE_ID_PERSONAL = os.getenv("STRIPE_PRICE_ID_PERSONAL", "")
STRIPE_PRICE_ID_PRO = os.getenv("STRIPE_PRICE_ID_PRO", "")
# Map Stripe price IDs to tier names
PRICE_TO_TIER = {
STRIPE_PRICE_ID_PERSONAL: "personal",
STRIPE_PRICE_ID_PRO: "pro",
}
# Tier limits (None = unlimited)
TIER_LIMITS = {
"free": {
"articles_per_period": 10,
"minutes_per_period": None,
},
"personal": {
"articles_per_period": 50,
"minutes_per_period": None,
},
"pro": {
"articles_per_period": None,
"minutes_per_period": None,
},
}
# Price map for checkout
PRICE_MAP = {
"personal": STRIPE_PRICE_ID_PERSONAL,
"pro": STRIPE_PRICE_ID_PRO,
}
def get_period_boundaries(
user: dict[str, typing.Any],
) -> tuple[datetime, datetime]:
"""Get billing period boundaries for user.
For paid users: use Stripe subscription period.
For free users: use current calendar month (UTC).
Returns:
tuple: (period_start, period_end) as datetime objects
"""
if user.get("plan_tier") != "free" and user.get("current_period_start"):
# Paid user - use Stripe billing period
period_start = datetime.fromisoformat(user["current_period_start"])
period_end = datetime.fromisoformat(user["current_period_end"])
else:
# Free user - use calendar month
now = datetime.now(timezone.utc)
period_start = now.replace(
day=1,
hour=0,
minute=0,
second=0,
microsecond=0,
)
# Get first day of next month
if now.month == 12: # noqa: PLR2004
period_end = period_start.replace(year=now.year + 1, month=1)
else:
period_end = period_start.replace(month=now.month + 1)
return period_start, period_end
def get_usage(
user_id: int,
period_start: datetime,
period_end: datetime,
) -> dict[str, int]:
"""Get usage stats for user in billing period.
Returns:
dict with keys: articles (int), minutes (int)
"""
return Core.Database.get_usage(user_id, period_start, period_end)
def can_submit(user_id: int) -> tuple[bool, str, dict[str, int]]:
"""Check if user can submit article based on tier limits.
Returns:
tuple: (allowed: bool, message: str, usage: dict)
"""
user = Core.Database.get_user_by_id(user_id)
if not user:
return False, "User not found", {}
tier = user.get("plan_tier", "free")
limits = TIER_LIMITS.get(tier, TIER_LIMITS["free"])
# Get billing period boundaries
period_start, period_end = get_period_boundaries(user)
# Get current usage
usage = get_usage(user_id, period_start, period_end)
# Check article limit
article_limit = limits["articles_per_period"] # type: ignore[index]
if article_limit is not None and usage["articles"] >= article_limit:
msg = (
f"You've reached your limit of {article_limit} articles "
"per period. Upgrade to continue."
)
return (False, msg, usage)
# Check minutes limit (if implemented)
minute_limit = limits.get("minutes_per_period") # type: ignore[attr-defined]
if minute_limit is not None and usage.get("minutes", 0) >= minute_limit:
return (
False,
f"You've reached your limit of {minute_limit} minutes per period. "
"Please upgrade to continue.",
usage,
)
return True, "", usage
def create_checkout_session(user_id: int, tier: str, base_url: str) -> str:
"""Create Stripe Checkout session for subscription.
Args:
user_id: User ID
tier: Subscription tier (personal or pro)
base_url: Base URL for success/cancel redirects
Returns:
Checkout session URL to redirect user to
Raises:
ValueError: If tier is invalid or price ID not configured
"""
if tier not in PRICE_MAP:
msg = f"Invalid tier: {tier}"
raise ValueError(msg)
price_id = PRICE_MAP[tier]
if not price_id:
msg = f"Stripe price ID not configured for tier: {tier}"
raise ValueError(msg)
user = Core.Database.get_user_by_id(user_id)
if not user:
msg = f"User not found: {user_id}"
raise ValueError(msg)
# Create checkout session
session_params = {
"mode": "subscription",
"line_items": [{"price": price_id, "quantity": 1}],
"success_url": f"{base_url}/billing?status=success",
"cancel_url": f"{base_url}/billing?status=cancel",
"client_reference_id": str(user_id),
"metadata": {"user_id": str(user_id), "tier": tier},
"allow_promotion_codes": True,
}
# Use existing customer if available
if user.get("stripe_customer_id"):
session_params["customer"] = user["stripe_customer_id"]
else:
session_params["customer_email"] = user["email"]
session = stripe.checkout.Session.create(**session_params) # type: ignore[arg-type]
logger.info(
"Created checkout session for user %s, tier %s: %s",
user_id,
tier,
session.id,
)
return session.url # type: ignore[return-value]
def create_portal_session(user_id: int, base_url: str) -> str:
"""Create Stripe Billing Portal session.
Args:
user_id: User ID
base_url: Base URL for return redirect
Returns:
Portal session URL to redirect user to
Raises:
ValueError: If user has no Stripe customer ID
"""
user = Core.Database.get_user_by_id(user_id)
if not user:
msg = f"User not found: {user_id}"
raise ValueError(msg)
if not user.get("stripe_customer_id"):
msg = "User has no Stripe customer ID"
raise ValueError(msg)
session = stripe.billing_portal.Session.create(
customer=user["stripe_customer_id"],
return_url=f"{base_url}/billing",
)
logger.info("Created portal session for user %s: %s", user_id, session.id)
return session.url
def handle_webhook_event(payload: bytes, sig_header: str) -> dict[str, str]:
"""Verify and process Stripe webhook event.
Args:
payload: Raw webhook body
sig_header: Stripe-Signature header value
Returns:
dict with processing status
Note:
May raise stripe.error.SignatureVerificationError if invalid signature
"""
# Verify webhook signature
event = stripe.Webhook.construct_event( # type: ignore[no-untyped-call]
payload,
sig_header,
STRIPE_WEBHOOK_SECRET,
)
event_id = event["id"]
event_type = event["type"]
# Check if already processed (idempotency)
if Core.Database.has_processed_stripe_event(event_id):
logger.info("Skipping already processed event: %s", event_id)
return {"status": "skipped", "reason": "already_processed"}
# Process event based on type
logger.info("Processing webhook event: %s (%s)", event_id, event_type)
try:
if event_type == "checkout.session.completed":
_handle_checkout_completed(event["data"]["object"])
elif event_type == "customer.subscription.created":
_handle_subscription_created(event["data"]["object"])
elif event_type == "customer.subscription.updated":
_handle_subscription_updated(event["data"]["object"])
elif event_type == "customer.subscription.deleted":
_handle_subscription_deleted(event["data"]["object"])
elif event_type == "invoice.payment_failed":
_handle_payment_failed(event["data"]["object"])
else:
logger.info("Unhandled event type: %s", event_type)
return {"status": "ignored", "type": event_type}
# Mark event as processed
Core.Database.mark_stripe_event_processed(event_id, event_type, payload)
except Exception:
logger.exception("Error processing webhook event %s", event_id)
raise
else:
return {"status": "processed", "type": event_type}
def _handle_checkout_completed(session: dict[str, typing.Any]) -> None:
"""Handle checkout.session.completed event."""
user_id = int(session.get("client_reference_id", 0))
customer_id = session.get("customer")
if not user_id or not customer_id:
logger.warning(
"Missing user_id or customer_id in checkout session: %s",
session["id"],
)
return
# Link Stripe customer to user
Core.Database.set_user_stripe_customer(user_id, customer_id)
logger.info("Linked user %s to Stripe customer %s", user_id, customer_id)
def _handle_subscription_created(subscription: dict[str, typing.Any]) -> None:
"""Handle customer.subscription.created event."""
_update_subscription_state(subscription)
def _handle_subscription_updated(subscription: dict[str, typing.Any]) -> None:
"""Handle customer.subscription.updated event."""
_update_subscription_state(subscription)
def _handle_subscription_deleted(subscription: dict[str, typing.Any]) -> None:
"""Handle customer.subscription.deleted event."""
customer_id = subscription["customer"]
# Find user by customer ID
user = Core.Database.get_user_by_stripe_customer_id(customer_id)
if not user:
logger.warning("User not found for customer: %s", customer_id)
return
# Downgrade to free
Core.Database.downgrade_to_free(user["id"])
logger.info("Downgraded user %s to free tier", user["id"])
def _handle_payment_failed(invoice: dict[str, typing.Any]) -> None:
"""Handle invoice.payment_failed event."""
customer_id = invoice["customer"]
subscription_id = invoice.get("subscription")
# Find user by customer ID
user = Core.Database.get_user_by_stripe_customer_id(customer_id)
if not user:
logger.warning("User not found for customer: %s", customer_id)
return
# Update subscription status to past_due
if subscription_id:
Core.Database.update_subscription_status(user["id"], "past_due")
logger.warning(
"Payment failed for user %s, subscription %s",
user["id"],
subscription_id,
)
def _update_subscription_state(subscription: dict[str, typing.Any]) -> None:
"""Update user subscription state from Stripe subscription object."""
customer_id = subscription["customer"]
subscription_id = subscription["id"]
status = subscription["status"]
cancel_at_period_end = subscription.get("cancel_at_period_end", False)
# Get billing period
period_start = datetime.fromtimestamp(
subscription["current_period_start"],
tz=timezone.utc,
)
period_end = datetime.fromtimestamp(
subscription["current_period_end"],
tz=timezone.utc,
)
# Determine tier from price ID
price_id = subscription["items"]["data"][0]["price"]["id"]
tier = PRICE_TO_TIER.get(price_id, "free")
# Find user by customer ID
user = Core.Database.get_user_by_stripe_customer_id(customer_id)
if not user:
logger.warning("User not found for customer: %s", customer_id)
return
# Update user subscription
Core.Database.update_user_subscription(
user["id"],
subscription_id,
status,
period_start,
period_end,
tier,
cancel_at_period_end,
)
logger.info(
"Updated user %s subscription: tier=%s, status=%s",
user["id"],
tier,
status,
)
def get_tier_info(tier: str) -> dict[str, typing.Any]:
"""Get tier information for display.
Returns:
dict with keys: name, articles_limit, price, description
"""
tier_info = {
"free": {
"name": "Free",
"articles_limit": 10,
"price": "$0",
"description": "10 articles per month",
},
"personal": {
"name": "Personal",
"articles_limit": 50,
"price": "$9/mo",
"description": "50 articles per month",
},
"pro": {
"name": "Pro",
"articles_limit": None,
"price": "$29/mo",
"description": "Unlimited articles",
},
}
return tier_info.get(tier, tier_info["free"]) # type: ignore[return-value]
|