为你的 Capacitor 应用添加应用内购买
使用 Adapty 的 Capacitor SDK 在 iOS 和 Android 上集成应用内购买。处理收据、追踪订阅状态,无需自建后端。一次集成,尽享全部功能。
import { adapty } from '@adapty/capacitor';
try {
await adapty.activate({
apiKey: 'YOUR_PUBLIC_SDK_KEY',
params: {
logLevel: 'verbose',
__ignoreActivationOnFastRefresh: true,
}
});
console.log('Adapty activated successfully!');
} catch (error) {
console.error('Failed to activate Adapty SDK:', error);
}
你无需为 Capacitor 应用内购买构建后端
Adapty SDK 负责所有通常在服务器端完成的工作:收据验证、订阅状态、续订、试用和退款。你只需连接一次,即可获得适用于 iOS 和 Android 的Capacitor 应用内购买配置。
安排演示为什么选择 Adapty SDK?
跨平台追踪订阅状态
你将始终知道用户在 iOS 和 Android 上的订阅状态。
在后端验证收据
无需自建验证系统,Adapty 会自动处理。
处理各种订阅状态
免费试用、升级、促销优惠、家庭共享、续订等全部支持。
企业级可扩展核心
我们频繁更新,保持 SDK 稳定,并运行在 >99.99% SLA 上。
配置平台
安装 Adapty SDK
await adapty.activate( 'PUBLIC_SDK_KEY', { customerUserId: 'YOUR_USER_ID', });
处理购买事件
使用 5 个 SDK 方法处理 Capacitor IAP
import { adapty } from '@adapty/capacitor';
try {
const result = await adapty.makePurchase({ product });
if (result.type === 'success') {
const isSubscribed = result.profile?.accessLevels['YOUR_ACCESS_LEVEL']?.isActive;
if (isSubscribed) {
// Grant access to the paid features
console.log('User is now subscribed!');
}
} else if (result.type === 'user_cancelled') {
console.log('Purchase cancelled by user');
} else if (result.type === 'pending') {
console.log('Purchase is pending');
}
} catch (error) {
console.error('Purchase failed:', error);
}
import { adapty } from '@adapty/capacitor';
try {
const profile = await adapty.restorePurchases();
const isSubscribed = profile.accessLevels['YOUR_ACCESS_LEVEL']?.isActive;
if (isSubscribed) {
// Restore access to paid features
console.log('Access restored successfully!');
} else {
console.log('No active subscriptions found');
}
} catch (error) {
console.error('Failed to restore purchases:', error);
}
import { adapty } from '@adapty/capacitor';
try {
await adapty.identify({ customerUserId: "YOUR_USER_ID" });
// successfully identified
} catch (error) {
// handle the error
}
import { adapty } from '@adapty/capacitor';
const params = {
email: '[email protected]',
phoneNumber: '+18888888888',
firstName: 'John',
lastName: 'Appleseed',
gender: 'other',
birthday: new Date().toISOString(),
};
try {
await adapty.updateProfile(params);
console.log('Profile updated successfully');
} catch (error) {
console.error('Failed to update profile:', error);
}
import { adapty } from '@adapty/capacitor';
try {
await adapty.updateProfile({
codableCustomAttributes: {
key_1: 'value_1',
key_2: 2,
key_3: null, // to remove keys, pass null as their values
},
});
console.log('Custom attributes updated successfully');
} catch (error) {
console.error('Failed to update custom attributes:', error);
}