Skip to content

Commit 7b19f7d

Browse files
authored
Merge pull request #2320 from kortix-ai/feature/mobile-upgrades
WIP
2 parents 962d939 + 1ecaabf commit 7b19f7d

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

apps/mobile/components/settings/BillingPage.tsx

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* Matches web's "Billing Status – Manage your credits and subscription" design
55
*/
66

7-
import React, { useState, useCallback } from 'react';
8-
import { View, ScrollView, Pressable } from 'react-native';
7+
import React, { useState, useCallback, useEffect } from 'react';
8+
import { View, ScrollView, Pressable, Platform } from 'react-native';
99
import * as WebBrowser from 'expo-web-browser';
1010
import { Text } from '@/components/ui/text';
1111
import { Icon } from '@/components/ui/icon';
@@ -21,6 +21,8 @@ import {
2121
presentCustomerInfo,
2222
shouldUseRevenueCat,
2323
isRevenueCatConfigured,
24+
isRevenueCatInitialized,
25+
initializeRevenueCat,
2426
} from '@/lib/billing';
2527
import { useAuthContext } from '@/contexts';
2628
import { useLanguage } from '@/contexts';
@@ -148,17 +150,47 @@ export function BillingPage({ visible, onClose, onChangePlan }: BillingPageProps
148150
const handleCustomerInfo = useCallback(async () => {
149151
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
150152
try {
153+
// Ensure RevenueCat is initialized before presenting customer info
154+
if (user && shouldUseRevenueCat() && isRevenueCatConfigured()) {
155+
const initialized = await isRevenueCatInitialized();
156+
if (!initialized) {
157+
console.log('🔄 RevenueCat not initialized, initializing now...');
158+
try {
159+
await initializeRevenueCat(user.id, user.email || undefined, true);
160+
} catch (initError) {
161+
console.warn('⚠️ RevenueCat initialization warning:', initError);
162+
}
163+
}
164+
}
165+
151166
await presentCustomerInfo();
152167
// Refresh billing data after user returns from customer info portal
153168
handleSubscriptionUpdate();
154169
} catch (error) {
155170
console.error('Error presenting customer info portal:', error);
156171
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
157172
}
158-
}, [handleSubscriptionUpdate]);
173+
}, [user, handleSubscriptionUpdate]);
159174

175+
// Show button if RevenueCat should be used and is configured
176+
// We'll handle initialization when the button is clicked if needed
160177
const useRevenueCat = shouldUseRevenueCat() && isRevenueCatConfigured();
161178

179+
// Debug logging to help diagnose button visibility
180+
useEffect(() => {
181+
if (visible) {
182+
console.log('🔍 [BillingPage] RevenueCat button visibility check:', {
183+
shouldUseRevenueCat: shouldUseRevenueCat(),
184+
isRevenueCatConfigured: isRevenueCatConfigured(),
185+
useRevenueCat,
186+
platform: Platform.OS,
187+
hasIosKey: !!process.env.EXPO_PUBLIC_REVENUECAT_IOS_API_KEY,
188+
hasAndroidKey: !!process.env.EXPO_PUBLIC_REVENUECAT_ANDROID_API_KEY,
189+
useRevenueCatEnv: process.env.EXPO_PUBLIC_USE_REVENUECAT,
190+
});
191+
}
192+
}, [visible, useRevenueCat]);
193+
162194
if (!visible) return null;
163195

164196
if (isLoadingSubscription) {

apps/mobile/lib/billing/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export {
8686
checkSubscriptionStatus,
8787
presentPaywall,
8888
presentCustomerInfo,
89+
isRevenueCatInitialized,
8990
} from './revenuecat';
9091
export type { RevenueCatProduct } from './revenuecat';
9192

apps/mobile/lib/billing/revenuecat.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,19 @@ export async function getCustomerInfo(): Promise<CustomerInfo> {
463463
}
464464
}
465465

466+
/**
467+
* Check if RevenueCat is actually initialized and ready to use
468+
* This is more reliable than just checking for API keys
469+
*/
470+
export async function isRevenueCatInitialized(): Promise<boolean> {
471+
try {
472+
await Purchases.getCustomerInfo();
473+
return true;
474+
} catch {
475+
return false;
476+
}
477+
}
478+
466479
export function getSubscriptionInfo(customerInfo: CustomerInfo): RevenueCatSubscriptionInfo {
467480
const entitlements = customerInfo.entitlements.active;
468481
const hasActiveEntitlement = Object.keys(entitlements).length > 0;

0 commit comments

Comments
 (0)