@umituz/react-native-subscription 2.15.4 → 2.15.6

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.15.4",
3
+ "version": "2.15.6",
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",
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Subscription Duration Configuration
3
+ * Production subscription durations by package type
4
+ */
5
+
6
+ import type { SubscriptionPackageType } from '../../../utils/packageTypeDetector';
7
+
8
+ /**
9
+ * Subscription durations in days
10
+ */
11
+ export const SUBSCRIPTION_DURATIONS: Record<SubscriptionPackageType, number> = {
12
+ weekly: 7,
13
+ monthly: 30,
14
+ yearly: 365,
15
+ unknown: 30,
16
+ };
17
+
18
+ /**
19
+ * Get subscription duration in days for a package type
20
+ */
21
+ export function getProductionDurationDays(packageType: SubscriptionPackageType): number {
22
+ return SUBSCRIPTION_DURATIONS[packageType] ?? SUBSCRIPTION_DURATIONS.unknown;
23
+ }
@@ -1,109 +1,49 @@
1
1
  /**
2
2
  * Expiration Date Calculator
3
- * Handles RevenueCat expiration date extraction with edge case handling
4
- * Includes sandbox-to-production date conversion for better testing UX
3
+ * Handles RevenueCat expiration date edge case
4
+ *
5
+ * Problem: RevenueCat sometimes returns expiration date = purchase date
6
+ * Solution: If same day, add subscription period based on package type
5
7
  */
6
8
 
7
9
  import type { RevenueCatEntitlement } from '../../domain/types/RevenueCatTypes';
8
- import { detectPackageType, type SubscriptionPackageType } from '../../../utils/packageTypeDetector';
9
-
10
- /**
11
- * Add production subscription period to a date
12
- */
13
- function addProductionPeriod(date: Date, packageType: SubscriptionPackageType): Date {
14
- const newDate = new Date(date);
15
-
16
- switch (packageType) {
17
- case 'weekly':
18
- newDate.setDate(newDate.getDate() + 7);
19
- break;
20
- case 'monthly':
21
- newDate.setDate(newDate.getDate() + 30);
22
- break;
23
- case 'yearly':
24
- newDate.setDate(newDate.getDate() + 365);
25
- break;
26
- default:
27
- newDate.setDate(newDate.getDate() + 30);
28
- break;
29
- }
30
-
31
- return newDate;
32
- }
10
+ import { detectPackageType } from '../../../utils/packageTypeDetector';
11
+ import { addProductionPeriod } from './SandboxDurationConverter';
33
12
 
34
13
  /**
35
- * Convert sandbox expiration to production-equivalent expiration
36
- *
37
- * Apple Sandbox durations:
38
- * - Weekly: 3 minutes → 7 days
39
- * - Monthly: 5 minutes → 30 days
40
- * - Yearly: 1 hour → 365 days
41
- *
42
- * For better testing UX, we calculate what the expiration would be in production
43
- * based on the latest purchase date.
14
+ * Check if two dates are on the same day
44
15
  */
45
- function convertSandboxToProduction(
46
- latestPurchaseDate: string | null,
47
- productIdentifier: string
48
- ): string {
49
- const purchaseDate = latestPurchaseDate
50
- ? new Date(latestPurchaseDate)
51
- : new Date();
52
-
53
- const packageType = detectPackageType(productIdentifier);
54
- const productionExpiration = addProductionPeriod(purchaseDate, packageType);
55
-
56
- return productionExpiration.toISOString();
16
+ function isSameDay(date1: Date, date2: Date): boolean {
17
+ return (
18
+ date1.getFullYear() === date2.getFullYear() &&
19
+ date1.getMonth() === date2.getMonth() &&
20
+ date1.getDate() === date2.getDate()
21
+ );
57
22
  }
58
23
 
59
24
  /**
60
- * Adjust expiration date for edge cases
25
+ * Get expiration date from entitlement
61
26
  *
62
- * Handles two scenarios:
63
- * 1. Sandbox mode: Convert to production-equivalent dates for better UX
64
- * 2. Same-day expiration bug: Add period if expiration is in the past
27
+ * Handles edge case: If expiration date equals purchase date (same day),
28
+ * calculates correct expiration by adding subscription period.
65
29
  */
66
- function adjustExpirationDate(
67
- expirationDate: string,
68
- productIdentifier: string,
69
- isSandbox: boolean,
70
- latestPurchaseDate: string | null
71
- ): string {
72
- // In sandbox mode, show production-equivalent expiration for better testing UX
73
- if (isSandbox && __DEV__) {
74
- return convertSandboxToProduction(latestPurchaseDate, productIdentifier);
75
- }
76
-
77
- const expDate = new Date(expirationDate);
78
- const now = new Date();
79
-
80
- // If expiration is in the future, no adjustment needed
81
- if (expDate > now) {
82
- return expirationDate;
83
- }
84
-
85
- // If expiration is today or past, add subscription period
86
- const packageType = detectPackageType(productIdentifier);
87
- const adjustedDate = addProductionPeriod(expDate, packageType);
88
-
89
- return adjustedDate.toISOString();
90
- }
91
-
92
30
  export function getExpirationDate(
93
31
  entitlement: RevenueCatEntitlement | null
94
32
  ): string | null {
95
- if (!entitlement) {
33
+ if (!entitlement?.expirationDate) {
96
34
  return null;
97
35
  }
98
36
 
99
- if (!entitlement.expirationDate) {
100
- return null;
37
+ const expDate = new Date(entitlement.expirationDate);
38
+ const purchaseDate = entitlement.latestPurchaseDate
39
+ ? new Date(entitlement.latestPurchaseDate)
40
+ : null;
41
+
42
+ // Only adjust if expiration equals purchase date (same day bug)
43
+ if (purchaseDate && isSameDay(expDate, purchaseDate)) {
44
+ const packageType = detectPackageType(entitlement.productIdentifier);
45
+ return addProductionPeriod(purchaseDate, packageType).toISOString();
101
46
  }
102
47
 
103
- return adjustExpirationDate(
104
- entitlement.expirationDate,
105
- entitlement.productIdentifier,
106
- entitlement.isSandbox ?? false,
107
- entitlement.latestPurchaseDate ?? null
108
- );
48
+ return entitlement.expirationDate;
109
49
  }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Subscription Duration Utilities
3
+ * Calculates subscription period based on package type
4
+ */
5
+
6
+ import type { SubscriptionPackageType } from '../../../utils/packageTypeDetector';
7
+ import { getProductionDurationDays } from '../config/SandboxDurationConfig';
8
+
9
+ /**
10
+ * Add subscription period to a date based on package type
11
+ */
12
+ export function addProductionPeriod(
13
+ date: Date,
14
+ packageType: SubscriptionPackageType
15
+ ): Date {
16
+ const newDate = new Date(date);
17
+ const daysToAdd = getProductionDurationDays(packageType);
18
+ newDate.setDate(newDate.getDate() + daysToAdd);
19
+ return newDate;
20
+ }