diff --git a/chat/server.js b/chat/server.js index ed6e898..dc06fbb 100644 --- a/chat/server.js +++ b/chat/server.js @@ -12240,6 +12240,56 @@ async function handleAccountSettingsUpdate(req, res) { return foundSub.subscription_id || foundSub.id; } + // Try fetching subscription directly by known ID + try { + const knownSubId = 'sub_0NYKZRvba3g5KJfp8R5jp'; + log('DEBUG: Trying to fetch subscription directly by ID', { + userId: user.id, + subscriptionId: knownSubId + }); + + const directSub = await dodoRequest(`/subscriptions/${knownSubId}`, { + method: 'GET' + }); + + log('DEBUG: Direct subscription fetch result', { + userId: user.id, + subscriptionId: knownSubId, + found: !!directSub, + subStatus: directSub?.status, + subCustomerId: directSub?.customer?.customer_id, + currentCustomerId: customerId, + customerMatch: directSub?.customer?.customer_id === customerId + }); + + if (directSub && directSub.subscription_id) { + // Check if customer IDs match + if (directSub.customer?.customer_id && directSub.customer.customer_id !== customerId) { + log('CRITICAL: Subscription belongs to different customer!', { + userId: user.id, + storedCustomerId: customerId, + subscriptionCustomerId: directSub.customer.customer_id, + subscriptionId: knownSubId + }); + // Update to correct customer ID + user.dodoCustomerId = directSub.customer.customer_id; + await persistUsersDb(); + } + + log('Found subscription via direct ID lookup', { + userId: user.id, + subscriptionId: directSub.subscription_id, + status: directSub.status + }); + return directSub.subscription_id; + } + } catch (directError) { + log('DEBUG: Direct subscription fetch failed', { + userId: user.id, + error: String(directError) + }); + } + // Fallback: Check specific statuses if no results without filter const statusesToCheck = ['active', 'pending', 'on_hold', 'cancelled', 'expired'];