()
| 16 | |
| 17 | |
| 18 | def main(): |
| 19 | # Get environment variables |
| 20 | stripe_secret_key = os.getenv('STRIPE_SECRET_KEY') |
| 21 | if not stripe_secret_key: |
| 22 | logger.error("STRIPE_SECRET_KEY environment variable not set") |
| 23 | sys.exit(1) |
| 24 | |
| 25 | stripe.api_key = stripe_secret_key |
| 26 | |
| 27 | logger.info("Checking for sunset legacy subscriptions...") |
| 28 | |
| 29 | # Counters |
| 30 | total_active = 0 |
| 31 | sunset_legacy = 0 |
| 32 | cancelling_other_reason = 0 |
| 33 | not_cancelling = 0 |
| 34 | |
| 35 | sunset_subscriptions = [] |
| 36 | |
| 37 | try: |
| 38 | # Get all active subscriptions |
| 39 | has_more = True |
| 40 | starting_after = None |
| 41 | |
| 42 | while has_more: |
| 43 | params = { |
| 44 | 'status': 'active', |
| 45 | 'limit': 100, |
| 46 | } |
| 47 | if starting_after: |
| 48 | params['starting_after'] = starting_after |
| 49 | |
| 50 | subscriptions = stripe.Subscription.list(**params) |
| 51 | |
| 52 | for sub in subscriptions.data: |
| 53 | total_active += 1 |
| 54 | |
| 55 | cancel_at_period_end = sub.get('cancel_at_period_end', False) |
| 56 | cancellation_reason = sub.get('metadata', {}).get('cancellation_reason') |
| 57 | |
| 58 | # This is the exact check from the webhook handler |
| 59 | if cancel_at_period_end and cancellation_reason == 'billing_model_change': |
| 60 | sunset_legacy += 1 |
| 61 | |
| 62 | # Get customer email for display |
| 63 | customer_email = 'unknown' |
| 64 | try: |
| 65 | customer = stripe.Customer.retrieve(sub.get('customer')) |
| 66 | customer_email = customer.get('email', 'unknown') |
| 67 | except: |
| 68 | pass |
| 69 | |
| 70 | # Calculate days until cancellation |
| 71 | current_period_end_ts = sub.get('current_period_end') |
| 72 | if current_period_end_ts: |
| 73 | current_period_end = datetime.fromtimestamp(current_period_end_ts) |
| 74 | days_until_cancel = (current_period_end - datetime.now()).days |
| 75 | cancel_date = current_period_end.strftime('%Y-%m-%d') |
no test coverage detected
searching dependent graphs…