How to use the react-native-iap.getAvailablePurchases function in react-native-iap

To help you get started, we’ve selected a few react-native-iap examples, based on popular ways it is used in public projects.

github Musicoin / app / components / product / ProductModal.js View on Github external
if (Platform.OS === 'ios') {
        validation = await validateAppleIAPJSON(this.props.auth.email, this.props.auth.accessToken, this.state.receipt.transactionReceipt);
      }

      if (Platform.OS === 'android') {
        //ToDo: check verify Play store purchases
        validation = await validateGoogleIAPJSON(this.props.auth.email, this.props.auth.accessToken, {receipt: this.state.receipt.dataAndroid, signature: this.state.receipt.signatureAndroid});
        // console.log(validation);
        // validation = {tx: "remove this"};
      }
      console.log('validation:');
      console.log(validation);

      // consume products
      const purchases = await RNIap.getAvailablePurchases();
      console.log(purchases);
      purchases.forEach(async purchase => {
        console.log(purchase);
        await RNIap.consumePurchase(purchase.purchaseToken);
      });

      if (validation.tx) {
        this.props.addAlert('success', 'Thank you for your purchase!', 'Your coins will appear in your wallet soon');
      } else {
        this.props.addAlert('error', '', validation.error ? validation.error : 'Something went wrong');
      }
    } catch (err) {
      console.log(err); // standardized err.code and err.message available
      this.props.addAlert('error', '', err.message);
    }
  };
github Skjutsgruppen / skjutsgruppen-reactnative / app / services / support / purchase.js View on Github external
export async function isSubscriptionActive() {
  let currentSubscription;
  if (Platform.OS === 'android') {
    try {
      await RNIap.prepare();
      const subscriptionList = await RNIap.getAvailablePurchases();
      if (subscriptionList.length > 0) {
        currentSubscription = subscriptionList[0].productId;
      }
    } catch (error) {
      currentSubscription = 'error';
      console.warn(error);
    } finally {
      await RNIap.endConnection();
    }
  }
  return currentSubscription;
}
github Skjutsgruppen / skjutsgruppen-reactnative / app / services / support / purchase.js View on Github external
export async function getLatestSubscription(callback) {
  try {
    await RNIap.prepare();
    const subscriptionList = await RNIap.getAvailablePurchases();

    if (subscriptionList.length === 0) {
      callback(null, null);
    } else {
      const latestSubscription = [].concat(subscriptionList).sort((first, second) =>
        second.transactionDate - first.transactionDate)[0];
      callback(null, {
        planId: latestSubscription.productId,
        receipt: latestSubscription.transactionReceipt,
        device: Platform.OS,
      });
    }
  } catch (error) {
    console.warn(error);
    callback(error, null);
  } finally {
github guardian / editions / projects / Mallard / src / authentication / services / iap.ts View on Github external
const tryRestoreActiveIOSSubscriptionReceipt = async (): Promise<
    AuthResult<ReceiptIOS>
> => {
    try {
        if (Platform.OS !== 'ios') return InvalidResult()
        const purchases = await RNIAP.getAvailablePurchases()
        const mostRecentReceipt = getMostRecentTransactionReceipt(purchases)
        if (!mostRecentReceipt) return InvalidResult()
        const decodedReceipt = await fetchDecodeReceipt(mostRecentReceipt)
        if (!decodedReceipt) return InvalidResult()
        const validReceipt = findValidReceipt(decodedReceipt)
        return validReceipt ? ValidResult(validReceipt) : InvalidResult()
    } catch {
        return ErrorResult('Verification error')
    }
}
github guardian / editions / projects / Mallard / src / hooks / use-subscribe.ts View on Github external
RNIAP.initConnection().then(() =>
            RNIAP.getAvailablePurchases()
                .then(findCurrentSubInfoFromPurchases)
                .then(setCurrentSubscriptionInfo),
        )
github Flaque / quirk / src / subscriptions / iosReceipts.ts View on Github external
export async function getAppleExpirationDateFromRecentPurchases(): Promise<
  false | number
> {
  const purchases = (await InAppPurchases.getAvailablePurchases()) || [];
  if (!purchases || purchases.length === 0) {
    return false;
  }

  const receipt = purchases[0].transactionReceipt;
  return getAppleExpirationDateFromReceipt(receipt);
}