Skip to content

Instantly share code, notes, and snippets.

@andreferi3
Last active December 2, 2020 06:51
Show Gist options
  • Select an option

  • Save andreferi3/63d3fd89c2578487dab7426bb166606c to your computer and use it in GitHub Desktop.

Select an option

Save andreferi3/63d3fd89c2578487dab7426bb166606c to your computer and use it in GitHub Desktop.
import { Platform, Alert } from "react-native";
import {
getAvailablePurchases,
requestSubscription,
getSubscriptions,
} from "react-native-iap";
import { RenderToast } from "../components/CToast";
export const itemSkus: any = Platform.select({
ios: ["com.afterwords.ios.monthly", "com.afterwords.ios.premium"],
android: ["afterwords_subscription_id_1", "afterwords_subscription_id_3"],
});
/**
* @function getItems
* * Getting all products in Google Play Console or App Store
*/
export async function getItems(): Promise<any> {
try {
const products = await getSubscriptions(itemSkus);
return products;
} catch (err) {
RenderToast(`Google Play Error : ${err.message}`);
}
}
/**
* @function getAvaliablePurchase
* * Get all purchases made by the user
*/
export async function getAvaliablePurchase(): Promise<any> {
try {
console.info(
"Get available purchases (non-consumable or unconsumed consumable)",
);
const purchases = await getAvailablePurchases();
console.info("Available purchases : ", purchases);
if (purchases && purchases.length > 0) {
console.warn(`Got ${purchases.length} items.`);
return purchases[0].transactionReceipt;
}
} catch (err) {
console.warn(err.code, err.message);
Alert.alert(err.message);
}
}
/**
* @function buySubscription
* @param sku {string}
* * Create (buy) a subscription to a sku.
*/
export async function buySubscription(
sku: string,
oldSku: string,
): Promise<any> {
try {
requestSubscription(sku, false, oldSku);
} catch (err) {
Alert.alert(err.message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment