Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
}
};
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;
}
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 {
const tryRestoreActiveIOSSubscriptionReceipt = async (): Promise<
AuthResult
> => {
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')
}
}
RNIAP.initConnection().then(() =>
RNIAP.getAvailablePurchases()
.then(findCurrentSubInfoFromPurchases)
.then(setCurrentSubscriptionInfo),
)
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);
}