@spicysparks/react-native-iap
Version:
React Native In App Purchase Module.
81 lines (77 loc) • 2.79 kB
JavaScript
import { Linking, NativeModules } from 'react-native';
import { checkNativeAndroidAvailable, getAndroidModule, isAndroid } from '../internal';
import { InstallSourceAndroid } from '../types';
const {
RNIapModule
} = NativeModules;
export const AndroidModule = NativeModules.RNIapModule;
export const getInstallSourceAndroid = () => {
return RNIapModule ? InstallSourceAndroid.GOOGLE_PLAY : InstallSourceAndroid.AMAZON;
};
/**
* Deep link to subscriptions screen on Android.
* @param {string} sku The product's SKU (on Android)
* @returns {Promise<void>}
*/
export const deepLinkToSubscriptionsAndroid = async ({
sku
}) => {
checkNativeAndroidAvailable();
return Linking.openURL(`https://play.google.com/store/account/subscriptions?package=${await RNIapModule.getPackageName()}&sku=${sku}`);
};
/**
* Validate receipt for Android. NOTE: This method is here for debugging purposes only. Including
* your access token in the binary you ship to users is potentially dangerous.
* Use server side validation instead for your production builds
* @param {string} packageName package name of your app.
* @param {string} productId product id for your in app product.
* @param {string} productToken token for your purchase.
* @param {string} accessToken accessToken from googleApis.
* @param {boolean} isSub whether this is subscription or inapp. `true` for subscription.
* @returns {Promise<object>}
*/
export const validateReceiptAndroid = async ({
packageName,
productId,
productToken,
accessToken,
isSub
}) => {
const type = isSub ? 'subscriptions' : 'products';
const url = 'https://androidpublisher.googleapis.com/androidpublisher/v3/applications' + `/${packageName}/purchases/${type}/${productId}` + `/tokens/${productToken}?access_token=${accessToken}`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw Object.assign(new Error(response.statusText), {
statusCode: response.status
});
}
return response.json();
};
/**
* Acknowledge a product (on Android.) No-op on iOS.
* @param {string} token The product's token (on Android)
* @returns {Promise<PurchaseResult | void>}
*/
export const acknowledgePurchaseAndroid = ({
token,
developerPayload
}) => {
return getAndroidModule().acknowledgePurchase(token, developerPayload);
};
/**
* Acknowledge a product (on Android.) No-op on iOS.
* @param {Android.FeatureType} feature to be checked
* @returns {Promise<boolean>}
*/
export const isFeatureSupported = feature => {
if (!(isAndroid && RNIapModule)) {
return Promise.reject('This is only available on Android clients');
}
return AndroidModule.isFeatureSupported(feature);
};
//# sourceMappingURL=android.js.map