revenuecat/purchases-capacitor チュートリアル
このチュートリアルでは、@revenuecat/purchases-capacitor
パッケージを使用して、Ionic Capacitor アプリにアプリ内購入とサブスクリプションを実装するプロセスをガイドします。
始める前に、以下を確認してください。
1 ターミナルまたはコマンドプロンプトを開き、プロジェクトディレクトリに移動します。
2 次のコマンドを実行してパッケージをインストールします:
npm install @revenuecat/purchases-capacitor
3 インストール後、Capacitor プロジェクトを同期します:
npx cap sync
1 アプリのメイン TypeScript ファイル(例:appcomponentts
)に Purchases モジュールをインポートします:
import { Purchases } from '@revenuecat/purchases-capacitor';
2 RevenueCat API キーを使用して SDK を設定します:
async function initializePurchases() {
await Purchases.configure({
apiKey: 'YOUR_REVENUECAT_API_KEY',
});
}
アプリが起動したときにこの関数を呼び出します。例えば、メインコンポーネントの ngOnInit()
メソッド内です。
利用可能な製品のリストを取得するには:
async function getProducts() {
try {
const offerings = await Purchases.getOfferings();
if (offerings.current !== null) {
const products = offerings.current.availablePackages;
console.log('Available products:', products);
}
} catch (error) {
console.error('Error fetching products:', error);
}
}
購入を開始するには:
async function purchasePackage(package: PurchasesPackage) {
try {
const { customerInfo, productIdentifier } = await Purchases.purchasePackage({ aPackage: package });
console.log('Purchase successful:', productIdentifier);
console.log('Customer Info:', customerInfo);
} catch (error) {
console.error('Error making purchase:', error);
}
}
ユーザーの現在のサブスクリプションの状態を確認するには:
async function checkSubscriptionStatus() {
try {
const { customerInfo } = await Purchases.getCustomerInfo();
const activeSubscriptions = customerInfo.activeSubscriptions;
console.log('Active subscriptions:', activeSubscriptions);
} catch (error) {
console.error('Error checking subscription status:', error);
}
}
ユーザーの以前の購入を復元するには:
async function restorePurchases() {
try {
const { customerInfo } = await Purchases.restorePurchases();
console.log('Purchases restored:', customerInfo);
} catch (error) {
console.error('Error restoring purchases:', error);
}
}
独自のユーザー ID システムがある場合は、ユーザーを RevenueCat に識別させることができます:
async function identifyUser(userId: string) {
try {
const { customerInfo } = await Purchases.logIn({ appUserID: userId });
console.log('User identified:', customerInfo);
} catch (error) {
console.error('Error identifying user:', error);
}
}
ユーザーがイントロダクテリー価格の適格性があるか確認するには:
async function checkIntroEligibility(productIdentifiers: string[]) {
try {
const eligibility = await Purchases.checkTrialOrIntroductoryPriceEligibility({ productIdentifiers });
console.log('Introductory price eligibility:', eligibility);
} catch (error) {
console.error('Error checking eligibility:', error);
}
}
ユーザーにカスタム属性を設定できます:
async function setUserAttributes() {
try {
await Purchases.setAttributes({
'user_level': '5',
'user_type': 'premium'
});
console.log('Attributes set successfully');
} catch (error) {
console.error('Error setting attributes:', error);
}
}
このチュートリアルでは、@revenuecat/purchases-capacitor
パッケージを使用したアプリ内購入とサブスクリプションの実装の基本を説明しました。エラーを適切に処理し、実装を十分にテストすることを忘れないでください。
詳細な API ドキュメントや高度な使用法については、RevenueCat のドキュメント(https://docsrevenuecatcom/)を参照してください。
RevenueCat ダッシュボードで製品を設定し、アプリストア製品にリンクすることを忘れないでください。また、リリース前にサンドボックス環境で実装をテストすることを確認してください。