@umituz/react-native-subscription 2.20.3 → 2.20.5
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.20.
|
|
3
|
+
"version": "2.20.5",
|
|
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",
|
|
@@ -95,6 +95,7 @@ export async function initializeCreditsTransaction(
|
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
// Create purchase metadata for history (only if productId and source exists and packageType detected)
|
|
98
|
+
// NOTE: Cannot use serverTimestamp() in arrays, using Date.now() instead
|
|
98
99
|
const purchaseMetadata: PurchaseMetadata | undefined =
|
|
99
100
|
productId && metadata?.source && packageType && packageType !== "unknown" ? {
|
|
100
101
|
productId,
|
|
@@ -104,7 +105,7 @@ export async function initializeCreditsTransaction(
|
|
|
104
105
|
type: purchaseType,
|
|
105
106
|
platform,
|
|
106
107
|
appVersion,
|
|
107
|
-
timestamp: now as any,
|
|
108
|
+
timestamp: Date.now() as any, // Use Date.now() instead of serverTimestamp() for arrays
|
|
108
109
|
} : undefined;
|
|
109
110
|
|
|
110
111
|
// Update purchase history (keep last 10, only if metadata exists)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from "./useAuthAwarePurchase";
|
|
2
2
|
export * from "./useAuthGate";
|
|
3
3
|
export * from "./useAuthSubscriptionSync";
|
|
4
|
+
export * from "./useSavedPurchaseAutoExecution";
|
|
4
5
|
export * from "./useCreditChecker";
|
|
5
6
|
export * from "./useCredits";
|
|
6
7
|
export * from "./useCreditsGate";
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Saved Purchase Auto-Execution Hook
|
|
3
|
+
* Automatically executes saved purchase when user converts from anonymous to authenticated
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { useEffect, useRef, useCallback } from "react";
|
|
7
|
+
import { getSavedPurchase, clearSavedPurchase } from "./useAuthAwarePurchase";
|
|
8
|
+
import { usePremium } from "./usePremium";
|
|
9
|
+
import { SubscriptionManager } from "../../revenuecat/infrastructure/services/SubscriptionManager";
|
|
10
|
+
|
|
11
|
+
declare const __DEV__: boolean;
|
|
12
|
+
|
|
13
|
+
export interface UseSavedPurchaseAutoExecutionParams {
|
|
14
|
+
userId?: string;
|
|
15
|
+
isAnonymous?: boolean;
|
|
16
|
+
onSuccess?: () => void;
|
|
17
|
+
onError?: (error: Error) => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface UseSavedPurchaseAutoExecutionResult {
|
|
21
|
+
isExecuting: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const useSavedPurchaseAutoExecution = (
|
|
25
|
+
params: UseSavedPurchaseAutoExecutionParams
|
|
26
|
+
): UseSavedPurchaseAutoExecutionResult => {
|
|
27
|
+
const { userId, isAnonymous, onSuccess, onError } = params;
|
|
28
|
+
const { purchasePackage } = usePremium(userId);
|
|
29
|
+
|
|
30
|
+
const prevIsAnonymousRef = useRef<boolean | undefined>(undefined);
|
|
31
|
+
const isExecutingRef = useRef(false);
|
|
32
|
+
|
|
33
|
+
const executeWithWait = useCallback(async () => {
|
|
34
|
+
const savedPurchase = getSavedPurchase();
|
|
35
|
+
if (!savedPurchase) return;
|
|
36
|
+
|
|
37
|
+
if (__DEV__) {
|
|
38
|
+
console.log(
|
|
39
|
+
"[SavedPurchaseAutoExecution] Waiting for RevenueCat initialization..."
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
isExecutingRef.current = true;
|
|
44
|
+
|
|
45
|
+
const maxAttempts = 20;
|
|
46
|
+
const delayMs = 500;
|
|
47
|
+
|
|
48
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
49
|
+
const isReady = SubscriptionManager.isInitializedForUser(userId);
|
|
50
|
+
|
|
51
|
+
if (__DEV__) {
|
|
52
|
+
console.log(
|
|
53
|
+
`[SavedPurchaseAutoExecution] Attempt ${attempt + 1}/${maxAttempts}, isReady: ${isReady}`
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (isReady) {
|
|
58
|
+
const pkg = savedPurchase.pkg;
|
|
59
|
+
clearSavedPurchase();
|
|
60
|
+
|
|
61
|
+
if (__DEV__) {
|
|
62
|
+
console.log(
|
|
63
|
+
"[SavedPurchaseAutoExecution] RevenueCat ready, executing purchase:",
|
|
64
|
+
pkg.product.identifier
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
const success = await purchasePackage(pkg);
|
|
70
|
+
|
|
71
|
+
if (__DEV__) {
|
|
72
|
+
console.log("[SavedPurchaseAutoExecution] Purchase result:", success);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (success && onSuccess) {
|
|
76
|
+
onSuccess();
|
|
77
|
+
}
|
|
78
|
+
} catch (error) {
|
|
79
|
+
if (__DEV__) {
|
|
80
|
+
console.error("[SavedPurchaseAutoExecution] Purchase failed:", error);
|
|
81
|
+
}
|
|
82
|
+
if (onError && error instanceof Error) {
|
|
83
|
+
onError(error);
|
|
84
|
+
}
|
|
85
|
+
} finally {
|
|
86
|
+
isExecutingRef.current = false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (__DEV__) {
|
|
96
|
+
console.log(
|
|
97
|
+
"[SavedPurchaseAutoExecution] Timeout waiting for RevenueCat, clearing saved purchase"
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
clearSavedPurchase();
|
|
101
|
+
isExecutingRef.current = false;
|
|
102
|
+
}, [userId, purchasePackage, onSuccess, onError]);
|
|
103
|
+
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
const isAuthenticated = !!userId && !isAnonymous;
|
|
106
|
+
const prevIsAnonymous = prevIsAnonymousRef.current;
|
|
107
|
+
const savedPurchase = getSavedPurchase();
|
|
108
|
+
|
|
109
|
+
const wasAnonymous = prevIsAnonymous === true;
|
|
110
|
+
const becameAuthenticated = wasAnonymous && isAuthenticated;
|
|
111
|
+
|
|
112
|
+
if (__DEV__) {
|
|
113
|
+
console.log("[SavedPurchaseAutoExecution] Check:", {
|
|
114
|
+
userId: userId?.slice(0, 8),
|
|
115
|
+
prevIsAnonymous,
|
|
116
|
+
isAnonymous,
|
|
117
|
+
isAuthenticated,
|
|
118
|
+
wasAnonymous,
|
|
119
|
+
becameAuthenticated,
|
|
120
|
+
hasSavedPurchase: !!savedPurchase,
|
|
121
|
+
willExecute: becameAuthenticated && !!savedPurchase && !isExecutingRef.current,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (becameAuthenticated && savedPurchase && !isExecutingRef.current) {
|
|
126
|
+
executeWithWait();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
prevIsAnonymousRef.current = isAnonymous;
|
|
130
|
+
}, [userId, isAnonymous, executeWithWait]);
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
isExecuting: isExecutingRef.current,
|
|
134
|
+
};
|
|
135
|
+
};
|