在移动应用中展示付费墙是向用户提供高级内容或服务访问权限的重要步骤。然而,仅仅展示付费墙只有在您使用付费墙编辑工具来自定义付费墙时,才足以支持购买流程。
如果您不使用付费墙编辑工具,则必须使用名为 .makePurchase() 的独立方法来完成购买并解锁所需内容。该方法是用户与付费墙交互并完成所需交易的入口。
如果您的付费墙对用户尝试购买的产品设有有效的促销活动,Adapty 将在购买时自动应用该优惠。
请注意,新用户优惠只有在您使用付费墙编辑工具设置付费墙时才会自动应用。
在其他情况下,您需要验证用户是否符合 iOS 上新用户优惠的资格。跳过此步骤可能导致您的应用在发布审核时被拒绝,还可能对符合新用户优惠资格的用户收取全价。
请确保您已完成初始配置,不要跳过任何步骤。没有完成配置,我们将无法验证购买。
进行购买
正在使用付费墙编辑工具? 购买将自动处理——您可以跳过此步骤。
需要分步指导? 请查阅快速入门指南,获取包含完整上下文的端到端实现说明。
Adapty.makePurchase(activity, product, null) { result ->
when (result) {
is AdaptyResult.Success -> {
when (val purchaseResult = result.value) {
is AdaptyPurchaseResult.Success -> {
val profile = purchaseResult.profile
if (profile.accessLevels["YOUR_ACCESS_LEVEL"]?.isActive == true) {
// Grant access to the paid features
}
}
is AdaptyPurchaseResult.UserCanceled -> {
// Handle the case where the user canceled the purchase
}
is AdaptyPurchaseResult.Pending -> {
// Handle deferred purchases (e.g., the user will pay offline with cash)
}
}
}
is AdaptyResult.Error -> {
val error = result.error
// Handle the error
}
}
}
Adapty.makePurchase(activity, product, null, result -> {
if (result instanceof AdaptyResult.Success) {
AdaptyPurchaseResult purchaseResult = ((AdaptyResult.Success<AdaptyPurchaseResult>) result).getValue();
if (purchaseResult instanceof AdaptyPurchaseResult.Success) {
AdaptyProfile profile = ((AdaptyPurchaseResult.Success) purchaseResult).getProfile();
AdaptyProfile.AccessLevel premium = profile.getAccessLevels().get("YOUR_ACCESS_LEVEL");
if (premium != null && premium.isActive()) {
// Grant access to the paid features
}
} else if (purchaseResult instanceof AdaptyPurchaseResult.UserCanceled) {
// Handle the case where the user canceled the purchase
} else if (purchaseResult instanceof AdaptyPurchaseResult.Pending) {
// Handle deferred purchases (e.g., the user will pay offline with cash)
}
} else if (result instanceof AdaptyResult.Error) {
AdaptyError error = ((AdaptyResult.Error) result).getError();
// Handle the error
}
});
请求参数:
响应参数:
| 参数 | 描述 |
|---|
| Profile | 如果请求成功,响应将包含此对象。AdaptyProfile 对象提供了关于用户访问等级、订阅及应用内非订阅购买的全面信息。 请检查访问等级状态,以确认用户是否拥有访问应用所需的权限。 |
注意: 如果您仍在使用低于 v2.0 的 Apple StoreKit 版本以及低于 v.2.9.0 的 Adapty SDK 版本,则需要改为提供 Apple App Store 共享密钥。该方法目前已被 Apple 弃用。
购买时更改订阅
当用户选择新订阅而不是续订当前订阅时,具体行为取决于应用商店。对于 Google Play,订阅不会自动更新。您需要按照以下说明在移动应用代码中管理切换操作。
要在 Android 中将订阅替换为另一个订阅,请使用附加参数调用 .makePurchase() 方法:
Adapty.makePurchase(
activity,
product,
AdaptyPurchaseParameters.Builder()
.withSubscriptionUpdateParams(subscriptionUpdateParams)
.build()
) { result ->
when (result) {
is AdaptyResult.Success -> {
when (val purchaseResult = result.value) {
is AdaptyPurchaseResult.Success -> {
val profile = purchaseResult.profile
// successful cross-grade
}
is AdaptyPurchaseResult.UserCanceled -> {
// user canceled the purchase flow
}
is AdaptyPurchaseResult.Pending -> {
// the purchase has not been finished yet, e.g. user will pay offline by cash
}
}
}
is AdaptyResult.Error -> {
val error = result.error
// Handle the error
}
}
}
附加请求参数:
Adapty.makePurchase(
activity,
product,
new AdaptyPurchaseParameters.Builder()
.withSubscriptionUpdateParams(subscriptionUpdateParams)
.build(),
result -> {
if (result instanceof AdaptyResult.Success) {
AdaptyPurchaseResult purchaseResult = ((AdaptyResult.Success<AdaptyPurchaseResult>) result).getValue();
if (purchaseResult instanceof AdaptyPurchaseResult.Success) {
AdaptyProfile profile = ((AdaptyPurchaseResult.Success) purchaseResult).getProfile();
// successful cross-grade
} else if (purchaseResult instanceof AdaptyPurchaseResult.UserCanceled) {
// user canceled the purchase flow
} else if (purchaseResult instanceof AdaptyPurchaseResult.Pending) {
// the purchase has not been finished yet, e.g. user will pay offline by cash
}
} else if (result instanceof AdaptyResult.Error) {
AdaptyError error = ((AdaptyResult.Error) result).getError();
// Handle the error
}
});
附加请求参数:
您可以在 Google 开发者文档中了解更多关于订阅和替换模式的内容:
管理预付费套餐
如果您的应用用户可以购买预付费套餐(例如,购买数月的非续订订阅),您可以为预付费套餐启用待处理交易。
import com.adapty.models.AdaptyConfig
AdaptyConfig.Builder("PUBLIC_SDK_KEY")
.withEnablePendingPrepaidPlans(true)
.build()
import com.adapty.models.AdaptyConfig;
new AdaptyConfig.Builder("PUBLIC_SDK_KEY")
.withEnablePendingPrepaidPlans(true)
.build();