skip to Main Content

I install and linked react native React Native IAP and also upload app in play store after that it create subscription product id But When I am request for get subscription I am getting this error

error

subscription product id


const items = Platform.select({
  ios: [
    'dev.products.gas',
    'dev.products.premium',
    'dev.products.gold_monthly',
    'dev.products.gold_yearly',
  ],
  android: ['dev.products.gas',
  'dev.products.premium',
  'dev.products.gold_monthly',
  'dev.products.gold_yearly',],
});


import React, { useEffect } from 'react';
import { View, Text, Button, Platform } from 'react-native';
import { requestPurchase, useIAP } from 'react-native-iap';

const ParchesCode = () => {
  const {
    connected,
    products,
    promotedProductsIOS,
    subscriptions,
    purchaseHistories,
    availablePurchases,
    currentPurchase,
    currentPurchaseError,
    initConnectionError,
    finishTransaction,
    getProducts,
    getSubscriptions,
    getAvailablePurchases,
    getPurchaseHistories,
  } = useIAP();

  const handlePurchase = async (sku) => {
    await requestPurchase({ sku });
  };

  useEffect(() => {
    // ... listen to currentPurchaseError, to check if any error happened
  }, [currentPurchaseError]);

  useEffect(() => {
    // ... listen to currentPurchase, to check if the purchase went through
  }, [currentPurchase]);

  return (
    <View style={{ flex: 1, }}>
      <Button
        title="Get the products"
        onPress={() => getSubscriptions(items)}
      />

      {subscriptions.map((product) => (
        <View key={product.productId}>
          <Text>{product.productId}</Text>

          <Button
            title="Buy"
            onPress={() => handlePurchase(product.productId)}
          />


        </View>
      ))}
      <Text>{JSON.stringify(connected)}</Text>
      <Text>{JSON.stringify(connected)}</Text>
    </View>
  );
};

export default ParchesCode;

I am not getting subscriptions when I am calling getSubscriptions() function. I need result after calling this but I am getting error like that -> Possible Unhandled Promise Rejection (id: 0):
skus is required

2

Answers


  1. Here is with working example. there issue is due to change in code but the documentation is not updated.

    Here is the product list. the code only for iOS.

    import {requestPurchase, useIAP, withIAPContext} from 'react-native-iap';
        const myProducts = [
          'product.example.1',
          'product.example.2',
        ];
    const InAppPurchaseScreen = ({navigation}) => {
         useEffect(() => {
               getProductsAndPurchases();
         }, []);
        const getProductsAndPurchases = async () => {
           try {
             await getProducts({skus: myProducts});
           } catch (error) {
             //console.log("Products error: ", error)
           }
        };
    
    }
    export default withIAPContext(InAppPurchaseScreen);
    
    Login or Signup to reply.
  2. For Android, here is a working example of react-native-iap with latest version.

    import {  initConnection, getSubscriptions, requestSubscription } from 'react-native-iap';
    
    const products = ['example_799_1m', 'example_1100_3m'];
    
    useEffect(() => {
     initConnection()
      .catch(() => {
        console.log('error connecting to store...');
      })
      .then(() => {
        getSubscriptions({skus: products})
          .catch(() => {
            console.log('error finding products');
          })
          .then(res => {
            console.log('products with offerToken', res);
            // store/handle response
          });
      });
    }, []);
    
    const handleRequestSubscription = () => {
       requestSubscription({
          sku: productId,
          ...(offerToken && {
             subscriptionOffers: [
               {
                 sku: productId,          // as a string
                 offerToken: offerToken,  // as a string
               },
             ],
          }),
        })
        .catch(err => {
           console.log('error buying product', err);
         })
        .then(async res => {
          console.log('request subscription ',JSON.stringify(res));
          // handle/store response
        });
    }
    
    • "react-native-iap": "^12.4.12",
    • React Native: 0.70.6
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search