---
title: "在 Android SDK 的自定义付费墙中启用购买功能"
description: "将 Adapty SDK 集成到您的自定义 Android 付费墙中，以启用应用内购买。"
---

本指南介绍如何将 Adapty 集成到您的自定义付费墙中。您可以完全掌控付费墙的实现，同时由 Adapty SDK 负责获取产品、处理新购买以及恢复历史购买。

:::important
**本指南面向正在实现自定义付费墙的开发者。** 如果您想以最简便的方式启用购买功能，请使用 [Adapty 付费墙编辑工具](android-quickstart-paywalls)。借助付费墙编辑工具，您可以在无代码可视化编辑器中创建付费墙，Adapty 会自动处理所有购买逻辑，且无需重新发布应用即可测试不同设计。
:::

## 开始之前 \{#before-you-start\}

### 配置产品 \{#set-up-products\}

要启用应用内购买，您需要了解三个关键概念：

- [**产品**](product) – 用户可以购买的任何内容（订阅、消耗型商品、永久授权）
- [**付费墙**](paywalls) – 定义要展示哪些产品的配置。在 Adapty 中，付费墙是获取产品的唯一途径，但这种设计让您无需修改应用代码即可调整产品、价格和优惠。
- [**版位**](placements) – 您在应用中展示付费墙的时机和位置（如 `main`、`onboarding`、`settings`）。您在看板中为版位配置付费墙，然后在代码中通过版位 ID 请求它们。这使得运行 A/B 测试和向不同用户展示不同付费墙变得轻而易举。

即使您使用自定义付费墙，也请务必理解这些概念。基本上，它们只是您管理应用内销售产品的方式。

要实现自定义付费墙，您需要创建一个**付费墙**并将其添加到**版位**中。此配置允许您获取产品。要了解在看板中需要执行哪些操作，请参阅[此处](quickstart)的快速入门指南。

### 管理用户 \{#manage-users\}

您可以选择在您的后端使用或不使用身份验证。

但是，Adapty SDK 对匿名用户和已识别用户的处理方式不同。请阅读[用户识别快速入门指南](android-quickstart-identify)以了解具体细节，确保您正确处理用户信息。

## 步骤 1：获取产品 \{#step-1-get-products\}

要获取自定义付费墙的产品，您需要：

1. 将[版位](placements) ID 传递给 `getPaywall` 方法以获取 `paywall` 对象。
2. 使用 `getPaywallProducts` 方法获取该付费墙的产品数组。

<Tabs groupId="current-os" queryString>

<TabItem value="kotlin" label="Kotlin" default>

```kotlin showLineNumbers

fun loadPaywall() {
    Adapty.getPaywall("YOUR_PLACEMENT_ID") { result ->
        when (result) {
            is AdaptyResult.Success -> {
                val paywall = result.value
                Adapty.getPaywallProducts(paywall) { productResult ->
                    when (productResult) {
                        is AdaptyResult.Success -> {
                            val products = productResult.value
                            // Use products to build your custom paywall UI
                        }
                        is AdaptyResult.Error -> {
                            val error = productResult.error
                            // Handle the error
                        }
                    }
                }
            }
            is AdaptyResult.Error -> {
                val error = result.error
                // Handle the error
            }
        }
    }
}
```
</TabItem>

<TabItem value="java" label="Java" default>

```java showLineNumbers

public void loadPaywall() {
    Adapty.getPaywall("YOUR_PLACEMENT_ID", result -> {
        if (result instanceof AdaptyResult.Success) {
            AdaptyPaywall paywall = ((AdaptyResult.Success<AdaptyPaywall>) result).getValue();
            
            Adapty.getPaywallProducts(paywall, productResult -> {
                if (productResult instanceof AdaptyResult.Success) {
                    List<AdaptyPaywallProduct> products = ((AdaptyResult.Success<List<AdaptyPaywallProduct>>) productResult).getValue();
                    // Use products to build your custom paywall UI
                } else if (productResult instanceof AdaptyResult.Error) {
                    AdaptyError error = ((AdaptyResult.Error) productResult).getError();
                    // Handle the error
                }
            });
        } else if (result instanceof AdaptyResult.Error) {
            AdaptyError error = ((AdaptyResult.Error) result).getError();
            // Handle the error
        }
    });
}
```
</TabItem>
</Tabs>

