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' ;
99import * as WebBrowser from 'expo-web-browser' ;
1010import { Text } from '@/components/ui/text' ;
1111import { Icon } from '@/components/ui/icon' ;
@@ -21,6 +21,8 @@ import {
2121 presentCustomerInfo ,
2222 shouldUseRevenueCat ,
2323 isRevenueCatConfigured ,
24+ isRevenueCatInitialized ,
25+ initializeRevenueCat ,
2426} from '@/lib/billing' ;
2527import { useAuthContext } from '@/contexts' ;
2628import { 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 ) {
0 commit comments