@umituz/react-native-subscription 2.37.32 → 2.37.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/react-native-subscription",
3
- "version": "2.37.32",
3
+ "version": "2.37.33",
4
4
  "description": "Complete subscription management with RevenueCat, paywall UI, and credits system for React Native apps",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -29,7 +29,7 @@ async function fetchOfferingsSafe(): Promise<any> {
29
29
  }
30
30
 
31
31
  function normalizeUserId(userId: string): string | null {
32
- return (userId && userId.length > 0) ? userId : null;
32
+ return (userId && userId.length > 0 && userId !== "__anonymous__") ? userId : null;
33
33
  }
34
34
 
35
35
  function isAnonymousId(userId: string): boolean {
@@ -28,7 +28,6 @@ export interface SubscriptionInitConfig {
28
28
  showAuthModal: () => void;
29
29
  onCreditsUpdated: (userId: string) => void;
30
30
  creditPackages: CreditPackageConfig;
31
- allowAnonymousPurchases?: boolean;
32
31
  timeoutMs: number;
33
32
  authStateTimeoutMs: number;
34
33
  }
@@ -7,7 +7,7 @@ import type { CustomerInfo } from "react-native-purchases";
7
7
  import type { PackageType } from "../../../revenuecat/core/types/RevenueCatTypes";
8
8
 
9
9
  export function configureServices(config: SubscriptionInitConfig, apiKey: string): SubscriptionSyncService {
10
- const { entitlementId, credits, creditPackages, getFirebaseAuth, showAuthModal, onCreditsUpdated, getAnonymousUserId, allowAnonymousPurchases } = config;
10
+ const { entitlementId, credits, creditPackages, getFirebaseAuth, showAuthModal, onCreditsUpdated, getAnonymousUserId } = config;
11
11
 
12
12
  if (!creditPackages) {
13
13
  throw new Error('[ServiceConfigurator] creditPackages configuration is required');
@@ -59,7 +59,6 @@ export function configureServices(config: SubscriptionInitConfig, apiKey: string
59
59
  }
60
60
 
61
61
  const u = auth.currentUser;
62
- if (allowAnonymousPurchases && u?.isAnonymous) return true;
63
62
  return !!(u && !u.isAnonymous);
64
63
  },
65
64
  showAuthModal,
@@ -11,6 +11,7 @@ import { useAlert } from "@umituz/react-native-design-system";
11
11
  import {
12
12
  useAuthStore,
13
13
  selectUserId,
14
+ selectIsAnonymous,
14
15
  } from "@umituz/react-native-auth";
15
16
  import { SubscriptionManager } from "../../infrastructure/managers/SubscriptionManager";
16
17
  import { SUBSCRIPTION_QUERY_KEYS } from "./subscriptionQueryKeys";
@@ -31,6 +32,7 @@ interface PurchaseMutationResult {
31
32
  */
32
33
  export const usePurchasePackage = () => {
33
34
  const userId = useAuthStore(selectUserId);
35
+ const isAnonymous = useAuthStore(selectIsAnonymous);
34
36
  const queryClient = useQueryClient();
35
37
  const { showSuccess, showError } = useAlert();
36
38
 
@@ -40,12 +42,17 @@ export const usePurchasePackage = () => {
40
42
  throw new Error("User not authenticated");
41
43
  }
42
44
 
45
+ if (isAnonymous) {
46
+ throw new Error("Anonymous users cannot purchase subscriptions");
47
+ }
48
+
43
49
  const productId = pkg.product.identifier;
44
50
  if (typeof __DEV__ !== "undefined" && __DEV__) {
45
- console.log("[Purchase] Calling SubscriptionManager.purchasePackage()");
51
+ console.log(`[Purchase] Initializing and purchasing. User: ${userId}`);
46
52
  }
47
53
 
48
- const success = await SubscriptionManager.purchasePackage(pkg);
54
+ await SubscriptionManager.initialize(userId);
55
+ const success = await SubscriptionManager.purchasePackage(pkg, userId);
49
56
 
50
57
  return { success, productId };
51
58
  },
@@ -10,7 +10,6 @@ import { useAlert } from "@umituz/react-native-design-system";
10
10
  import {
11
11
  useAuthStore,
12
12
  selectUserId,
13
- selectIsAnonymous,
14
13
  } from "@umituz/react-native-auth";
15
14
  import { SubscriptionManager } from "../../infrastructure/managers/SubscriptionManager";
16
15
  import { SUBSCRIPTION_QUERY_KEYS } from "./subscriptionQueryKeys";
@@ -30,7 +29,6 @@ interface RestoreResult {
30
29
  */
31
30
  export const useRestorePurchase = () => {
32
31
  const userId = useAuthStore(selectUserId);
33
- const isAnonymous = useAuthStore(selectIsAnonymous);
34
32
  const queryClient = useQueryClient();
35
33
  const { showSuccess, showInfo, showError } = useAlert();
36
34
 
@@ -40,11 +38,8 @@ export const useRestorePurchase = () => {
40
38
  throw new Error("User not authenticated");
41
39
  }
42
40
 
43
- if (isAnonymous) {
44
- throw new Error("Anonymous users cannot restore purchases");
45
- }
46
-
47
- const result = await SubscriptionManager.restore();
41
+ await SubscriptionManager.initialize(userId);
42
+ const result = await SubscriptionManager.restore(userId);
48
43
  return result;
49
44
  },
50
45
  onSuccess: (result) => {
@@ -2,7 +2,7 @@ import type { PurchasesPackage } from "react-native-purchases";
2
2
  import type { IRevenueCatService } from "../../../../shared/application/ports/IRevenueCatService";
3
3
  import type { PackageHandler } from "../handlers/PackageHandler";
4
4
  import { SubscriptionInternalState } from "./SubscriptionInternalState";
5
- import { ensureServiceAvailable } from "./subscriptionManagerUtils";
5
+ import { ensureServiceAvailable, getCurrentUserIdOrThrow } from "./subscriptionManagerUtils";
6
6
  import type { SubscriptionManagerConfig, PremiumStatus, RestoreResultInfo } from "./SubscriptionManager.types";
7
7
  import { createPackageHandler } from "./packageHandlerFactory";
8
8
  import { checkPremiumStatusFromService } from "./premiumStatusChecker";
@@ -100,17 +100,25 @@ class SubscriptionManagerImpl {
100
100
  return getPackagesOperation(this.managerConfig, this.serviceInstance, this.packageHandler!);
101
101
  }
102
102
 
103
- async purchasePackage(pkg: PurchasesPackage): Promise<boolean> {
103
+ async purchasePackage(pkg: PurchasesPackage, explicitUserId?: string): Promise<boolean> {
104
104
  this.ensureConfigured();
105
+ if (explicitUserId) {
106
+ await this.initialize(explicitUserId);
107
+ }
105
108
  this.ensurePackageHandlerInitialized();
106
- const result = await purchasePackageOperation(pkg, this.managerConfig, this.state, this.packageHandler!);
109
+ const resolvedUserId = explicitUserId || getCurrentUserIdOrThrow(this.state);
110
+ const result = await purchasePackageOperation(pkg, this.managerConfig, resolvedUserId, this.packageHandler!);
107
111
  return result;
108
112
  }
109
113
 
110
- async restore(): Promise<RestoreResultInfo> {
114
+ async restore(explicitUserId?: string): Promise<RestoreResultInfo> {
111
115
  this.ensureConfigured();
116
+ if (explicitUserId) {
117
+ await this.initialize(explicitUserId);
118
+ }
112
119
  this.ensurePackageHandlerInitialized();
113
- return restoreOperation(this.managerConfig, this.state, this.packageHandler!);
120
+ const resolvedUserId = explicitUserId || getCurrentUserIdOrThrow(this.state);
121
+ return restoreOperation(this.managerConfig, resolvedUserId, this.packageHandler!);
114
122
  }
115
123
 
116
124
  async checkPremiumStatus(): Promise<PremiumStatus> {
@@ -2,8 +2,7 @@ import type { PurchasesPackage } from "react-native-purchases";
2
2
  import type { IRevenueCatService } from "../../../../shared/application/ports/IRevenueCatService";
3
3
  import type { PackageHandler } from "../handlers/PackageHandler";
4
4
  import type { RestoreResultInfo } from "./SubscriptionManager.types";
5
- import { SubscriptionInternalState } from "./SubscriptionInternalState";
6
- import { ensureConfigured, getCurrentUserIdOrThrow, getOrCreateService } from "./subscriptionManagerUtils";
5
+ import { ensureConfigured, getOrCreateService } from "./subscriptionManagerUtils";
7
6
  import type { SubscriptionManagerConfig } from "./SubscriptionManager.types";
8
7
 
9
8
  export const getPackagesOperation = async (
@@ -19,21 +18,19 @@ export const getPackagesOperation = async (
19
18
  export const purchasePackageOperation = async (
20
19
  pkg: PurchasesPackage,
21
20
  managerConfig: SubscriptionManagerConfig | null,
22
- state: SubscriptionInternalState,
21
+ userId: string,
23
22
  packageHandler: PackageHandler
24
23
  ): Promise<boolean> => {
25
24
  ensureConfigured(managerConfig);
26
- const userId = getCurrentUserIdOrThrow(state);
27
25
  const result = await packageHandler.purchase(pkg, userId);
28
26
  return result;
29
27
  };
30
28
 
31
29
  export const restoreOperation = async (
32
30
  managerConfig: SubscriptionManagerConfig | null,
33
- state: SubscriptionInternalState,
31
+ userId: string,
34
32
  packageHandler: PackageHandler
35
33
  ): Promise<RestoreResultInfo> => {
36
34
  ensureConfigured(managerConfig);
37
- const userId = getCurrentUserIdOrThrow(state);
38
35
  return packageHandler.restore(userId);
39
36
  };