Last active
December 2, 2020 06:51
-
-
Save andreferi3/63d3fd89c2578487dab7426bb166606c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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