react-native-iaptic
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Iaptic React Native SDK

npm version License: MIT

A robust in-app purchase library for React Native that simplifies receipt validation and subscription management through the Iaptic service.

✨ Features

Cross-Platform Support

  • 🛒 Unified Purchasing API - Single interface for iOS and Android
  • 🔄 Subscription Management - Easy status tracking and renewal handling

Security & Reliability

  • 🔒 Secure Receipt Validation - Server-side validation via Iaptic
  • 🛡 Error Handling - Comprehensive error codes and localization

Product Management

  • 📦 Product Catalog - Structured product definitions with pricing phases
  • 📊 Entitlement Tracking - Real-time purchase state management

📦 Installation

npm install react-native-iaptic
# or
yarn add react-native-iaptic

🚀 Quick Start

Here's a complete example to get you started:

import { IapticRN } from 'react-native-iaptic';

// 1. Initialize with your configuration
const iaptic = new IapticRN({
  appName: 'app.example.com',
  publicKey: 'YOUR_PUBLIC_KEY',
  iosBundleId: 'com.yourcompany.app',
});

// 2. Define your products
iaptic.setProductDefinitions([
  {
    id: 'premium_monthly',
    type: 'paid subscription',
    entitlements: ['premium']
  },
  {
    id: 'coins_100',
    type: 'consumable',
    tokenType: 'coins',
    tokenValue: 100
  }
]);

// 3. Initialize connection and load products/purchases
await iaptic.initialize();

// 4. Handle purchases
const offer = iaptic.products.get('premium_monthly')?.offers[0];
if (offer) {
  await iaptic.order(offer);
}

// 5. Check access
if (iaptic.checkEntitlement('premium')) {
  // Unlock premium features
}

💡 Core Concepts

Product Definitions

Products can be subscriptions, consumables, or non-consumables. Each product can grant one or more entitlements:

iaptic.setProductDefinitions([
  // Subscription that unlocks premium features
  { 
    id: 'premium_monthly',
    type: 'paid subscription',
    entitlements: ['premium']
  },
  // Non-consumable that unlocks a specific feature
  {
    id: 'dark_theme',
    type: 'non consumable',
    entitlements: ['cool_feature']
  },
  // Consumable tokens/currency
  { 
    id: 'coins_100',
    type: 'consumable',
    tokenType: 'coins',
    tokenValue: 100
  }
]);

Purchase Flow

Handle purchases with proper error management:

try {
  await iaptic.order(productOffer);
} catch (error) {
  showError(error);
}

Restore Purchases

Allow users to restore their previous purchases:

try {
  iaptic.restorePurchases((processed, total) => {
    console.log(`Processed ${processed} of ${total} purchases`);
  });
}
catch (error) {
  showError(error);
}

Event Handling

Listen for purchase and subscription updates:

// Listen for subscription updates
iaptic.addEventListener('subscription.updated', (reason, purchase) => {
  console.log(`Subscription ${purchase.id} ${reason}`);
});

// Listen for pending purchase updates
iaptic.addEventListener('pendingPurchase.updated', (pendingPurchase) => {
  console.log(`Purchase ${pendingPurchase.productId} is now ${pendingPurchase.status}`);
});

// Listen for purchase updates
iaptic.addEventListener('purchase.updated', (purchase) => {
  console.log(`Purchase ${purchase.id} ${purchase.status}`);
});

Feature Access Control

Check if users have access to specific features:

// Check premium access
if (iaptic.checkEntitlement('premium')) {
  showPremiumContent();
} else {
  showUpgradePrompt();
}

// List all active entitlements
const unlockedFeatures = iaptic.listEntitlements();
// ['basic', 'premium', 'cool_feature']

Error Handling

function showError(error: Error | IapticError) {
  if (error instanceof IapticError) {
    trackAnalyticsEvent(error.code);
    if (error.severity === IapticErrorSeverity.INFO) {
      console.log('Info:', error.localizedMessage);
      return;
    }
    Alert.alert(error.localizedTitle, error.localizedMessage);
  } else {
    Alert.alert('Unknown error', error.message);
  }
}

📚 API Reference

For complete API documentation, visit our API Reference.

🤝 Need Help?

📄 License

MIT © Iaptic

Package Sidebar

Install

npm i react-native-iaptic

Weekly Downloads

4

Version

1.0.0

License

MIT

Unpacked Size

215 kB

Total Files

51

Last publish

Collaborators

  • jchoelt