@umituz/react-native-subscription 2.13.18 → 2.13.20
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.13.
|
|
3
|
+
"version": "2.13.20",
|
|
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",
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* Follows "Package Driven Design" by accepting dynamic props.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import { useState, useCallback } from "react";
|
|
9
|
+
import { useState, useCallback, useRef, useEffect } from "react";
|
|
10
10
|
import { Alert } from "react-native";
|
|
11
11
|
import { useLocalization } from "@umituz/react-native-localization";
|
|
12
12
|
import { usePremium } from "./usePremium";
|
|
@@ -36,6 +36,10 @@ export interface UsePaywallOperationsResult {
|
|
|
36
36
|
handleInAppPurchase: (pkg: PurchasesPackage) => Promise<boolean>;
|
|
37
37
|
/** Handle in-app restore (with auto-close logic) */
|
|
38
38
|
handleInAppRestore: () => Promise<boolean>;
|
|
39
|
+
/** Complete pending purchase after authentication */
|
|
40
|
+
completePendingPurchase: () => Promise<boolean>;
|
|
41
|
+
/** Clear pending package without purchasing */
|
|
42
|
+
clearPendingPackage: () => void;
|
|
39
43
|
}
|
|
40
44
|
|
|
41
45
|
export function usePaywallOperations({
|
|
@@ -49,6 +53,18 @@ export function usePaywallOperations({
|
|
|
49
53
|
const { purchasePackage, restorePurchase, closePaywall } = usePremium(userId);
|
|
50
54
|
const [pendingPackage, setPendingPackage] = useState<PurchasesPackage | null>(null);
|
|
51
55
|
|
|
56
|
+
// Ref to always have latest purchasePackage function (avoids stale closure)
|
|
57
|
+
const purchasePackageRef = useRef(purchasePackage);
|
|
58
|
+
const onPurchaseSuccessRef = useRef(onPurchaseSuccess);
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
purchasePackageRef.current = purchasePackage;
|
|
62
|
+
}, [purchasePackage]);
|
|
63
|
+
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
onPurchaseSuccessRef.current = onPurchaseSuccess;
|
|
66
|
+
}, [onPurchaseSuccess]);
|
|
67
|
+
|
|
52
68
|
/**
|
|
53
69
|
* Check if action requires authentication
|
|
54
70
|
* @returns true if authenticated, false if auth required
|
|
@@ -155,11 +171,48 @@ export function usePaywallOperations({
|
|
|
155
171
|
[executeRestore, closePaywall]
|
|
156
172
|
);
|
|
157
173
|
|
|
174
|
+
const completePendingPurchase = useCallback(async (): Promise<boolean> => {
|
|
175
|
+
if (!pendingPackage) {
|
|
176
|
+
if (__DEV__) {
|
|
177
|
+
console.log("[usePaywallOperations] No pending package to complete");
|
|
178
|
+
}
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (__DEV__) {
|
|
183
|
+
console.log("[usePaywallOperations] Completing pending purchase:", pendingPackage.identifier);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const pkg = pendingPackage;
|
|
187
|
+
setPendingPackage(null);
|
|
188
|
+
|
|
189
|
+
// Use ref to get latest purchasePackage (avoids stale closure after auth)
|
|
190
|
+
const success = await purchasePackageRef.current(pkg);
|
|
191
|
+
|
|
192
|
+
if (success) {
|
|
193
|
+
// Use ref to get latest callback
|
|
194
|
+
if (onPurchaseSuccessRef.current) onPurchaseSuccessRef.current();
|
|
195
|
+
} else {
|
|
196
|
+
Alert.alert(
|
|
197
|
+
t("premium.purchaseError"),
|
|
198
|
+
t("premium.purchaseErrorMessage")
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return success;
|
|
203
|
+
}, [pendingPackage, t]);
|
|
204
|
+
|
|
205
|
+
const clearPendingPackage = useCallback(() => {
|
|
206
|
+
setPendingPackage(null);
|
|
207
|
+
}, []);
|
|
208
|
+
|
|
158
209
|
return {
|
|
159
210
|
pendingPackage,
|
|
160
211
|
handlePurchase,
|
|
161
212
|
handleRestore,
|
|
162
213
|
handleInAppPurchase,
|
|
163
214
|
handleInAppRestore,
|
|
215
|
+
completePendingPurchase,
|
|
216
|
+
clearPendingPackage,
|
|
164
217
|
};
|
|
165
218
|
}
|