在 Android SDK 中进行移动应用内购
在移动应用中展示付费墙是向用户提供高级内容或服务访问权限的重要步骤。不过,只有在 Adapty 负责渲染界面时——即使用流程或旧版付费墙编辑工具构建的付费墙——展示付费墙才能独立完成购买。
如果你在自己的代码中渲染界面,则需要调用一个名为 .makePurchase() 的独立方法来完成购买并解锁所需内容。该方法是用户与付费墙交互并完成目标交易的核心入口。
如果你的付费墙针对用户正在购买的产品设置了有效的促销活动,Adapty 会在购买时自动应用该优惠。
请注意,只有在 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
}
}
}请求参数:
| 参数 | 必要性 | 描述 |
|---|---|---|
| Product | 必填 | 从付费墙中获取的 AdaptyPaywallProduct 对象。 |
响应参数:
| 参数 | 描述 |
|---|---|
| Profile | 如果请求成功,响应将包含此对象。AdaptyProfile 对象提供了关于用户访问等级、订阅及应用内非订阅购买的全面信息。 请检查访问等级状态,以确认用户是否拥有访问应用所需的权限。 |
如果用户选择了延迟付款方式(例如在门店付现金),makePurchase 将返回 AdaptyPurchaseResult.Pending,用户此时尚未获得访问权限。从 Adapty SDK 4.3.0 起,当 Google Play 批准付款后,SDK 会自动向 Adapty 验证该购买。如果此时应用已关闭,SDK 将在用户下次打开应用时进行验证。要在验证通过时立即授予访问权限,请监听用户画像更新。
一旦 Google Play 通过实时开发者通知报告购买已完成,Adapty 服务器便会确认该购买;如果是消耗型商品,则会消耗它。因此,即使用户在三天内没有打开应用,Google Play 也不会自动退款。SDK 版本和下次应用启动只影响购买何时与用户画像关联。
购买时更改订阅
当用户选择新订阅而不是续订当前订阅时,具体行为取决于应用商店。对于 Google Play,订阅不会自动更新。您需要按照以下说明在移动应用代码中管理切换操作。
要在 Android 中将订阅替换为另一个订阅,请使用附加参数调用 .makePurchase() 方法:
仅在用户从同一 Google 账号下已激活的订阅进行切换时,才传入 subscriptionUpdateParams。若是全新购买,请在调用 makePurchase 时不要传该参数——传入此参数会导致购买失败,并抛出 CURRENT_SUBSCRIPTION_TO_UPDATE_NOT_FOUND_IN_HISTORY 错误,该错误信息具有误导性,会指向非活跃订阅或其他 Google 账号。
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
}
}
}附加请求参数:
| 参数 | 必要性 | 描述 |
|---|---|---|
| subscriptionUpdateParams | 必填 | 一个 AdaptySubscriptionUpdateParameters 对象。 |
您可以在 Google 开发者文档中了解更多关于订阅和替换模式的内容:
- 关于替换模式
- Google 对替换模式的建议
- 替换模式
CHARGE_PRORATED_PRICE。注意:此方法仅适用于订阅升级,不支持降级。 - 替换模式
DEFERRED。注意:实际的订阅变更将在当前订阅计费周期结束时才会发生。
管理预付费套餐
如果您的应用用户可以购买预付费套餐(例如,购买数月的非续订订阅),您可以为预付费套餐启用待处理交易。
AdaptyConfig.Builder("PUBLIC_SDK_KEY")
.withEnablePendingPrepaidPlans(true)
.build()