()
| 21 | |
| 22 | |
| 23 | def main(): |
| 24 | parser = argparse.ArgumentParser(description='Sunset all legacy subscriptions') |
| 25 | parser.add_argument( |
| 26 | '--dry-run', action='store_true', help='Show what would be done without making changes' |
| 27 | ) |
| 28 | parser.add_argument('--limit', type=int, default=100, help='Number of subscriptions to process per page') |
| 29 | args = parser.parse_args() |
| 30 | |
| 31 | # Get environment variables |
| 32 | stripe_secret_key = os.getenv('STRIPE_SECRET_KEY') |
| 33 | current_price_id = os.getenv('STRIPE_SUBSCRIPTION_PRICE_ID') |
| 34 | |
| 35 | if not stripe_secret_key: |
| 36 | logger.error("STRIPE_SECRET_KEY environment variable not set") |
| 37 | sys.exit(1) |
| 38 | |
| 39 | if not current_price_id: |
| 40 | logger.error("STRIPE_SUBSCRIPTION_PRICE_ID environment variable not set") |
| 41 | sys.exit(1) |
| 42 | |
| 43 | stripe.api_key = stripe_secret_key |
| 44 | |
| 45 | logger.info(f"Current price ID: {current_price_id}") |
| 46 | logger.info(f"Dry run: {args.dry_run}") |
| 47 | |
| 48 | sunset_count = 0 |
| 49 | already_cancelled_count = 0 |
| 50 | current_pricing_count = 0 |
| 51 | error_count = 0 |
| 52 | |
| 53 | try: |
| 54 | # Get all active subscriptions |
| 55 | has_more = True |
| 56 | starting_after = None |
| 57 | |
| 58 | while has_more: |
| 59 | params = { |
| 60 | 'status': 'active', |
| 61 | 'limit': args.limit, |
| 62 | } |
| 63 | if starting_after: |
| 64 | params['starting_after'] = starting_after |
| 65 | |
| 66 | subscriptions = stripe.Subscription.list(**params) |
| 67 | |
| 68 | for sub in subscriptions.data: |
| 69 | try: |
| 70 | # Check if it's using the old price |
| 71 | is_legacy = True |
| 72 | legacy_price_id = None |
| 73 | |
| 74 | for item in sub.get('items', {}).get('data', []): |
| 75 | price_id = item.get('price', {}).get('id') |
| 76 | if price_id == current_price_id: |
| 77 | is_legacy = False |
| 78 | break |
| 79 | else: |
| 80 | legacy_price_id = price_id |
no test coverage detected
searching dependent graphs…