From 5dcdeed5fbeec96ff096a700a73d4cbab67a360e Mon Sep 17 00:00:00 2001 From: Jonas Acker Date: Fri, 13 Nov 2026 16:15:51 +0100 Subject: [PATCH] Count promotional Pro grants as Pro Co-Authored-By: Claude Opus 5.5 --- lib/models/entitlement.dart | 19 +++++++- lib/services/entitlements_service.dart | 18 +++++++ lib/utils/iap/iap_manager.dart | 9 ++-- lib/utils/iap/revenuecat_service.dart | 3 +- test/models/entitlement_test.dart | 50 ++++++++++++++++++++ test/utils/iap/iap_manager_account_test.dart | 22 +++++++++ 6 files changed, 111 insertions(+), 10 deletions(-) diff --git a/lib/models/entitlement.dart b/lib/models/entitlement.dart index 521eb8f1..e09f32a7 100644 --- a/lib/models/entitlement.dart +++ b/lib/models/entitlement.dart @@ -13,13 +13,28 @@ class Entitlement { bool get isActive => isActiveAt(DateTime.now()); + /// RevenueCat stores a promotional grant under `rc_promo__`. + static const String _promoPrefix = 'rc_promo_'; + + /// Whether a row with [productKey] unlocks Pro: a monthly or yearly + /// subscription, or a promotional Pro grant from RevenueCat + /// (`rc_promo_pro_weekly`, `rc_promo_pro_lifetime`, ...). + static bool isProProductKey(String productKey) => + productKey == 'premium_monthly' || + productKey == 'premium_yearly' || + productKey.startsWith('${_promoPrefix}pro_'); + + bool get isPro => isProProductKey(productKey); + /// Store and Stripe rows are kept current by webhooks, and their /// `activeUntil` can lag a renewal, so only their status counts. Nothing - /// flips a manual grant's status, so it ends at its `activeUntil`. + /// flips a manual grant's status, so it ends at its `activeUntil`; a + /// promotional grant never renews, so it ends there too. bool isActiveAt(DateTime now) { if (status != 'active') return false; // || status == 'grace'; final until = activeUntil; - if (source == 'manual' && until != null && !until.isAfter(now)) return false; + final endsAtActiveUntil = source == 'manual' || productKey.startsWith(_promoPrefix); + if (endsAtActiveUntil && until != null && !until.isAfter(now)) return false; return true; } diff --git a/lib/services/entitlements_service.dart b/lib/services/entitlements_service.dart index 38a9202a..f83fe028 100644 --- a/lib/services/entitlements_service.dart +++ b/lib/services/entitlements_service.dart @@ -89,6 +89,24 @@ class EntitlementsService extends ChangeNotifier { }); } + /// Any active Pro entitlement, see [Entitlement.isPro]. + bool get hasActivePro { + final now = _now(); + return _entitlements.any((entitlement) => entitlement.isPro && entitlement.isActiveAt(now)); + } + + /// The latest end date among the active Pro entitlements. + DateTime? get proActiveUntil { + final now = _now(); + DateTime? latest; + for (final entitlement in _entitlements) { + if (!entitlement.isPro || !entitlement.isActiveAt(now)) continue; + final value = entitlement.activeUntil; + if (value != null && (latest == null || value.isAfter(latest))) latest = value; + } + return latest; + } + DateTime? activeUntil(String productKey) { DateTime? latest; for (final entitlement in _entitlements) { diff --git a/lib/utils/iap/iap_manager.dart b/lib/utils/iap/iap_manager.dart index dc2b189f..e8e62f34 100644 --- a/lib/utils/iap/iap_manager.dart +++ b/lib/utils/iap/iap_manager.dart @@ -157,9 +157,7 @@ class IAPManager { bool get isBetaTester => (isLoggedIn && entitlements.hasActive(betaAccessProductKey) || kDebugMode); bool get hasActiveSubscription => - (isLoggedIn && (entitlements.hasActive(premiumMonthlyProductKey)) || - entitlements.hasActive(premiumYearlyProductKey)) || - (!isLoggedIn && isLocalPro.value); + (isLoggedIn && entitlements.hasActivePro) || (!isLoggedIn && isLocalPro.value); bool get isProEnabled => hasActiveSubscription && (isLoggedIn || (!isLoggedIn && isLocalPro.value)); @@ -211,7 +209,7 @@ class IAPManager { if (fromStore != null) return fromStore; Entitlement? latest; for (final e in entitlements.current) { - if (!e.isActive || (e.productKey != premiumMonthlyProductKey && e.productKey != premiumYearlyProductKey)) { + if (!e.isActive || !e.isPro) { continue; } if (latest == null || (e.activeUntil?.isAfter(latest.activeUntil ?? DateTime(0)) ?? false)) latest = e; @@ -243,8 +241,7 @@ class IAPManager { @visibleForTesting set purchaseChannelForTesting(PurchaseChannel? channel) => _purchaseChannelForTesting = channel; - DateTime? get premiumActiveUntil => - entitlements.activeUntil(premiumMonthlyProductKey) ?? entitlements.activeUntil(premiumYearlyProductKey); + DateTime? get premiumActiveUntil => entitlements.proActiveUntil; /// Initialize the IAP manager. Future initialize() async { diff --git a/lib/utils/iap/revenuecat_service.dart b/lib/utils/iap/revenuecat_service.dart index 4e72cddb..eb29adb4 100644 --- a/lib/utils/iap/revenuecat_service.dart +++ b/lib/utils/iap/revenuecat_service.dart @@ -492,8 +492,7 @@ class RevenueCatService { static Map? syncSubscriptionsBody({required bool expectActive}) => expectActive ? const {'expectActive': true} : null; - bool get _backendPro => - entitlementsService.hasActive(premiumProductKeyMonthly) || entitlementsService.hasActive(premiumProductKeyYearly); + bool get _backendPro => entitlementsService.hasActivePro; /// Whether the backend must be told to re-read the store: signed into an /// account, RevenueCat says Pro, but the account's entitlements don't. diff --git a/test/models/entitlement_test.dart b/test/models/entitlement_test.dart index 560d0f87..0f940420 100644 --- a/test/models/entitlement_test.dart +++ b/test/models/entitlement_test.dart @@ -64,4 +64,54 @@ void main() { expect(service.hasActive('beta_access'), isTrue); }); }); + + group('Pro entitlements', () { + Entitlement row(String key, {String source = 'revenuecat', DateTime? activeUntil, String status = 'active'}) => + Entitlement(productKey: key, status: status, activeUntil: activeUntil, source: source); + + test('subscriptions and RevenueCat promotional Pro grants are Pro', () { + for (final key in ['premium_monthly', 'premium_yearly', 'rc_promo_pro_weekly', 'rc_promo_pro_lifetime']) { + expect(row(key).isPro, isTrue, reason: key); + } + }); + + test('Base, beta and promotional Base grants are not Pro', () { + for (final key in ['full_version', 'full_access_unlock', 'beta_access', 'rc_promo_Full Version_lifetime']) { + expect(row(key).isPro, isFalse, reason: key); + } + }); + + test('a promotional grant ends at its end date even before RevenueCat expires the row', () { + expect(row('rc_promo_pro_weekly', activeUntil: _past).isActiveAt(_now), isFalse); + expect(row('rc_promo_pro_weekly', activeUntil: _future).isActiveAt(_now), isTrue); + expect(row('rc_promo_pro_lifetime').isActiveAt(_now), isTrue); + }); + + Future serviceWith(List rows) async { + SharedPreferences.setMockInitialValues({ + 'entitlements_cache_items': jsonEncode([for (final r in rows) r.toJson()]), + }); + final client = SupabaseClient('https://example.test', 'test-anon-key'); + addTearDown(client.dispose); + final service = EntitlementsService(client, deviceIdentityService: DeviceIdentityService(), now: () => _now); + await service.initialize(); + return service; + } + + test('hasActivePro counts a promotional Pro week', () async { + final service = await serviceWith([row('rc_promo_pro_weekly', activeUntil: _future)]); + + expect(service.hasActivePro, isTrue); + expect(service.proActiveUntil, _future); + }); + + test('hasActivePro is false with only Base or an expired promo', () async { + final service = await serviceWith([ + row('full_version'), + row('rc_promo_pro_weekly', status: 'expired', activeUntil: _past), + ]); + + expect(service.hasActivePro, isFalse); + }); + }); } diff --git a/test/utils/iap/iap_manager_account_test.dart b/test/utils/iap/iap_manager_account_test.dart index eb3a7ec6..46fe8793 100644 --- a/test/utils/iap/iap_manager_account_test.dart +++ b/test/utils/iap/iap_manager_account_test.dart @@ -75,6 +75,28 @@ Future main() async { }); }); + group('signed-in account with a promotional Pro grant', () { + test('has Pro (rc_promo_pro_weekly counts like a subscription)', () async { + SharedPreferences.setMockInitialValues({ + 'entitlements_cache_items': jsonEncode([ + { + 'product_key': 'rc_promo_pro_weekly', + 'status': 'active', + 'active_until': DateTime.now().add(const Duration(days: 4)).toUtc().toIso8601String(), + 'source': 'revenuecat', + }, + ]), + }); + await iap.entitlements.initialize(); + await auth.recoverSession(jsonEncode(_sessionJson(anonymous: false, email: 'rider@example.com'))); + + expect(iap.isLoggedIn, isTrue); + expect(iap.hasActiveSubscription, isTrue); + expect(iap.isProEnabled, isTrue); + expect(iap.premiumActiveUntil, isNotNull); + }); + }); + group('revenueCatLogin', () { test('never logs RevenueCat in as an anonymous user', () { for (final event in AuthChangeEvent.values) {