File size: 13,012 Bytes
7cfdc4c | 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 | #!/usr/bin/env python3
import asyncio
import sys
from pathlib import Path
from typing import List, Dict, Any
from datetime import datetime, timezone
from decimal import Decimal
import time
backend_dir = Path(__file__).parent.parent.parent
sys.path.insert(0, str(backend_dir))
import stripe
from core.services.supabase import DBConnection
from core.utils.config import config
from core.utils.logger import logger
from core.billing.shared.config import get_tier_by_price_id, TIERS
from core.billing.external.stripe import webhook_service
stripe.api_key = config.STRIPE_SECRET_KEY
class FastSyncService:
def __init__(self):
self.db = DBConnection()
self.stats = {
'total_users': 0,
'synced': 0,
'converted_to_trial': 0,
'errors': 0,
'skipped': 0,
'no_subscription': 0,
'wrong_status': 0,
'no_price_id': 0,
'unmatched_tier': 0,
'start_time': time.time()
}
async def initialize(self):
await self.db.initialize()
self.client = await self.db.client
async def run(self):
print("\n" + "="*60)
print("STRIPE SUBSCRIPTION SYNC")
print("="*60)
await self.initialize()
await self.sync_stripe_subscriptions()
await self.convert_free_users_batch()
await self.fix_topup_credits_batch()
self.print_stats()
async def sync_stripe_subscriptions(self):
print("\nπ SYNCING STRIPE SUBSCRIPTIONS...")
offset = 0
batch_size = 1000
while True:
customers_result = await self.client.schema('basejump')\
.from_('billing_customers')\
.select('id, account_id')\
.range(offset, offset + batch_size - 1)\
.execute()
if not customers_result.data:
break
print(f" Processing batch {offset//batch_size + 1} ({len(customers_result.data)} customers)...")
tasks = []
for customer in customers_result.data:
tasks.append(self.sync_customer_subscription(customer))
for i in range(0, len(tasks), 10):
chunk = tasks[i:i+10]
await asyncio.gather(*chunk, return_exceptions=True)
offset += batch_size
if len(customers_result.data) < batch_size:
break
print(f" β
Processed {self.stats['synced']} subscriptions")
async def sync_customer_subscription(self, customer: Dict) -> None:
stripe_customer_id = customer['id']
account_id = customer['account_id']
try:
subscriptions = await stripe.Subscription.list_async(
customer=stripe_customer_id,
status='all',
limit=1,
expand=['data.items.data.price']
)
if not subscriptions.data:
self.stats['no_subscription'] += 1
logger.debug(f"Customer {stripe_customer_id}: no subscription found")
return
sub = subscriptions.data[0]
if sub.status not in ['active', 'trialing']:
self.stats['wrong_status'] += 1
logger.debug(f"Customer {stripe_customer_id}: subscription status is {sub.status}, skipping")
return
if sub.status in ['active', 'trialing']:
price_id = None
items_data = None
if hasattr(sub, 'items'):
if callable(sub.items):
items_obj = sub.items()
if hasattr(items_obj, 'data'):
items_data = items_obj.data
elif hasattr(sub.items, 'data'):
items_data = sub.items.data
if items_data and len(items_data) > 0:
item = items_data[0]
if hasattr(item, 'price'):
if hasattr(item.price, 'id'):
price_id = item.price.id
elif isinstance(item.price, str):
price_id = item.price
if not price_id:
self.stats['no_price_id'] += 1
logger.debug(f"Customer {stripe_customer_id}: no price_id found in subscription")
return
tier = get_tier_by_price_id(price_id) if price_id else None
if not tier:
self.stats['unmatched_tier'] += 1
logger.debug(f"Customer {stripe_customer_id}: price_id {price_id} doesn't match any configured tier")
return
logger.info(f"Syncing customer {stripe_customer_id} with tier {tier.name}")
update_data = {
'tier': tier.name,
'stripe_subscription_id': sub.id,
'billing_cycle_anchor': datetime.fromtimestamp(sub.created).isoformat() if sub.created else None,
'updated_at': datetime.now(timezone.utc).isoformat()
}
if sub.current_period_end:
update_data['next_credit_grant'] = datetime.fromtimestamp(sub.current_period_end).isoformat()
await self.client.from_('credit_accounts')\
.update(update_data)\
.eq('account_id', account_id)\
.execute()
self.stats['synced'] += 1
except Exception as e:
logger.error(f"Error syncing customer {stripe_customer_id}: {e}")
self.stats['errors'] += 1
async def convert_free_users_batch(self):
print("\nπ CONVERTING FREE TIER USERS...")
offset = 0
batch_size = 1000
total_converted = 0
while True:
free_users = await self.client.from_('credit_accounts')\
.select('account_id, balance')\
.eq('tier', 'free')\
.is_('stripe_subscription_id', 'null')\
.range(offset, offset + batch_size - 1)\
.execute()
if not free_users.data:
break
print(f" Converting batch {offset//batch_size + 1} ({len(free_users.data)} users)...")
account_ids = []
ledger_entries = []
for user in free_users.data:
account_id = user['account_id']
old_balance = Decimal(str(user['balance']))
account_ids.append(account_id)
if old_balance > 0:
ledger_entries.append({
'account_id': account_id,
'amount': float(-old_balance),
'balance_after': 0,
'type': 'adjustment',
'description': 'Free tier discontinued - please start a trial to continue',
'created_at': datetime.now(timezone.utc).isoformat()
})
if account_ids:
try:
update_chunk_size = 50
for i in range(0, len(account_ids), update_chunk_size):
chunk_ids = account_ids[i:i+update_chunk_size]
await self.client.from_('credit_accounts')\
.update({
'tier': 'none',
'balance': 0,
'expiring_credits': 0,
'non_expiring_credits': 0,
'trial_status': None,
'updated_at': datetime.now(timezone.utc).isoformat()
})\
.in_('account_id', chunk_ids)\
.execute()
if ledger_entries:
for i in range(0, len(ledger_entries), 100):
chunk = ledger_entries[i:i+100]
await self.client.from_('credit_ledger').insert(chunk).execute()
total_converted += len(account_ids)
self.stats['converted_to_trial'] += len(account_ids)
except Exception as e:
logger.error(f"Error converting batch: {e}")
self.stats['errors'] += 1
offset += batch_size
if len(free_users.data) < batch_size:
break
print(f" β
Converted {total_converted} users to trial-ready state")
async def fix_topup_credits_batch(self):
print("\nπ° CHECKING TOPUP CREDITS...")
offset = 0
batch_size = 1000
user_purchases = {}
while True:
purchases = await self.client.from_('credit_purchases')\
.select('account_id, amount_dollars')\
.eq('status', 'completed')\
.range(offset, offset + batch_size - 1)\
.execute()
if not purchases.data:
break
for purchase in purchases.data:
account_id = purchase['account_id']
amount = Decimal(str(purchase.get('amount_dollars', 0)))
if account_id not in user_purchases:
user_purchases[account_id] = Decimal('0')
user_purchases[account_id] += amount
offset += batch_size
if len(purchases.data) < batch_size:
break
if not user_purchases:
print(" No purchases to check")
return
print(f" Found {len(user_purchases)} users with purchases")
fixed_count = 0
account_ids = list(user_purchases.keys())
for i in range(0, len(account_ids), 100):
chunk_ids = account_ids[i:i+100]
accounts = await self.client.from_('credit_accounts')\
.select('account_id, non_expiring_credits')\
.in_('account_id', chunk_ids)\
.execute()
for account in accounts.data:
account_id = account['account_id']
current_non_expiring = Decimal(str(account.get('non_expiring_credits', 0)))
expected_purchases = user_purchases[account_id]
if current_non_expiring < expected_purchases:
missing = expected_purchases - current_non_expiring
from core.billing.credits.manager import credit_manager
await credit_manager.add_credits(
account_id=account_id,
amount=missing,
is_expiring=False,
description=f"Sync fix: Restored missing topup credits (${missing})"
)
fixed_count += 1
if fixed_count > 0:
print(f" β
Fixed {fixed_count} users with missing topup credits")
else:
print(" β
All topup credits are correct")
def print_stats(self):
elapsed = time.time() - self.stats['start_time']
print("\n" + "="*60)
print("SYNC COMPLETE")
print("="*60)
print(f"Time taken: {elapsed:.2f} seconds")
print(f"Subscriptions synced: {self.stats['synced']}")
print(f"Users converted to trial-ready: {self.stats['converted_to_trial']}")
print(f"Errors: {self.stats['errors']}")
print("\nπ Subscription Sync Details:")
print(f" No subscription found: {self.stats['no_subscription']}")
print(f" Wrong status (not active/trialing): {self.stats['wrong_status']}")
print(f" No price ID in subscription: {self.stats['no_price_id']}")
print(f" Price ID doesn't match tiers: {self.stats['unmatched_tier']}")
print("="*60)
async def main():
service = FastSyncService()
try:
await service.run()
except Exception as e:
print(f"\nβ FATAL ERROR: {e}")
logger.error(f"Sync failed: {e}", exc_info=True)
sys.exit(1)
if __name__ == "__main__":
print("Starting fast sync service...")
asyncio.run(main()) |