@umituz/react-native-subscription 2.23.1 → 2.24.0

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.23.1",
3
+ "version": "2.24.0",
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",
@@ -17,6 +17,8 @@ export interface UseDeductCreditParams {
17
17
  }
18
18
 
19
19
  export interface UseDeductCreditResult {
20
+ /** Check if user has enough credits (server-side validation) */
21
+ checkCredits: (cost?: number) => Promise<boolean>;
20
22
  deductCredit: (cost?: number) => Promise<boolean>;
21
23
  deductCredits: (cost: number) => Promise<boolean>;
22
24
  isDeducting: boolean;
@@ -73,5 +75,10 @@ export const useDeductCredit = ({
73
75
  return await deductCredit(cost);
74
76
  }, [deductCredit]);
75
77
 
76
- return { deductCredit, deductCredits, isDeducting: mutation.isPending };
78
+ const checkCredits = useCallback(async (cost: number = 1): Promise<boolean> => {
79
+ if (!userId) return false;
80
+ return repository.hasCredits(userId, cost);
81
+ }, [userId, repository]);
82
+
83
+ return { checkCredits, deductCredit, deductCredits, isDeducting: mutation.isPending };
77
84
  };
@@ -3,7 +3,7 @@
3
3
  * Combines auth, subscription, and credits gates into a unified feature gate.
4
4
  */
5
5
 
6
- import { useCallback } from "react";
6
+ import { useCallback, useRef, useEffect } from "react";
7
7
  import { useAuthGate } from "./useAuthGate";
8
8
  import { useSubscriptionGate } from "./useSubscriptionGate";
9
9
  import { useCreditsGate } from "./useCreditsGate";
@@ -55,6 +55,24 @@ export function useFeatureGate(
55
55
  onShowPaywall,
56
56
  } = params;
57
57
 
58
+ // Store pending action for execution after purchase
59
+ const pendingActionRef = useRef<(() => void | Promise<void>) | null>(null);
60
+ const prevHasCreditsRef = useRef(hasCredits);
61
+
62
+ // Execute pending action when credits become available
63
+ useEffect(() => {
64
+ const hadNoCredits = !prevHasCreditsRef.current;
65
+ const nowHasCredits = hasCredits;
66
+
67
+ if (hadNoCredits && nowHasCredits && pendingActionRef.current) {
68
+ const action = pendingActionRef.current;
69
+ pendingActionRef.current = null;
70
+ action();
71
+ }
72
+
73
+ prevHasCreditsRef.current = hasCredits;
74
+ }, [hasCredits]);
75
+
58
76
  // Compose individual gates
59
77
  const authGate = useAuthGate({
60
78
  isAuthenticated,
@@ -90,6 +108,8 @@ export function useFeatureGate(
90
108
 
91
109
  // Step 3: Credits check
92
110
  if (!creditsGate.requireCredits(() => {})) {
111
+ // Store pending action for execution after purchase
112
+ pendingActionRef.current = action;
93
113
  return;
94
114
  }
95
115