@umituz/react-native-subscription 2.35.3 → 2.35.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 +1 -1
- package/src/domains/subscription/application/SubscriptionSyncProcessor.ts +24 -8
- package/src/domains/subscription/application/SubscriptionSyncService.ts +5 -2
- package/src/domains/subscription/application/initializer/BackgroundInitializer.ts +10 -10
- package/src/domains/subscription/application/initializer/ServiceConfigurator.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-subscription",
|
|
3
|
-
"version": "2.35.
|
|
3
|
+
"version": "2.35.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",
|
|
@@ -9,14 +9,26 @@ import { handleExpiredSubscription, handleFreeUserInitialization, handlePremiumS
|
|
|
9
9
|
import { NO_SUBSCRIPTION_PRODUCT_ID } from "./syncConstants";
|
|
10
10
|
|
|
11
11
|
export class SubscriptionSyncProcessor {
|
|
12
|
-
constructor(
|
|
12
|
+
constructor(
|
|
13
|
+
private entitlementId: string,
|
|
14
|
+
private getAnonymousUserId: () => Promise<string>
|
|
15
|
+
) {}
|
|
16
|
+
|
|
17
|
+
private async getCreditsUserId(revenueCatUserId: string): Promise<string> {
|
|
18
|
+
if (!revenueCatUserId || revenueCatUserId.length === 0) {
|
|
19
|
+
return this.getAnonymousUserId();
|
|
20
|
+
}
|
|
21
|
+
return revenueCatUserId;
|
|
22
|
+
}
|
|
13
23
|
|
|
14
24
|
async processPurchase(userId: string, productId: string, customerInfo: CustomerInfo, source?: PurchaseSource) {
|
|
15
25
|
const revenueCatData = extractRevenueCatData(customerInfo, this.entitlementId);
|
|
16
26
|
const purchaseId = generatePurchaseId(revenueCatData.originalTransactionId, productId);
|
|
17
27
|
|
|
28
|
+
const creditsUserId = await this.getCreditsUserId(userId);
|
|
29
|
+
|
|
18
30
|
await getCreditsRepository().initializeCredits(
|
|
19
|
-
|
|
31
|
+
creditsUserId,
|
|
20
32
|
purchaseId,
|
|
21
33
|
productId,
|
|
22
34
|
source ?? PURCHASE_SOURCE.SETTINGS,
|
|
@@ -24,7 +36,7 @@ export class SubscriptionSyncProcessor {
|
|
|
24
36
|
PURCHASE_TYPE.INITIAL
|
|
25
37
|
);
|
|
26
38
|
|
|
27
|
-
emitCreditsUpdated(
|
|
39
|
+
emitCreditsUpdated(creditsUserId);
|
|
28
40
|
}
|
|
29
41
|
|
|
30
42
|
async processRenewal(userId: string, productId: string, newExpirationDate: string, customerInfo: CustomerInfo) {
|
|
@@ -32,8 +44,10 @@ export class SubscriptionSyncProcessor {
|
|
|
32
44
|
revenueCatData.expirationDate = newExpirationDate || revenueCatData.expirationDate;
|
|
33
45
|
const purchaseId = generateRenewalId(revenueCatData.originalTransactionId, productId, newExpirationDate);
|
|
34
46
|
|
|
47
|
+
const creditsUserId = await this.getCreditsUserId(userId);
|
|
48
|
+
|
|
35
49
|
await getCreditsRepository().initializeCredits(
|
|
36
|
-
|
|
50
|
+
creditsUserId,
|
|
37
51
|
purchaseId,
|
|
38
52
|
productId,
|
|
39
53
|
PURCHASE_SOURCE.RENEWAL,
|
|
@@ -41,7 +55,7 @@ export class SubscriptionSyncProcessor {
|
|
|
41
55
|
PURCHASE_TYPE.RENEWAL
|
|
42
56
|
);
|
|
43
57
|
|
|
44
|
-
emitCreditsUpdated(
|
|
58
|
+
emitCreditsUpdated(creditsUserId);
|
|
45
59
|
}
|
|
46
60
|
|
|
47
61
|
async processStatusChange(
|
|
@@ -52,21 +66,23 @@ export class SubscriptionSyncProcessor {
|
|
|
52
66
|
willRenew?: boolean,
|
|
53
67
|
periodType?: PeriodType
|
|
54
68
|
) {
|
|
69
|
+
const creditsUserId = await this.getCreditsUserId(userId);
|
|
70
|
+
|
|
55
71
|
// Expired subscription case
|
|
56
72
|
if (!isPremium && productId) {
|
|
57
|
-
await handleExpiredSubscription(
|
|
73
|
+
await handleExpiredSubscription(creditsUserId);
|
|
58
74
|
return;
|
|
59
75
|
}
|
|
60
76
|
|
|
61
77
|
// Free user case
|
|
62
78
|
if (!isPremium && !productId) {
|
|
63
|
-
await handleFreeUserInitialization(
|
|
79
|
+
await handleFreeUserInitialization(creditsUserId);
|
|
64
80
|
return;
|
|
65
81
|
}
|
|
66
82
|
|
|
67
83
|
// Premium user case
|
|
68
84
|
await handlePremiumStatusSync(
|
|
69
|
-
|
|
85
|
+
creditsUserId,
|
|
70
86
|
isPremium,
|
|
71
87
|
productId ?? NO_SUBSCRIPTION_PRODUCT_ID,
|
|
72
88
|
expiresAt ?? null,
|
|
@@ -6,8 +6,11 @@ import { SubscriptionSyncProcessor } from "./SubscriptionSyncProcessor";
|
|
|
6
6
|
export class SubscriptionSyncService {
|
|
7
7
|
private processor: SubscriptionSyncProcessor;
|
|
8
8
|
|
|
9
|
-
constructor(
|
|
10
|
-
|
|
9
|
+
constructor(
|
|
10
|
+
entitlementId: string,
|
|
11
|
+
getAnonymousUserId: () => Promise<string>
|
|
12
|
+
) {
|
|
13
|
+
this.processor = new SubscriptionSyncProcessor(entitlementId, getAnonymousUserId);
|
|
11
14
|
}
|
|
12
15
|
|
|
13
16
|
async handlePurchase(userId: string, productId: string, customerInfo: CustomerInfo, source?: PurchaseSource) {
|
|
@@ -5,11 +5,11 @@ import type { SubscriptionInitConfig } from "../SubscriptionInitializerTypes";
|
|
|
5
5
|
declare const __DEV__: boolean;
|
|
6
6
|
|
|
7
7
|
export async function startBackgroundInitialization(config: SubscriptionInitConfig): Promise<() => void> {
|
|
8
|
-
const initializeInBackground = async (
|
|
8
|
+
const initializeInBackground = async (revenueCatUserId?: string): Promise<void> => {
|
|
9
9
|
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
10
|
-
console.log('[BackgroundInitializer] initializeInBackground called with userId:',
|
|
10
|
+
console.log('[BackgroundInitializer] initializeInBackground called with userId:', revenueCatUserId || '(undefined - anonymous)');
|
|
11
11
|
}
|
|
12
|
-
await SubscriptionManager.initialize(
|
|
12
|
+
await SubscriptionManager.initialize(revenueCatUserId);
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
const auth = config.getFirebaseAuth();
|
|
@@ -21,22 +21,22 @@ export async function startBackgroundInitialization(config: SubscriptionInitConf
|
|
|
21
21
|
console.log('[BackgroundInitializer] Starting background initialization');
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
const
|
|
24
|
+
const initialRevenueCatUserId = getCurrentUserId(() => auth);
|
|
25
25
|
|
|
26
26
|
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
27
|
-
console.log('[BackgroundInitializer] Initial userId:',
|
|
27
|
+
console.log('[BackgroundInitializer] Initial RevenueCat userId:', initialRevenueCatUserId || '(undefined - anonymous)');
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
await initializeInBackground(
|
|
30
|
+
await initializeInBackground(initialRevenueCatUserId);
|
|
31
31
|
|
|
32
|
-
const unsubscribe = setupAuthStateListener(() => auth, async (
|
|
32
|
+
const unsubscribe = setupAuthStateListener(() => auth, async (newRevenueCatUserId) => {
|
|
33
33
|
if (typeof __DEV__ !== 'undefined' && __DEV__) {
|
|
34
|
-
console.log('[BackgroundInitializer] Auth state listener triggered, reinitializing with userId:',
|
|
34
|
+
console.log('[BackgroundInitializer] Auth state listener triggered, reinitializing with userId:', newRevenueCatUserId || '(undefined - anonymous)');
|
|
35
35
|
}
|
|
36
36
|
try {
|
|
37
|
-
await initializeInBackground(
|
|
37
|
+
await initializeInBackground(newRevenueCatUserId);
|
|
38
38
|
} catch (error) {
|
|
39
|
-
console.error('[BackgroundInitializer] Failed to reinitialize on auth change', { userId:
|
|
39
|
+
console.error('[BackgroundInitializer] Failed to reinitialize on auth change', { userId: newRevenueCatUserId, error });
|
|
40
40
|
}
|
|
41
41
|
});
|
|
42
42
|
|
|
@@ -5,7 +5,7 @@ import { SubscriptionSyncService } from "../SubscriptionSyncService";
|
|
|
5
5
|
import type { SubscriptionInitConfig } from "../SubscriptionInitializerTypes";
|
|
6
6
|
|
|
7
7
|
export function configureServices(config: SubscriptionInitConfig, apiKey: string): SubscriptionSyncService {
|
|
8
|
-
const { entitlementId, credits, creditPackages, getFirebaseAuth, showAuthModal, onCreditsUpdated } = config;
|
|
8
|
+
const { entitlementId, credits, creditPackages, getFirebaseAuth, showAuthModal, onCreditsUpdated, getAnonymousUserId } = config;
|
|
9
9
|
|
|
10
10
|
if (!creditPackages) {
|
|
11
11
|
throw new Error('[ServiceConfigurator] creditPackages configuration is required');
|
|
@@ -16,7 +16,7 @@ export function configureServices(config: SubscriptionInitConfig, apiKey: string
|
|
|
16
16
|
creditPackageAmounts: creditPackages.amounts
|
|
17
17
|
});
|
|
18
18
|
|
|
19
|
-
const syncService = new SubscriptionSyncService(entitlementId);
|
|
19
|
+
const syncService = new SubscriptionSyncService(entitlementId, getAnonymousUserId);
|
|
20
20
|
|
|
21
21
|
SubscriptionManager.configure({
|
|
22
22
|
config: {
|