@umituz/react-native-subscription 2.37.59 → 2.37.61
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 +1 -1
- package/src/domains/credits/application/creditOperationUtils.ts +1 -0
- package/src/domains/credits/infrastructure/operations/CreditsInitializer.ts +1 -0
- package/src/domains/credits/presentation/useCredits.ts +7 -8
- package/src/domains/revenuecat/core/types/RevenueCatData.ts +1 -0
- package/src/domains/revenuecat/infrastructure/services/userSwitchHandler.ts +21 -0
- package/src/domains/subscription/application/SubscriptionInitializerTypes.ts +1 -0
- package/src/domains/subscription/application/SubscriptionSyncProcessor.ts +11 -0
- package/src/domains/subscription/infrastructure/hooks/usePurchasePackage.ts +0 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-subscription",
|
|
3
|
-
"version": "2.37.
|
|
3
|
+
"version": "2.37.61",
|
|
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",
|
|
@@ -73,5 +73,6 @@ export function buildCreditsData({
|
|
|
73
73
|
...(billingIssueTimestamp && { billingIssueDetectedAt: billingIssueTimestamp }),
|
|
74
74
|
...(metadata.store && { store: metadata.store }),
|
|
75
75
|
...(metadata.ownershipType && { ownershipType: metadata.ownershipType }),
|
|
76
|
+
...(metadata.revenueCatUserId && { revenueCatUserId: metadata.revenueCatUserId }),
|
|
76
77
|
};
|
|
77
78
|
}
|
|
@@ -70,6 +70,7 @@ export async function initializeCreditsWithRetry(params: InitializeCreditsParams
|
|
|
70
70
|
billingIssueDetectedAt: revenueCatData.billingIssueDetectedAt,
|
|
71
71
|
store: revenueCatData.store,
|
|
72
72
|
ownershipType: revenueCatData.ownershipType,
|
|
73
|
+
revenueCatUserId: revenueCatData.revenueCatUserId,
|
|
73
74
|
type,
|
|
74
75
|
}
|
|
75
76
|
);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useQuery, useQueryClient } from "@umituz/react-native-design-system";
|
|
2
2
|
import { useCallback, useMemo, useEffect } from "react";
|
|
3
|
-
import { useAuthStore, selectUserId
|
|
3
|
+
import { useAuthStore, selectUserId } from "@umituz/react-native-auth";
|
|
4
4
|
import { subscriptionEventBus, SUBSCRIPTION_EVENTS } from "../../../shared/infrastructure/SubscriptionEventBus";
|
|
5
5
|
import { NO_CACHE_QUERY_CONFIG } from "../../../shared/infrastructure/react-query/queryConfig";
|
|
6
6
|
import { usePreviousUserCleanup } from "../../../shared/infrastructure/react-query/hooks/usePreviousUserCleanup";
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
isCreditsRepositoryConfigured,
|
|
11
11
|
} from "../infrastructure/CreditsRepositoryManager";
|
|
12
12
|
import { calculateSafePercentage, canAffordAmount } from "../utils/creditValidation";
|
|
13
|
-
import {
|
|
13
|
+
import { isAuthenticated } from "../../subscription/utils/authGuards";
|
|
14
14
|
import { creditsQueryKeys } from "./creditsQueryKeys";
|
|
15
15
|
import type { UseCreditsResult, CreditsLoadStatus } from "./useCredits.types";
|
|
16
16
|
|
|
@@ -26,17 +26,16 @@ const deriveLoadStatus = (
|
|
|
26
26
|
|
|
27
27
|
export const useCredits = (): UseCreditsResult => {
|
|
28
28
|
const userId = useAuthStore(selectUserId);
|
|
29
|
-
const isAnonymous = useAuthStore(selectIsAnonymous);
|
|
30
29
|
const isConfigured = isCreditsRepositoryConfigured();
|
|
31
30
|
|
|
32
31
|
const config = isConfigured ? getCreditsConfig() : null;
|
|
33
|
-
const
|
|
34
|
-
const queryEnabled =
|
|
32
|
+
const hasUser = isAuthenticated(userId);
|
|
33
|
+
const queryEnabled = hasUser && isConfigured;
|
|
35
34
|
|
|
36
35
|
const { data, status, error, refetch } = useQuery({
|
|
37
36
|
queryKey: creditsQueryKeys.user(userId),
|
|
38
37
|
queryFn: async () => {
|
|
39
|
-
if (!
|
|
38
|
+
if (!hasUser || !isConfigured) return null;
|
|
40
39
|
|
|
41
40
|
const repository = getCreditsRepository();
|
|
42
41
|
const result = await repository.getCredits(userId);
|
|
@@ -56,7 +55,7 @@ export const useCredits = (): UseCreditsResult => {
|
|
|
56
55
|
usePreviousUserCleanup(userId, queryClient, creditsQueryKeys.user);
|
|
57
56
|
|
|
58
57
|
useEffect(() => {
|
|
59
|
-
if (!
|
|
58
|
+
if (!hasUser) return undefined;
|
|
60
59
|
|
|
61
60
|
const unsubscribe = subscriptionEventBus.on(SUBSCRIPTION_EVENTS.CREDITS_UPDATED, (updatedUserId) => {
|
|
62
61
|
if (updatedUserId === userId) {
|
|
@@ -65,7 +64,7 @@ export const useCredits = (): UseCreditsResult => {
|
|
|
65
64
|
});
|
|
66
65
|
|
|
67
66
|
return unsubscribe;
|
|
68
|
-
}, [userId,
|
|
67
|
+
}, [userId, hasUser, queryClient]);
|
|
69
68
|
|
|
70
69
|
const credits = data ?? null;
|
|
71
70
|
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import Purchases, { type CustomerInfo, type PurchasesOfferings } from "react-native-purchases";
|
|
2
|
+
import { doc, setDoc } from "firebase/firestore";
|
|
2
3
|
import type { InitializeResult } from "../../../../shared/application/ports/IRevenueCatService";
|
|
3
4
|
import type { InitializerDeps } from "./RevenueCatInitializer.types";
|
|
4
5
|
import { FAILED_INITIALIZATION_RESULT } from "./initializerConstants";
|
|
5
6
|
import { UserSwitchMutex } from "./UserSwitchMutex";
|
|
6
7
|
import { getPremiumEntitlement } from "../../core/types";
|
|
7
8
|
import type { PeriodType } from "../../../subscription/core/SubscriptionConstants";
|
|
9
|
+
import { requireFirestore } from "../../../../shared/infrastructure/firestore";
|
|
8
10
|
|
|
9
11
|
const ANONYMOUS_CACHE_KEY = '__anonymous__';
|
|
10
12
|
|
|
@@ -39,6 +41,16 @@ function isAnonymousId(userId: string): boolean {
|
|
|
39
41
|
return userId.startsWith('$RCAnonymous') || userId.startsWith('device_');
|
|
40
42
|
}
|
|
41
43
|
|
|
44
|
+
async function syncRevenueCatIdToProfile(firebaseUserId: string, revenueCatUserId: string): Promise<void> {
|
|
45
|
+
try {
|
|
46
|
+
const db = requireFirestore();
|
|
47
|
+
const userRef = doc(db, "users", firebaseUserId);
|
|
48
|
+
await setDoc(userRef, { revenueCatUserId }, { merge: true });
|
|
49
|
+
} catch {
|
|
50
|
+
// Non-fatal: profile update failure should not block SDK initialization
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
42
54
|
export async function handleUserSwitch(
|
|
43
55
|
deps: InitializerDeps,
|
|
44
56
|
userId: string
|
|
@@ -108,6 +120,11 @@ async function performUserSwitch(
|
|
|
108
120
|
deps.setCurrentUserId(normalizedUserId || undefined);
|
|
109
121
|
const offerings = await fetchOfferingsSafe();
|
|
110
122
|
|
|
123
|
+
if (normalizedUserId) {
|
|
124
|
+
const rcId = await Purchases.getAppUserID();
|
|
125
|
+
void syncRevenueCatIdToProfile(normalizedUserId, rcId);
|
|
126
|
+
}
|
|
127
|
+
|
|
111
128
|
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
112
129
|
console.log('[UserSwitchHandler] ✅ User switch completed successfully');
|
|
113
130
|
}
|
|
@@ -170,6 +187,10 @@ export async function handleInitialConfiguration(
|
|
|
170
187
|
|
|
171
188
|
const currentUserId = await Purchases.getAppUserID();
|
|
172
189
|
|
|
190
|
+
if (normalizedUserId) {
|
|
191
|
+
void syncRevenueCatIdToProfile(normalizedUserId, currentUserId);
|
|
192
|
+
}
|
|
193
|
+
|
|
173
194
|
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
174
195
|
console.log('[UserSwitchHandler] ✅ Initial configuration completed:', {
|
|
175
196
|
revenueCatUserId: currentUserId,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { CustomerInfo } from "react-native-purchases";
|
|
2
|
+
import Purchases from "react-native-purchases";
|
|
2
3
|
import type { PeriodType, PurchaseSource } from "../core/SubscriptionConstants";
|
|
3
4
|
import { PURCHASE_SOURCE, PURCHASE_TYPE } from "../core/SubscriptionConstants";
|
|
4
5
|
import { getCreditsRepository } from "../../credits/infrastructure/CreditsRepositoryManager";
|
|
@@ -14,6 +15,14 @@ export class SubscriptionSyncProcessor {
|
|
|
14
15
|
private getAnonymousUserId: () => Promise<string>
|
|
15
16
|
) {}
|
|
16
17
|
|
|
18
|
+
private async getRevenueCatAppUserId(): Promise<string | null> {
|
|
19
|
+
try {
|
|
20
|
+
return await Purchases.getAppUserID();
|
|
21
|
+
} catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
17
26
|
private async getCreditsUserId(revenueCatUserId: string): Promise<string> {
|
|
18
27
|
if (!revenueCatUserId || revenueCatUserId.trim().length === 0) {
|
|
19
28
|
const anonymousId = await this.getAnonymousUserId();
|
|
@@ -28,6 +37,7 @@ export class SubscriptionSyncProcessor {
|
|
|
28
37
|
async processPurchase(userId: string, productId: string, customerInfo: CustomerInfo, source?: PurchaseSource, packageType?: PackageType | null) {
|
|
29
38
|
const revenueCatData = extractRevenueCatData(customerInfo, this.entitlementId);
|
|
30
39
|
revenueCatData.packageType = packageType ?? null;
|
|
40
|
+
revenueCatData.revenueCatUserId = await this.getRevenueCatAppUserId();
|
|
31
41
|
const purchaseId = generatePurchaseId(revenueCatData.originalTransactionId, productId);
|
|
32
42
|
|
|
33
43
|
const creditsUserId = await this.getCreditsUserId(userId);
|
|
@@ -47,6 +57,7 @@ export class SubscriptionSyncProcessor {
|
|
|
47
57
|
async processRenewal(userId: string, productId: string, newExpirationDate: string, customerInfo: CustomerInfo) {
|
|
48
58
|
const revenueCatData = extractRevenueCatData(customerInfo, this.entitlementId);
|
|
49
59
|
revenueCatData.expirationDate = newExpirationDate ?? revenueCatData.expirationDate;
|
|
60
|
+
revenueCatData.revenueCatUserId = await this.getRevenueCatAppUserId();
|
|
50
61
|
const purchaseId = generateRenewalId(revenueCatData.originalTransactionId, productId, newExpirationDate);
|
|
51
62
|
|
|
52
63
|
const creditsUserId = await this.getCreditsUserId(userId);
|
|
@@ -4,7 +4,6 @@ import { useAlert } from "@umituz/react-native-design-system";
|
|
|
4
4
|
import {
|
|
5
5
|
useAuthStore,
|
|
6
6
|
selectUserId,
|
|
7
|
-
selectIsAnonymous,
|
|
8
7
|
} from "@umituz/react-native-auth";
|
|
9
8
|
import { SubscriptionManager } from "../../infrastructure/managers/SubscriptionManager";
|
|
10
9
|
import { SUBSCRIPTION_QUERY_KEYS } from "./subscriptionQueryKeys";
|
|
@@ -19,7 +18,6 @@ interface PurchaseMutationResult {
|
|
|
19
18
|
|
|
20
19
|
export const usePurchasePackage = () => {
|
|
21
20
|
const userId = useAuthStore(selectUserId);
|
|
22
|
-
const isAnonymous = useAuthStore(selectIsAnonymous);
|
|
23
21
|
const queryClient = useQueryClient();
|
|
24
22
|
const { showSuccess, showError } = useAlert();
|
|
25
23
|
|
|
@@ -29,10 +27,6 @@ export const usePurchasePackage = () => {
|
|
|
29
27
|
throw new Error("User not authenticated");
|
|
30
28
|
}
|
|
31
29
|
|
|
32
|
-
if (isAnonymous) {
|
|
33
|
-
throw new Error("Anonymous users cannot purchase subscriptions");
|
|
34
|
-
}
|
|
35
|
-
|
|
36
30
|
const productId = pkg.product.identifier;
|
|
37
31
|
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
38
32
|
console.log(`[Purchase] Initializing and purchasing. User: ${userId}`);
|