---
title: "Bật tính năng mua hàng trong custom paywall trên Android SDK"
description: "Tích hợp Adapty SDK vào custom paywall Android để bật tính năng in-app purchase."
---

Hướng dẫn này mô tả cách tích hợp Adapty vào custom paywall của bạn. Bạn toàn quyền kiểm soát việc triển khai paywall, trong khi Adapty SDK lo việc lấy sản phẩm, xử lý giao dịch mới và khôi phục giao dịch cũ.

:::important
**Hướng dẫn này dành cho các nhà phát triển đang triển khai custom paywall.** Nếu bạn muốn cách đơn giản nhất để bật tính năng mua hàng, hãy sử dụng [Adapty Paywall Builder](android-quickstart-paywalls). Với Paywall Builder, bạn tạo paywall bằng trình chỉnh sửa trực quan không cần code, Adapty tự động xử lý toàn bộ logic mua hàng, và bạn có thể thử nghiệm các thiết kế khác nhau mà không cần phát hành lại ứng dụng.
:::

## Trước khi bắt đầu \{#before-you-start\}

### Thiết lập sản phẩm \{#set-up-products\}

Để bật tính năng in-app purchase, bạn cần hiểu ba khái niệm chính:

- [**Sản phẩm**](product) – bất cứ thứ gì người dùng có thể mua (gói đăng ký, consumable, quyền truy cập trọn đời)
- [**Paywall**](paywalls) – các cấu hình xác định sản phẩm nào sẽ được hiển thị. Trong Adapty, paywall là cách duy nhất để lấy sản phẩm, nhưng thiết kế này cho phép bạn thay đổi sản phẩm, giá cả và ưu đãi mà không cần chỉnh sửa code ứng dụng.
- [**Placement**](placements) – nơi và thời điểm bạn hiển thị paywall trong ứng dụng (như `main`, `onboarding`, `settings`). Bạn thiết lập paywall cho các placement trên dashboard, sau đó gọi chúng bằng placement ID trong code. Điều này giúp dễ dàng chạy A/B test và hiển thị paywall khác nhau cho từng nhóm người dùng.

Hãy đảm bảo bạn hiểu các khái niệm này ngay cả khi làm việc với custom paywall. Về cơ bản, đây chỉ là cách bạn quản lý các sản phẩm bán trong ứng dụng.

Để triển khai custom paywall, bạn cần tạo một **paywall** và thêm nó vào một **placement**. Thiết lập này cho phép bạn lấy các sản phẩm. Để hiểu những gì cần làm trên dashboard, hãy theo dõi hướng dẫn khởi đầu nhanh [tại đây](quickstart).

### Quản lý người dùng \{#manage-users\}

Bạn có thể làm việc có hoặc không có xác thực backend ở phía mình.

Tuy nhiên, Adapty SDK xử lý người dùng ẩn danh và người dùng đã xác định theo các cách khác nhau. Đọc [hướng dẫn khởi đầu nhanh về nhận dạng người dùng](android-quickstart-identify) để hiểu rõ sự khác biệt và đảm bảo bạn đang làm việc với người dùng đúng cách.

## Bước 1. Lấy sản phẩm \{#step-1-get-products\}

Để lấy sản phẩm cho custom paywall, bạn cần:

1. Lấy đối tượng `paywall` bằng cách truyền ID [placement](placements) vào phương thức `getPaywall`.
2. Lấy mảng sản phẩm cho paywall này bằng phương thức `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>

## Bước 2. Nhận giao dịch mua hàng \{#step-2-accept-purchases\}

Khi người dùng nhấn vào một sản phẩm trong custom paywall, hãy gọi phương thức `makePurchase` với sản phẩm đã chọn. Thao tác này sẽ xử lý luồng mua hàng và trả về hồ sơ người dùng đã được cập nhật.

<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>

## Bước 3. Khôi phục giao dịch mua hàng \{#step-3-restore-purchases\}

Google Play và các cửa hàng ứng dụng khác yêu cầu tất cả ứng dụng có gói đăng ký phải cung cấp cách để người dùng khôi phục giao dịch mua hàng của họ.

Gọi phương thức `restorePurchases` khi người dùng nhấn nút khôi phục. Thao tác này sẽ đồng bộ lịch sử mua hàng của họ với Adapty và trả về hồ sơ người dùng đã được cập nhật.

<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>

## Các bước tiếp theo \{#next-steps\}

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

<Callout type="tip">
Bạn có câu hỏi hoặc gặp sự cố? Hãy xem [diễn đàn hỗ trợ](https://adapty.featurebase.app/) của chúng tôi — nơi bạn có thể tìm câu trả lời cho các câu hỏi thường gặp hoặc đặt câu hỏi của riêng mình. Đội ngũ và cộng đồng của chúng tôi luôn sẵn sàng giúp đỡ!
</Callout>

Paywall của bạn đã sẵn sàng để hiển thị trong ứng dụng. [Kiểm thử giao dịch mua hàng trên Google Play Store](testing-on-android) để đảm bảo bạn có thể hoàn thành một giao dịch thử nghiệm từ paywall. Để xem cách hoạt động trong một triển khai sẵn sàng cho môi trường production, hãy xem [ProductListFragment.kt](https://github.com/adaptyteam/AdaptySDK-Android/blob/master/app/src/main/java/com/adapty/example/ProductListFragment.kt) trong ứng dụng ví dụ của chúng tôi, minh họa cách xử lý giao dịch mua hàng với error handling đầy đủ, phản hồi giao diện người dùng và quản lý gói đăng ký.

Tiếp theo, [kiểm tra xem người dùng đã hoàn tất giao dịch mua hàng chưa](android-check-subscription-status) để quyết định có hiển thị paywall hay cấp quyền truy cập vào các tính năng trả phí.