Alper Taş
Alper Taş

Reputation: 45

react-native-iap 'skus' is required

I prepared a setup that would start the purchase when the button was pressed on a course purchase page, but I ran into an obstacle.

I reviewed various issues and tried possible solutions, but I could not break the same error chain.

 try {
  setLoading(true);
  const productId = Platform.OS === 'ios' 
    ? data.apple_store_product?.product_id 
    : data.android_store_product?.product_id;

  console.log('Product ID:', productId);

  if (!productId) {
    throw new Error('ID not found.');
  }

  // IAP start
  await RNIap.initConnection();

  // get products (just one product id that we need)
  const products = await RNIap.getProducts([String(productId)]);
  
  console.log('founded products:', products);

  if (!products || products.length === 0) {
    throw new Error('no products exist');
  }

  // buy
  if (Platform.OS === 'ios') {
    await RNIap.requestPurchase({skus: [sku]});
  } else {
    const sku = String(productId);
    await RNIap.getProducts([sku]); // before get product
    await RNIap.requestPurchase(sku, false); // after start purchase      }
} catch (error) {
  console.error('error:', error);
  if (error.message === 'not found on market') {
    Alert.alert('Error', 'not found on market');
  } else {
    Alert.alert('Error', 'The purchase could not be initiated.');
  }
} finally {
  setLoading(false);
  try {
    await RNIap.endConnection();
  } catch (err) {
    console.log('error when trying to close IAP:', err);
  }
}

log:

 (NOBRIDGE) LOG  Product ID: course_tier_24
 (NOBRIDGE) INFO  Using Storekit 2
 (NOBRIDGE) ERROR  error: "skus" is required
 (NOBRIDGE) INFO  Using Storekit 2

I request your support.

Upvotes: 0

Views: 94

Answers (0)

Related Questions