## 步骤 2：接受购买 \{#step-2-accept-purchases\}

当用户在您的自定义付费墙中点击某个产品时，请使用所选产品调用 `makePurchase` 方法。这将处理购买流程并返回更新后的用户画像。

<Tabs groupId="current-os" queryString>

<TabItem value="kotlin" label="Kotlin" default>

```kotlin showLineNumbers

fun purchaseProduct(activity: Activity, product: AdaptyPaywallProduct) {
    Adapty.makePurchase(activity, product) { result ->
        when (result) {
            is AdaptyResult.Success -> {
                when (val purchaseResult = result.value) {
                    is AdaptyPurchaseResult.Success -> {
                        val profile = purchaseResult.profile
                        // Purchase successful, profile updated
                    }
                    is AdaptyPurchaseResult.UserCanceled -> {
                        // User canceled the purchase
                    }
                    is AdaptyPurchaseResult.Pending -> {
                        // Purchase is pending (e.g., user will pay offline with cash)
                    }
                }
            }
            is AdaptyResult.Error -> {
                val error = result.error
                // Handle the error
            }
        }
    }
}
```
</TabItem>

<TabItem value="java" label="Java" default>

```java showLineNumbers

public void purchaseProduct(Activity activity, AdaptyPaywallProduct product) {
    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();
                // Purchase successful, profile updated
            } else if (purchaseResult instanceof AdaptyPurchaseResult.UserCanceled) {
                // User canceled the purchase
            } else if (purchaseResult instanceof AdaptyPurchaseResult.Pending) {
                // Purchase is pending (e.g., user will pay offline with cash)
            }
        } else if (result instanceof AdaptyResult.Error) {
            AdaptyError error = ((AdaptyResult.Error) result).getError();
            // Handle the error
        }
    });
}
```
</TabItem>
</Tabs>

## 步骤 3：恢复购买 \{#step-3-restore-purchases\}

Google Play 及其他应用商店要求所有包含订阅功能的应用提供用户恢复购买的途径。

当用户点击恢复按钮时，调用 `restorePurchases` 方法。这将把他们的购买历史与 Adapty 同步，并返回更新后的用户画像。

<Tabs groupId="current-os" queryString>

<TabItem value="kotlin" label="Kotlin" default>

```kotlin showLineNumbers

fun restorePurchases() {
    Adapty.restorePurchases { result ->
        when (result) {
            is AdaptyResult.Success -> {
                val profile = result.value
                // Restore successful, profile updated
            }
            is AdaptyResult.Error -> {
                val error = result.error
                // Handle the error
            }
        }
    }
}
```
</TabItem>

<TabItem value="java" label="Java" default>

```java showLineNumbers

public void restorePurchases() {
    Adapty.restorePurchases(result -> {
        if (result instanceof AdaptyResult.Success) {
            AdaptyProfile profile = ((AdaptyResult.Success<AdaptyProfile>) result).getValue();
            // Restore successful, profile updated
        } else if (result instanceof AdaptyResult.Error) {
            AdaptyError error = ((AdaptyResult.Error) result).getError();
            // Handle the error
        }
    });
}
```
</TabItem>
</Tabs>

## 后续步骤 \{#next-steps\}

---
no_index: true
---
import Callout from '../../../components/Callout.astro';

<Callout type="tip">
有疑问或遇到问题？请访问我们的[支持论坛](https://adapty.featurebase.app/)，您可以在那里找到常见问题的解答，或者提出您自己的问题。我们的团队和社区随时为您提供帮助！
</Callout>

您的付费墙已准备好在应用中展示。[在 Google Play Store 中测试您的购买](testing-on-android)，确保您可以从付费墙完成测试购买。要查看这在生产就绪实现中的工作方式，请查看我们示例应用中的 [ProductListFragment.kt](https://github.com/adaptyteam/AdaptySDK-Android/blob/master/app/src/main/java/com/adapty/example/ProductListFragment.kt)，该文件演示了带有适当错误处理、UI 反馈和订阅管理的购买处理。

接下来，[检查用户是否已完成购买](android-check-subscription-status)，以确定是否显示付费墙或授予对付费功能的访问权限。