@chargebee/react-native-chargebee
Version:
Package for Chargebee In-App Purchases
152 lines (141 loc) • 6.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _Purchases = require("./Purchases");
const LINKING_ERROR = `The package '@chargebee/react-native-chargebee' doesn't seem to be linked. Make sure: \n\n` + _reactNative.Platform.select({
ios: "- You have run 'pod install'\n",
default: ''
}) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n';
// @ts-expect-error
const isTurboModuleEnabled = global.__turboModuleProxy != null;
const ChargebeeReactNativeModule = isTurboModuleEnabled ? require('./NativeChargebeeReactNative').default : _reactNative.NativeModules.ChargebeeReactNative;
const ChargebeeReactNative = ChargebeeReactNativeModule ? ChargebeeReactNativeModule : new Proxy({}, {
get() {
throw new Error(LINKING_ERROR);
}
});
class Chargebee {
/**
* Sets up Chargebee SDK with site, API key and SDK IDs for Android and iOS.
*
* @param {String} site Chargebee site.
* Example: If the Chargebee domain url is https://mobile-test.chargebee.com, then the site value is 'mobile-test'
* @param {String} publishableApiKey Publishable API key generated for your Chargebee Site. Refer: https://www.chargebee.com/docs/2.0/api_keys.html#types-of-api-keys_publishable-key
* @param {String} androidSdkKey Android SDK key. Refer: https://www.chargebee.com/docs/1.0/mobile-playstore-notifications.html#app-id
* @param {String} iOsSdkKey iOS SDK key. Refer: https://www.chargebee.com/docs/1.0/mobile-app-store-product-iap.html#connection-keys_app-id
*/
static async configure(_ref) {
let {
site,
publishableApiKey,
androidSdkKey,
iOsSdkKey
} = _ref;
const sdkKey = (0, _Purchases.sdkKeyForPlatform)(androidSdkKey, iOsSdkKey);
return ChargebeeReactNative.configure(site, publishableApiKey, sdkKey);
}
/**
* Retrieves available product identifiers.
*
* @param {Object} productIdentitifiersRequest. Product Identifiers Request object.
* Example: {limit : '100', offset : '1'}
* @returns {Promise<Array<string>>} Array of product identifiers
*/
static async retrieveProductIdentifiers(productIdentitifiersRequest) {
return ChargebeeReactNative.retrieveProductIdentifiers(productIdentitifiersRequest);
}
/**
* Retrieves products for give product identifiers.
*
* @param {Array} productIds Array of product identifiers
* @returns {Promise<Array<Product>>} Array of products
*/
static async retrieveProducts(productIds) {
return ChargebeeReactNative.retrieveProducts(productIds);
}
// TODO: Refactor to pass Product object
/**
* Purchase product for the customer.
*
* @param {string} productId Product identifier
* @param {Object} customer Optional. Customer object.
* If the `id` is not passed in the customer's details, then the value of customerId will be the same as the SubscriptionId created in Chargebee.
* @returns {Promise<Purchase>} Purchase result
*/
static async purchaseProduct(productId, customer) {
return ChargebeeReactNative.purchaseProduct(productId, customer);
}
/**
* Purchase one time product for the customer.
*
* @param {string} productId Product identifier
* @param {Object} productType One Time Product Type.
* @param {Object} customer Optional. Customer object.
* If the `id` is not passed in the customer's details, then the value of customerId will be the same as the SubscriptionId created in Chargebee.
* @returns {Promise<OneTimePurchase>} Purchase result
*/
static async purchaseNonSubscriptionProduct(productId, productType, customer) {
return ChargebeeReactNative.purchaseNonSubscriptionProduct(productId, productType, customer);
}
/**
* Retrieves the subscriptions by customer_id or subscription_id.
*
* @param {Object} subscriptionRequest. Subscription Request object.
* Example: {customer_id : '<customer_id>', subscription_id : '<subscription_id>', status: 'active'}
* @returns {Promise<Array<Subscription>>} Array of subscriptions
*/
static async retrieveSubscriptions(subscriptionRequest) {
return ChargebeeReactNative.retrieveSubscriptions(subscriptionRequest);
}
/**
* Restores the subscriptions for the user logged in the device.
*
* @param {Boolean} includeInactivePurchases When set to true, the inactive purchases are also synced to Chargebee.
* @param {Object} customer Optional. Customer object.
* Please use the same customer id which was used during the initial purchase
* @returns {Promise<Array<RestoredSubscription>>} Array of subscriptions
*/
static async restorePurchases(includeInactivePurchases, customer) {
const shouldIncludeInactivePurchases = Boolean(includeInactivePurchases);
return ChargebeeReactNative.restorePurchases(shouldIncludeInactivePurchases, customer);
}
/**
* Validates the receipt of the given Product ID and Customer.
* This method can be used to retry sync with Chargebee, when sync fails after a successful purchase.
*
* @param {string} productId Product identifier
* @param {Object} customer Optional. Customer object.
* If the `id` is not passed in the customer's details, then the value of customerId will be the same as the SubscriptionId created in Chargebee.
* @returns {Promise<Purchase>} Purchase result
*/
static async validateReceipt(productId, customer) {
return ChargebeeReactNative.validateReceipt(productId, customer);
}
/**
* This method will be used to validate the receipt of One Time Purchase with Chargebee,
* when syncing with Chargebee fails after the successful purchase.
*
* @param {string} productId Product identifier.
* @param {Object} productType One Time Product Type.
* @param {Object} customer Optional. Customer object.
* If the `id` is not passed in the customer's details, then the value of customerId will be the same as the SubscriptionId created in Chargebee.
* @returns {Promise<OneTimePurchase>} Purchase result
*/
static async validateReceiptForNonSubscriptions(productId, productType, customer) {
return ChargebeeReactNative.validateReceiptForNonSubscriptions(productId, productType, customer);
}
/**
* Retrieves the entitlements for a given subscription id
*
* @param {string} subscriptionId Subscription identifier
* @returns {Promise<Array<Entitlement>>} Array of Entitlments
*/
static async retrieveEntitlements(entitlementsRequest) {
return ChargebeeReactNative.retrieveEntitlements(entitlementsRequest);
}
}
exports.default = Chargebee;
//# sourceMappingURL=Chargebee.js.map