react-native-iap 14.1.0 → 14.1.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.
@@ -1,6 +1,6 @@
1
1
  // External dependencies
2
- import { useCallback, useEffect, useState, useRef } from 'react'
3
- import { Platform } from 'react-native'
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 { syncIOS, requestPromotedProductIOS, buyPromotedProductIOS } from '../'
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({ skus, type: 'inapp' })
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({ skus, type: 'subs' })
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,118 +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: { request: any; type?: 'inapp' | 'subs' }) => {
309
- clearCurrentPurchase()
310
- clearCurrentPurchaseError()
308
+ async (requestObj: {request: any; type?: 'inapp' | 'subs'}) => {
309
+ clearCurrentPurchase();
310
+ clearCurrentPurchaseError();
311
311
 
312
312
  try {
313
- return await requestPurchaseInternal(requestObj)
313
+ return await requestPurchaseInternal(requestObj);
314
314
  } catch (error) {
315
- throw error
315
+ throw error;
316
316
  }
317
317
  },
318
- [clearCurrentPurchase, clearCurrentPurchaseError]
319
- )
318
+ [clearCurrentPurchase, clearCurrentPurchaseError],
319
+ );
320
320
 
321
321
  const restorePurchases = useCallback(async (): Promise<void> => {
322
322
  try {
323
323
  if (Platform.OS === 'ios') {
324
324
  await syncIOS().catch((error) => {
325
325
  if (optionsRef.current?.onSyncError) {
326
- optionsRef.current.onSyncError(error)
326
+ optionsRef.current.onSyncError(error);
327
327
  } else {
328
- console.warn('Error restoring purchases:', error)
328
+ console.warn('Error restoring purchases:', error);
329
329
  }
330
- })
330
+ });
331
331
  }
332
- await getAvailablePurchasesInternal()
332
+ await getAvailablePurchasesInternal();
333
333
  } catch (error) {
334
- console.warn('Failed to restore purchases:', error)
334
+ console.warn('Failed to restore purchases:', error);
335
335
  }
336
- }, [getAvailablePurchasesInternal])
336
+ }, [getAvailablePurchasesInternal]);
337
337
 
338
338
  const validateReceipt = useCallback(
339
339
  async (
340
340
  sku: string,
341
341
  androidOptions?: {
342
- packageName: string
343
- productToken: string
344
- accessToken: string
345
- isSub?: boolean
346
- }
342
+ packageName: string;
343
+ productToken: string;
344
+ accessToken: string;
345
+ isSub?: boolean;
346
+ },
347
347
  ) => {
348
- return validateReceiptInternal(sku, androidOptions)
348
+ return validateReceiptInternal(sku, androidOptions);
349
349
  },
350
- []
351
- )
350
+ [],
351
+ );
352
352
 
353
353
  const initIapWithSubscriptions = useCallback(async (): Promise<void> => {
354
- const result = await initConnection()
355
- setConnected(result)
354
+ const result = await initConnection();
355
+ setConnected(result);
356
356
 
357
357
  if (result) {
358
358
  subscriptionsRef.current.purchaseUpdate = purchaseUpdatedListener(
359
359
  async (purchase: Purchase) => {
360
- setCurrentPurchaseError(undefined)
361
- setCurrentPurchase(purchase)
360
+ setCurrentPurchaseError(undefined);
361
+ setCurrentPurchase(purchase);
362
362
 
363
363
  // Always refresh subscription state after a purchase event
364
364
  try {
365
- await getActiveSubscriptionsInternal()
366
- await getAvailablePurchasesInternal()
365
+ await getActiveSubscriptionsInternal();
366
+ await getAvailablePurchasesInternal();
367
367
  } catch (e) {
368
368
  // Non-fatal: UI will still update from event data
369
- console.warn('[useIAP] post-purchase refresh failed:', e)
369
+ console.warn('[useIAP] post-purchase refresh failed:', e);
370
370
  }
371
371
 
372
372
  if (optionsRef.current?.onPurchaseSuccess) {
373
- optionsRef.current.onPurchaseSuccess(purchase)
373
+ optionsRef.current.onPurchaseSuccess(purchase);
374
374
  }
375
- }
376
- )
375
+ },
376
+ );
377
377
 
378
378
  subscriptionsRef.current.purchaseError = purchaseErrorListener(
379
379
  (error: PurchaseError) => {
380
- setCurrentPurchase(undefined)
381
- setCurrentPurchaseError(error)
380
+ setCurrentPurchase(undefined);
381
+ setCurrentPurchaseError(error);
382
382
 
383
383
  if (optionsRef.current?.onPurchaseError) {
384
- optionsRef.current.onPurchaseError(error)
384
+ optionsRef.current.onPurchaseError(error);
385
385
  }
386
- }
387
- )
386
+ },
387
+ );
388
388
 
389
389
  if (Platform.OS === 'ios') {
390
390
  // iOS promoted products listener
391
391
  subscriptionsRef.current.promotedProductsIOS =
392
392
  promotedProductListenerIOS((product: Product) => {
393
- setPromotedProductIOS(product)
393
+ setPromotedProductIOS(product);
394
394
 
395
395
  if (optionsRef.current?.onPromotedProductIOS) {
396
- optionsRef.current.onPromotedProductIOS(product)
396
+ optionsRef.current.onPromotedProductIOS(product);
397
397
  }
398
- })
398
+ });
399
399
  }
400
400
  }
401
- }, [getActiveSubscriptionsInternal, getAvailablePurchasesInternal])
401
+ }, [getActiveSubscriptionsInternal, getAvailablePurchasesInternal]);
402
402
 
403
403
  useEffect(() => {
404
- initIapWithSubscriptions()
405
- const currentSubscriptions = subscriptionsRef.current
404
+ initIapWithSubscriptions();
405
+ const currentSubscriptions = subscriptionsRef.current;
406
406
 
407
407
  return () => {
408
- currentSubscriptions.purchaseUpdate?.remove()
409
- currentSubscriptions.purchaseError?.remove()
410
- currentSubscriptions.promotedProductsIOS?.remove()
411
- currentSubscriptions.promotedProductIOS?.remove()
412
- endConnection()
413
- setConnected(false)
414
- }
415
- }, [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]);
416
416
 
417
417
  return {
418
418
  connected,
@@ -439,5 +439,5 @@ export function useIAP(options?: UseIapOptions): UseIap {
439
439
  buyPromotedProductIOS,
440
440
  getActiveSubscriptions: getActiveSubscriptionsInternal,
441
441
  hasActiveSubscriptions: hasActiveSubscriptionsInternal,
442
- }
442
+ };
443
443
  }