react-native-iap 14.0.1 → 14.1.1-rc.1
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/android/src/main/java/com/margelo/nitro/iap/HybridRnIap.kt +81 -22
- package/app.plugin.js +1 -1
- package/ios/HybridRnIap.swift +167 -11
- package/ios/ProductStore.swift +10 -0
- package/ios/reactnativeiap.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
- package/ios/reactnativeiap.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
- package/lib/module/helpers/subscription.js +9 -1
- package/lib/module/helpers/subscription.js.map +1 -1
- package/lib/module/hooks/useIAP.js +9 -13
- package/lib/module/hooks/useIAP.js.map +1 -1
- package/lib/module/index.js +43 -28
- package/lib/module/index.js.map +1 -1
- package/lib/module/types.js +24 -16
- package/lib/module/types.js.map +1 -1
- package/lib/module/utils/error.js.map +1 -1
- package/lib/module/utils/type-bridge.js +64 -13
- package/lib/module/utils/type-bridge.js.map +1 -1
- package/lib/typescript/src/helpers/subscription.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useIAP.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +12 -22
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/specs/RnIap.nitro.d.ts +15 -11
- package/lib/typescript/src/specs/RnIap.nitro.d.ts.map +1 -1
- package/lib/typescript/src/types.d.ts +58 -58
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/lib/typescript/src/utils/error.d.ts.map +1 -1
- package/lib/typescript/src/utils/type-bridge.d.ts.map +1 -1
- package/nitro.json +5 -1
- package/nitrogen/generated/android/c++/JHybridRnIapSpec.cpp +13 -4
- package/nitrogen/generated/android/c++/JHybridRnIapSpec.hpp +1 -1
- package/nitrogen/generated/android/c++/JNitroProduct.hpp +40 -36
- package/nitrogen/generated/android/c++/JNitroPurchase.hpp +12 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/iap/HybridRnIapSpec.kt +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/iap/NitroProduct.kt +12 -9
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/iap/NitroPurchase.kt +9 -0
- package/nitrogen/generated/ios/c++/HybridRnIapSpecSwift.hpp +1 -1
- package/nitrogen/generated/ios/swift/HybridRnIapSpec.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridRnIapSpec_cxx.swift +13 -7
- package/nitrogen/generated/ios/swift/NitroProduct.swift +73 -43
- package/nitrogen/generated/ios/swift/NitroPurchase.swift +35 -2
- package/nitrogen/generated/shared/c++/HybridRnIapSpec.hpp +1 -1
- package/nitrogen/generated/shared/c++/NitroProduct.hpp +41 -37
- package/nitrogen/generated/shared/c++/NitroPurchase.hpp +13 -1
- package/package.json +9 -2
- package/plugin/build/src/withIAP.d.ts +3 -0
- package/plugin/build/src/withIAP.js +81 -0
- package/plugin/build/tsconfig.tsbuildinfo +1 -0
- package/plugin/tsconfig.tsbuildinfo +1 -1
- package/src/helpers/subscription.ts +36 -25
- package/src/hooks/useIAP.ts +188 -201
- package/src/index.ts +377 -356
- package/src/specs/RnIap.nitro.ts +15 -11
- package/src/types.ts +66 -62
- package/src/utils/error.ts +19 -19
- package/src/utils/type-bridge.ts +138 -75
package/src/hooks/useIAP.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// External dependencies
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import {useCallback, useEffect, useState, useRef} from 'react';
|
|
3
|
+
import {Platform} from 'react-native';
|
|
4
4
|
|
|
5
5
|
// Internal modules
|
|
6
6
|
import {
|
|
@@ -16,8 +16,8 @@ import {
|
|
|
16
16
|
validateReceipt as validateReceiptInternal,
|
|
17
17
|
getActiveSubscriptions,
|
|
18
18
|
hasActiveSubscriptions,
|
|
19
|
-
} from '../'
|
|
20
|
-
import {
|
|
19
|
+
} from '../';
|
|
20
|
+
import {syncIOS, requestPromotedProductIOS, buyPromotedProductIOS} from '../';
|
|
21
21
|
|
|
22
22
|
// Types
|
|
23
23
|
import type {
|
|
@@ -29,76 +29,76 @@ import type {
|
|
|
29
29
|
RequestPurchaseProps,
|
|
30
30
|
RequestSubscriptionProps,
|
|
31
31
|
ActiveSubscription,
|
|
32
|
-
} from '../types'
|
|
32
|
+
} from '../types';
|
|
33
33
|
|
|
34
34
|
// Types for event subscriptions
|
|
35
35
|
interface EventSubscription {
|
|
36
|
-
remove(): void
|
|
36
|
+
remove(): void;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
type UseIap = {
|
|
40
|
-
connected: boolean
|
|
41
|
-
products: Product[]
|
|
42
|
-
promotedProductsIOS: Purchase[]
|
|
43
|
-
promotedProductIdIOS?: string
|
|
44
|
-
subscriptions: SubscriptionProduct[]
|
|
45
|
-
availablePurchases: Purchase[]
|
|
46
|
-
currentPurchase?: Purchase
|
|
47
|
-
currentPurchaseError?: PurchaseError
|
|
48
|
-
promotedProductIOS?: Product
|
|
49
|
-
activeSubscriptions: ActiveSubscription[]
|
|
50
|
-
clearCurrentPurchase: () => void
|
|
51
|
-
clearCurrentPurchaseError: () => void
|
|
40
|
+
connected: boolean;
|
|
41
|
+
products: Product[];
|
|
42
|
+
promotedProductsIOS: Purchase[];
|
|
43
|
+
promotedProductIdIOS?: string;
|
|
44
|
+
subscriptions: SubscriptionProduct[];
|
|
45
|
+
availablePurchases: Purchase[];
|
|
46
|
+
currentPurchase?: Purchase;
|
|
47
|
+
currentPurchaseError?: PurchaseError;
|
|
48
|
+
promotedProductIOS?: Product;
|
|
49
|
+
activeSubscriptions: ActiveSubscription[];
|
|
50
|
+
clearCurrentPurchase: () => void;
|
|
51
|
+
clearCurrentPurchaseError: () => void;
|
|
52
52
|
finishTransaction: ({
|
|
53
53
|
purchase,
|
|
54
54
|
isConsumable,
|
|
55
55
|
}: {
|
|
56
|
-
purchase: Purchase
|
|
57
|
-
isConsumable?: boolean
|
|
58
|
-
}) => Promise<PurchaseResult | boolean
|
|
59
|
-
getAvailablePurchases: () => Promise<void
|
|
56
|
+
purchase: Purchase;
|
|
57
|
+
isConsumable?: boolean;
|
|
58
|
+
}) => Promise<PurchaseResult | boolean>;
|
|
59
|
+
getAvailablePurchases: () => Promise<void>;
|
|
60
60
|
fetchProducts: (params: {
|
|
61
|
-
skus: string[]
|
|
62
|
-
type?: 'inapp' | 'subs'
|
|
63
|
-
}) => Promise<void
|
|
61
|
+
skus: string[];
|
|
62
|
+
type?: 'inapp' | 'subs';
|
|
63
|
+
}) => Promise<void>;
|
|
64
64
|
/**
|
|
65
65
|
* @deprecated Use fetchProducts({ skus, type: 'inapp' }) instead. This method will be removed in version 3.0.0.
|
|
66
66
|
* Note: This method internally uses fetchProducts, so no deprecation warning is shown.
|
|
67
67
|
*/
|
|
68
|
-
getProducts: (skus: string[]) => Promise<void
|
|
68
|
+
getProducts: (skus: string[]) => Promise<void>;
|
|
69
69
|
/**
|
|
70
70
|
* @deprecated Use fetchProducts({ skus, type: 'subs' }) instead. This method will be removed in version 3.0.0.
|
|
71
71
|
* Note: This method internally uses fetchProducts, so no deprecation warning is shown.
|
|
72
72
|
*/
|
|
73
|
-
getSubscriptions: (skus: string[]) => Promise<void
|
|
73
|
+
getSubscriptions: (skus: string[]) => Promise<void>;
|
|
74
74
|
requestPurchase: (params: {
|
|
75
|
-
request: RequestPurchaseProps | RequestSubscriptionProps
|
|
76
|
-
type?: 'inapp' | 'subs'
|
|
77
|
-
}) => Promise<any
|
|
75
|
+
request: RequestPurchaseProps | RequestSubscriptionProps;
|
|
76
|
+
type?: 'inapp' | 'subs';
|
|
77
|
+
}) => Promise<any>;
|
|
78
78
|
validateReceipt: (
|
|
79
79
|
sku: string,
|
|
80
80
|
androidOptions?: {
|
|
81
|
-
packageName: string
|
|
82
|
-
productToken: string
|
|
83
|
-
accessToken: string
|
|
84
|
-
isSub?: boolean
|
|
85
|
-
}
|
|
86
|
-
) => Promise<any
|
|
87
|
-
restorePurchases: () => Promise<void
|
|
88
|
-
requestPromotedProductIOS: () => Promise<any | null
|
|
89
|
-
buyPromotedProductIOS: () => Promise<void
|
|
81
|
+
packageName: string;
|
|
82
|
+
productToken: string;
|
|
83
|
+
accessToken: string;
|
|
84
|
+
isSub?: boolean;
|
|
85
|
+
},
|
|
86
|
+
) => Promise<any>;
|
|
87
|
+
restorePurchases: () => Promise<void>; // 구매 복원 함수 추가
|
|
88
|
+
requestPromotedProductIOS: () => Promise<any | null>;
|
|
89
|
+
buyPromotedProductIOS: () => Promise<void>;
|
|
90
90
|
getActiveSubscriptions: (
|
|
91
|
-
subscriptionIds?: string[]
|
|
92
|
-
) => Promise<ActiveSubscription[]
|
|
93
|
-
hasActiveSubscriptions: (subscriptionIds?: string[]) => Promise<boolean
|
|
94
|
-
}
|
|
91
|
+
subscriptionIds?: string[],
|
|
92
|
+
) => Promise<ActiveSubscription[]>;
|
|
93
|
+
hasActiveSubscriptions: (subscriptionIds?: string[]) => Promise<boolean>;
|
|
94
|
+
};
|
|
95
95
|
|
|
96
96
|
export interface UseIapOptions {
|
|
97
|
-
onPurchaseSuccess?: (purchase: Purchase) => void
|
|
98
|
-
onPurchaseError?: (error: PurchaseError) => void
|
|
99
|
-
onSyncError?: (error: Error) => void
|
|
100
|
-
shouldAutoSyncPurchases?: boolean // New option to control auto-syncing
|
|
101
|
-
onPromotedProductIOS?: (product: Product) => void
|
|
97
|
+
onPurchaseSuccess?: (purchase: Purchase) => void;
|
|
98
|
+
onPurchaseError?: (error: PurchaseError) => void;
|
|
99
|
+
onSyncError?: (error: Error) => void;
|
|
100
|
+
shouldAutoSyncPurchases?: boolean; // New option to control auto-syncing
|
|
101
|
+
onPromotedProductIOS?: (product: Product) => void;
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
/**
|
|
@@ -106,193 +106,193 @@ export interface UseIapOptions {
|
|
|
106
106
|
* See documentation at https://expo-iap.hyo.dev/docs/hooks/useIAP
|
|
107
107
|
*/
|
|
108
108
|
export function useIAP(options?: UseIapOptions): UseIap {
|
|
109
|
-
const [connected, setConnected] = useState<boolean>(false)
|
|
110
|
-
const [products, setProducts] = useState<Product[]>([])
|
|
111
|
-
const [promotedProductsIOS] = useState<Purchase[]>([])
|
|
112
|
-
const [subscriptions, setSubscriptions] = useState<SubscriptionProduct[]>([])
|
|
113
|
-
const [availablePurchases, setAvailablePurchases] = useState<Purchase[]>([])
|
|
114
|
-
const [currentPurchase, setCurrentPurchase] = useState<Purchase>()
|
|
115
|
-
const [promotedProductIOS, setPromotedProductIOS] = useState<Product>()
|
|
109
|
+
const [connected, setConnected] = useState<boolean>(false);
|
|
110
|
+
const [products, setProducts] = useState<Product[]>([]);
|
|
111
|
+
const [promotedProductsIOS] = useState<Purchase[]>([]);
|
|
112
|
+
const [subscriptions, setSubscriptions] = useState<SubscriptionProduct[]>([]);
|
|
113
|
+
const [availablePurchases, setAvailablePurchases] = useState<Purchase[]>([]);
|
|
114
|
+
const [currentPurchase, setCurrentPurchase] = useState<Purchase>();
|
|
115
|
+
const [promotedProductIOS, setPromotedProductIOS] = useState<Product>();
|
|
116
116
|
const [currentPurchaseError, setCurrentPurchaseError] =
|
|
117
|
-
useState<PurchaseError>()
|
|
118
|
-
const [promotedProductIdIOS] = useState<string>()
|
|
117
|
+
useState<PurchaseError>();
|
|
118
|
+
const [promotedProductIdIOS] = useState<string>();
|
|
119
119
|
const [activeSubscriptions, setActiveSubscriptions] = useState<
|
|
120
120
|
ActiveSubscription[]
|
|
121
|
-
>([])
|
|
121
|
+
>([]);
|
|
122
122
|
|
|
123
|
-
const optionsRef = useRef<UseIapOptions | undefined>(options)
|
|
123
|
+
const optionsRef = useRef<UseIapOptions | undefined>(options);
|
|
124
124
|
|
|
125
125
|
// Helper function to merge arrays with duplicate checking
|
|
126
126
|
const mergeWithDuplicateCheck = useCallback(
|
|
127
127
|
<T>(
|
|
128
128
|
existingItems: T[],
|
|
129
129
|
newItems: T[],
|
|
130
|
-
getKey: (item: T) => string
|
|
130
|
+
getKey: (item: T) => string,
|
|
131
131
|
): T[] => {
|
|
132
|
-
const merged = [...existingItems]
|
|
132
|
+
const merged = [...existingItems];
|
|
133
133
|
newItems.forEach((newItem) => {
|
|
134
134
|
const isDuplicate = merged.some(
|
|
135
|
-
(existingItem) => getKey(existingItem) === getKey(newItem)
|
|
136
|
-
)
|
|
135
|
+
(existingItem) => getKey(existingItem) === getKey(newItem),
|
|
136
|
+
);
|
|
137
137
|
if (!isDuplicate) {
|
|
138
|
-
merged.push(newItem)
|
|
138
|
+
merged.push(newItem);
|
|
139
139
|
}
|
|
140
|
-
})
|
|
141
|
-
return merged
|
|
140
|
+
});
|
|
141
|
+
return merged;
|
|
142
142
|
},
|
|
143
|
-
[]
|
|
144
|
-
)
|
|
143
|
+
[],
|
|
144
|
+
);
|
|
145
145
|
|
|
146
146
|
useEffect(() => {
|
|
147
|
-
optionsRef.current = options
|
|
148
|
-
}, [options])
|
|
147
|
+
optionsRef.current = options;
|
|
148
|
+
}, [options]);
|
|
149
149
|
|
|
150
150
|
const subscriptionsRef = useRef<{
|
|
151
|
-
purchaseUpdate?: EventSubscription
|
|
152
|
-
purchaseError?: EventSubscription
|
|
153
|
-
promotedProductsIOS?: EventSubscription
|
|
154
|
-
promotedProductIOS?: EventSubscription
|
|
155
|
-
}>({})
|
|
151
|
+
purchaseUpdate?: EventSubscription;
|
|
152
|
+
purchaseError?: EventSubscription;
|
|
153
|
+
promotedProductsIOS?: EventSubscription;
|
|
154
|
+
promotedProductIOS?: EventSubscription;
|
|
155
|
+
}>({});
|
|
156
156
|
|
|
157
|
-
const subscriptionsRefState = useRef<SubscriptionProduct[]>([])
|
|
157
|
+
const subscriptionsRefState = useRef<SubscriptionProduct[]>([]);
|
|
158
158
|
|
|
159
159
|
useEffect(() => {
|
|
160
|
-
subscriptionsRefState.current = subscriptions
|
|
161
|
-
}, [subscriptions])
|
|
160
|
+
subscriptionsRefState.current = subscriptions;
|
|
161
|
+
}, [subscriptions]);
|
|
162
162
|
|
|
163
163
|
const clearCurrentPurchase = useCallback(() => {
|
|
164
|
-
setCurrentPurchase(undefined)
|
|
165
|
-
}, [])
|
|
164
|
+
setCurrentPurchase(undefined);
|
|
165
|
+
}, []);
|
|
166
166
|
|
|
167
167
|
const clearCurrentPurchaseError = useCallback(() => {
|
|
168
|
-
setCurrentPurchaseError(undefined)
|
|
169
|
-
}, [])
|
|
168
|
+
setCurrentPurchaseError(undefined);
|
|
169
|
+
}, []);
|
|
170
170
|
|
|
171
171
|
const getProductsInternal = useCallback(
|
|
172
172
|
async (skus: string[]): Promise<void> => {
|
|
173
173
|
try {
|
|
174
|
-
const result = await fetchProducts({
|
|
174
|
+
const result = await fetchProducts({skus, type: 'inapp'});
|
|
175
175
|
setProducts((prevProducts: Product[]) =>
|
|
176
176
|
mergeWithDuplicateCheck(
|
|
177
177
|
prevProducts,
|
|
178
178
|
result as Product[],
|
|
179
|
-
(product: Product) => product.id
|
|
180
|
-
)
|
|
181
|
-
)
|
|
179
|
+
(product: Product) => product.id,
|
|
180
|
+
),
|
|
181
|
+
);
|
|
182
182
|
} catch (error) {
|
|
183
|
-
console.error('Error fetching products:', error)
|
|
183
|
+
console.error('Error fetching products:', error);
|
|
184
184
|
}
|
|
185
185
|
},
|
|
186
|
-
[mergeWithDuplicateCheck]
|
|
187
|
-
)
|
|
186
|
+
[mergeWithDuplicateCheck],
|
|
187
|
+
);
|
|
188
188
|
|
|
189
189
|
const getSubscriptionsInternal = useCallback(
|
|
190
190
|
async (skus: string[]): Promise<void> => {
|
|
191
191
|
try {
|
|
192
|
-
const result = await fetchProducts({
|
|
192
|
+
const result = await fetchProducts({skus, type: 'subs'});
|
|
193
193
|
setSubscriptions((prevSubscriptions: SubscriptionProduct[]) =>
|
|
194
194
|
mergeWithDuplicateCheck(
|
|
195
195
|
prevSubscriptions,
|
|
196
196
|
result as SubscriptionProduct[],
|
|
197
|
-
(subscription: SubscriptionProduct) => subscription.id
|
|
198
|
-
)
|
|
199
|
-
)
|
|
197
|
+
(subscription: SubscriptionProduct) => subscription.id,
|
|
198
|
+
),
|
|
199
|
+
);
|
|
200
200
|
} catch (error) {
|
|
201
|
-
console.error('Error fetching subscriptions:', error)
|
|
201
|
+
console.error('Error fetching subscriptions:', error);
|
|
202
202
|
}
|
|
203
203
|
},
|
|
204
|
-
[mergeWithDuplicateCheck]
|
|
205
|
-
)
|
|
204
|
+
[mergeWithDuplicateCheck],
|
|
205
|
+
);
|
|
206
206
|
|
|
207
207
|
const fetchProductsInternal = useCallback(
|
|
208
208
|
async (params: {
|
|
209
|
-
skus: string[]
|
|
210
|
-
type?: 'inapp' | 'subs'
|
|
209
|
+
skus: string[];
|
|
210
|
+
type?: 'inapp' | 'subs';
|
|
211
211
|
}): Promise<void> => {
|
|
212
212
|
try {
|
|
213
|
-
const result = await fetchProducts(params)
|
|
213
|
+
const result = await fetchProducts(params);
|
|
214
214
|
if (params.type === 'subs') {
|
|
215
215
|
setSubscriptions((prevSubscriptions: SubscriptionProduct[]) =>
|
|
216
216
|
mergeWithDuplicateCheck(
|
|
217
217
|
prevSubscriptions,
|
|
218
218
|
result as SubscriptionProduct[],
|
|
219
|
-
(subscription: SubscriptionProduct) => subscription.id
|
|
220
|
-
)
|
|
221
|
-
)
|
|
219
|
+
(subscription: SubscriptionProduct) => subscription.id,
|
|
220
|
+
),
|
|
221
|
+
);
|
|
222
222
|
} else {
|
|
223
223
|
setProducts((prevProducts: Product[]) =>
|
|
224
224
|
mergeWithDuplicateCheck(
|
|
225
225
|
prevProducts,
|
|
226
226
|
result as Product[],
|
|
227
|
-
(product: Product) => product.id
|
|
228
|
-
)
|
|
229
|
-
)
|
|
227
|
+
(product: Product) => product.id,
|
|
228
|
+
),
|
|
229
|
+
);
|
|
230
230
|
}
|
|
231
231
|
} catch (error) {
|
|
232
|
-
console.error('Error fetching products:', error)
|
|
232
|
+
console.error('Error fetching products:', error);
|
|
233
233
|
}
|
|
234
234
|
},
|
|
235
|
-
[mergeWithDuplicateCheck]
|
|
236
|
-
)
|
|
235
|
+
[mergeWithDuplicateCheck],
|
|
236
|
+
);
|
|
237
237
|
|
|
238
238
|
const getAvailablePurchasesInternal = useCallback(async (): Promise<void> => {
|
|
239
239
|
try {
|
|
240
|
-
const result = await getAvailablePurchases()
|
|
241
|
-
setAvailablePurchases(result)
|
|
240
|
+
const result = await getAvailablePurchases();
|
|
241
|
+
setAvailablePurchases(result);
|
|
242
242
|
} catch (error) {
|
|
243
|
-
console.error('Error fetching available purchases:', error)
|
|
243
|
+
console.error('Error fetching available purchases:', error);
|
|
244
244
|
}
|
|
245
|
-
}, [])
|
|
245
|
+
}, []);
|
|
246
246
|
|
|
247
247
|
const getActiveSubscriptionsInternal = useCallback(
|
|
248
248
|
async (subscriptionIds?: string[]): Promise<ActiveSubscription[]> => {
|
|
249
249
|
try {
|
|
250
|
-
const result = await getActiveSubscriptions(subscriptionIds)
|
|
251
|
-
setActiveSubscriptions(result)
|
|
252
|
-
return result
|
|
250
|
+
const result = await getActiveSubscriptions(subscriptionIds);
|
|
251
|
+
setActiveSubscriptions(result);
|
|
252
|
+
return result;
|
|
253
253
|
} catch (error) {
|
|
254
|
-
console.error('Error getting active subscriptions:', error)
|
|
254
|
+
console.error('Error getting active subscriptions:', error);
|
|
255
255
|
// Don't clear existing activeSubscriptions on error - preserve current state
|
|
256
256
|
// This prevents the UI from showing empty state when there are temporary network issues
|
|
257
|
-
return []
|
|
257
|
+
return [];
|
|
258
258
|
}
|
|
259
259
|
},
|
|
260
|
-
[]
|
|
261
|
-
)
|
|
260
|
+
[],
|
|
261
|
+
);
|
|
262
262
|
|
|
263
263
|
const hasActiveSubscriptionsInternal = useCallback(
|
|
264
264
|
async (subscriptionIds?: string[]): Promise<boolean> => {
|
|
265
265
|
try {
|
|
266
|
-
return await hasActiveSubscriptions(subscriptionIds)
|
|
266
|
+
return await hasActiveSubscriptions(subscriptionIds);
|
|
267
267
|
} catch (error) {
|
|
268
|
-
console.error('Error checking active subscriptions:', error)
|
|
269
|
-
return false
|
|
268
|
+
console.error('Error checking active subscriptions:', error);
|
|
269
|
+
return false;
|
|
270
270
|
}
|
|
271
271
|
},
|
|
272
|
-
[]
|
|
273
|
-
)
|
|
272
|
+
[],
|
|
273
|
+
);
|
|
274
274
|
|
|
275
275
|
const finishTransaction = useCallback(
|
|
276
276
|
async ({
|
|
277
277
|
purchase,
|
|
278
278
|
isConsumable,
|
|
279
279
|
}: {
|
|
280
|
-
purchase: Purchase
|
|
281
|
-
isConsumable?: boolean
|
|
280
|
+
purchase: Purchase;
|
|
281
|
+
isConsumable?: boolean;
|
|
282
282
|
}): Promise<PurchaseResult | boolean> => {
|
|
283
283
|
try {
|
|
284
284
|
return await finishTransactionInternal({
|
|
285
285
|
purchase,
|
|
286
286
|
isConsumable,
|
|
287
|
-
})
|
|
287
|
+
});
|
|
288
288
|
} catch (err) {
|
|
289
|
-
throw err
|
|
289
|
+
throw err;
|
|
290
290
|
} finally {
|
|
291
291
|
if (purchase.id === currentPurchase?.id) {
|
|
292
|
-
clearCurrentPurchase()
|
|
292
|
+
clearCurrentPurchase();
|
|
293
293
|
}
|
|
294
294
|
if (purchase.id === currentPurchaseError?.productId) {
|
|
295
|
-
clearCurrentPurchaseError()
|
|
295
|
+
clearCurrentPurchaseError();
|
|
296
296
|
}
|
|
297
297
|
}
|
|
298
298
|
},
|
|
@@ -301,131 +301,118 @@ export function useIAP(options?: UseIapOptions): UseIap {
|
|
|
301
301
|
currentPurchaseError?.productId,
|
|
302
302
|
clearCurrentPurchase,
|
|
303
303
|
clearCurrentPurchaseError,
|
|
304
|
-
]
|
|
305
|
-
)
|
|
304
|
+
],
|
|
305
|
+
);
|
|
306
306
|
|
|
307
307
|
const requestPurchaseWithReset = useCallback(
|
|
308
|
-
async (requestObj: {
|
|
309
|
-
clearCurrentPurchase()
|
|
310
|
-
clearCurrentPurchaseError()
|
|
311
|
-
|
|
312
|
-
try {
|
|
313
|
-
return await requestPurchaseInternal(requestObj)
|
|
314
|
-
} catch (error) {
|
|
315
|
-
throw error
|
|
316
|
-
}
|
|
317
|
-
},
|
|
318
|
-
[clearCurrentPurchase, clearCurrentPurchaseError]
|
|
319
|
-
)
|
|
308
|
+
async (requestObj: {request: any; type?: 'inapp' | 'subs'}) => {
|
|
309
|
+
clearCurrentPurchase();
|
|
310
|
+
clearCurrentPurchaseError();
|
|
320
311
|
|
|
321
|
-
const refreshSubscriptionStatus = useCallback(
|
|
322
|
-
async (productId: string) => {
|
|
323
312
|
try {
|
|
324
|
-
|
|
325
|
-
subscriptionsRefState.current.some(
|
|
326
|
-
(sub: SubscriptionProduct) => sub.id === productId
|
|
327
|
-
)
|
|
328
|
-
) {
|
|
329
|
-
await getSubscriptionsInternal([productId])
|
|
330
|
-
await getAvailablePurchasesInternal()
|
|
331
|
-
}
|
|
313
|
+
return await requestPurchaseInternal(requestObj);
|
|
332
314
|
} catch (error) {
|
|
333
|
-
|
|
315
|
+
throw error;
|
|
334
316
|
}
|
|
335
317
|
},
|
|
336
|
-
[
|
|
337
|
-
)
|
|
318
|
+
[clearCurrentPurchase, clearCurrentPurchaseError],
|
|
319
|
+
);
|
|
338
320
|
|
|
339
321
|
const restorePurchases = useCallback(async (): Promise<void> => {
|
|
340
322
|
try {
|
|
341
323
|
if (Platform.OS === 'ios') {
|
|
342
324
|
await syncIOS().catch((error) => {
|
|
343
325
|
if (optionsRef.current?.onSyncError) {
|
|
344
|
-
optionsRef.current.onSyncError(error)
|
|
326
|
+
optionsRef.current.onSyncError(error);
|
|
345
327
|
} else {
|
|
346
|
-
console.warn('Error restoring purchases:', error)
|
|
328
|
+
console.warn('Error restoring purchases:', error);
|
|
347
329
|
}
|
|
348
|
-
})
|
|
330
|
+
});
|
|
349
331
|
}
|
|
350
|
-
await getAvailablePurchasesInternal()
|
|
332
|
+
await getAvailablePurchasesInternal();
|
|
351
333
|
} catch (error) {
|
|
352
|
-
console.warn('Failed to restore purchases:', error)
|
|
334
|
+
console.warn('Failed to restore purchases:', error);
|
|
353
335
|
}
|
|
354
|
-
}, [getAvailablePurchasesInternal])
|
|
336
|
+
}, [getAvailablePurchasesInternal]);
|
|
355
337
|
|
|
356
338
|
const validateReceipt = useCallback(
|
|
357
339
|
async (
|
|
358
340
|
sku: string,
|
|
359
341
|
androidOptions?: {
|
|
360
|
-
packageName: string
|
|
361
|
-
productToken: string
|
|
362
|
-
accessToken: string
|
|
363
|
-
isSub?: boolean
|
|
364
|
-
}
|
|
342
|
+
packageName: string;
|
|
343
|
+
productToken: string;
|
|
344
|
+
accessToken: string;
|
|
345
|
+
isSub?: boolean;
|
|
346
|
+
},
|
|
365
347
|
) => {
|
|
366
|
-
return validateReceiptInternal(sku, androidOptions)
|
|
348
|
+
return validateReceiptInternal(sku, androidOptions);
|
|
367
349
|
},
|
|
368
|
-
[]
|
|
369
|
-
)
|
|
350
|
+
[],
|
|
351
|
+
);
|
|
370
352
|
|
|
371
353
|
const initIapWithSubscriptions = useCallback(async (): Promise<void> => {
|
|
372
|
-
const result = await initConnection()
|
|
373
|
-
setConnected(result)
|
|
354
|
+
const result = await initConnection();
|
|
355
|
+
setConnected(result);
|
|
374
356
|
|
|
375
357
|
if (result) {
|
|
376
358
|
subscriptionsRef.current.purchaseUpdate = purchaseUpdatedListener(
|
|
377
359
|
async (purchase: Purchase) => {
|
|
378
|
-
setCurrentPurchaseError(undefined)
|
|
379
|
-
setCurrentPurchase(purchase)
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
360
|
+
setCurrentPurchaseError(undefined);
|
|
361
|
+
setCurrentPurchase(purchase);
|
|
362
|
+
|
|
363
|
+
// Always refresh subscription state after a purchase event
|
|
364
|
+
try {
|
|
365
|
+
await getActiveSubscriptionsInternal();
|
|
366
|
+
await getAvailablePurchasesInternal();
|
|
367
|
+
} catch (e) {
|
|
368
|
+
// Non-fatal: UI will still update from event data
|
|
369
|
+
console.warn('[useIAP] post-purchase refresh failed:', e);
|
|
383
370
|
}
|
|
384
371
|
|
|
385
372
|
if (optionsRef.current?.onPurchaseSuccess) {
|
|
386
|
-
optionsRef.current.onPurchaseSuccess(purchase)
|
|
373
|
+
optionsRef.current.onPurchaseSuccess(purchase);
|
|
387
374
|
}
|
|
388
|
-
}
|
|
389
|
-
)
|
|
375
|
+
},
|
|
376
|
+
);
|
|
390
377
|
|
|
391
378
|
subscriptionsRef.current.purchaseError = purchaseErrorListener(
|
|
392
379
|
(error: PurchaseError) => {
|
|
393
|
-
setCurrentPurchase(undefined)
|
|
394
|
-
setCurrentPurchaseError(error)
|
|
380
|
+
setCurrentPurchase(undefined);
|
|
381
|
+
setCurrentPurchaseError(error);
|
|
395
382
|
|
|
396
383
|
if (optionsRef.current?.onPurchaseError) {
|
|
397
|
-
optionsRef.current.onPurchaseError(error)
|
|
384
|
+
optionsRef.current.onPurchaseError(error);
|
|
398
385
|
}
|
|
399
|
-
}
|
|
400
|
-
)
|
|
386
|
+
},
|
|
387
|
+
);
|
|
401
388
|
|
|
402
389
|
if (Platform.OS === 'ios') {
|
|
403
390
|
// iOS promoted products listener
|
|
404
391
|
subscriptionsRef.current.promotedProductsIOS =
|
|
405
392
|
promotedProductListenerIOS((product: Product) => {
|
|
406
|
-
setPromotedProductIOS(product)
|
|
393
|
+
setPromotedProductIOS(product);
|
|
407
394
|
|
|
408
395
|
if (optionsRef.current?.onPromotedProductIOS) {
|
|
409
|
-
optionsRef.current.onPromotedProductIOS(product)
|
|
396
|
+
optionsRef.current.onPromotedProductIOS(product);
|
|
410
397
|
}
|
|
411
|
-
})
|
|
398
|
+
});
|
|
412
399
|
}
|
|
413
400
|
}
|
|
414
|
-
}, [
|
|
401
|
+
}, [getActiveSubscriptionsInternal, getAvailablePurchasesInternal]);
|
|
415
402
|
|
|
416
403
|
useEffect(() => {
|
|
417
|
-
initIapWithSubscriptions()
|
|
418
|
-
const currentSubscriptions = subscriptionsRef.current
|
|
404
|
+
initIapWithSubscriptions();
|
|
405
|
+
const currentSubscriptions = subscriptionsRef.current;
|
|
419
406
|
|
|
420
407
|
return () => {
|
|
421
|
-
currentSubscriptions.purchaseUpdate?.remove()
|
|
422
|
-
currentSubscriptions.purchaseError?.remove()
|
|
423
|
-
currentSubscriptions.promotedProductsIOS?.remove()
|
|
424
|
-
currentSubscriptions.promotedProductIOS?.remove()
|
|
425
|
-
endConnection()
|
|
426
|
-
setConnected(false)
|
|
427
|
-
}
|
|
428
|
-
}, [initIapWithSubscriptions])
|
|
408
|
+
currentSubscriptions.purchaseUpdate?.remove();
|
|
409
|
+
currentSubscriptions.purchaseError?.remove();
|
|
410
|
+
currentSubscriptions.promotedProductsIOS?.remove();
|
|
411
|
+
currentSubscriptions.promotedProductIOS?.remove();
|
|
412
|
+
endConnection();
|
|
413
|
+
setConnected(false);
|
|
414
|
+
};
|
|
415
|
+
}, [initIapWithSubscriptions]);
|
|
429
416
|
|
|
430
417
|
return {
|
|
431
418
|
connected,
|
|
@@ -452,5 +439,5 @@ export function useIAP(options?: UseIapOptions): UseIap {
|
|
|
452
439
|
buyPromotedProductIOS,
|
|
453
440
|
getActiveSubscriptions: getActiveSubscriptionsInternal,
|
|
454
441
|
hasActiveSubscriptions: hasActiveSubscriptionsInternal,
|
|
455
|
-
}
|
|
442
|
+
};
|
|
456
443
|
}
|