Purpl
Docs menu

Purpl Docs

Products and purchases

Load Google Play purchase options and purchase the option selected by the customer.

Define purchase options

A Google Play subscription can have several base plans, such as monthly and yearly. PurchaseOption defines one choice as a combination of a product, base plan, and offer, and PurchaseCatalog defines the options to sell and their display order.

Kotlin
val standardCatalog = PurchaseCatalog(
    identifier = "standard",
    options = listOf(
        PurchaseOption(
            identifier = "plus-monthly",
            productIdentifier = "time.plus",
            basePlanIdentifier = "monthly"
        ),
        PurchaseOption(
            identifier = "plus-yearly",
            productIdentifier = "time.plus",
            basePlanIdentifier = "yearly"
        )
    )
)

Purchase an option

Kotlin
val options = purchases.products(standardCatalog)

val yearlyOption = options.firstOrNull {
    it.identifier == "plus-yearly"
} ?: return

val result = purchases.purchase(activity, yearlyOption)

products returns only the options Google Play currently offers to the customer, with their prices. purchase needs the Activity that presents the purchase flow, and PurchaseResult distinguishes Completed, Pending, and Cancelled. Verification and store failures are thrown as PurchasesException.

Change subscriptions

To change a subscription, choose the existing purchase from activePurchases() and pass its purchase token and your replacement mode as a SubscriptionReplacement. The SDK never guesses whether a change is an upgrade or a downgrade.

Kotlin
val currentPurchase = purchases.activePurchases().singleOrNull() ?: return

val result = purchases.purchase(
    activity = activity,
    option = yearlyOption,
    replacement = SubscriptionReplacement(
        purchaseToken = currentPurchase.purchaseToken,
        mode = SubscriptionReplacementMode.WithTimeProration
    )
)

In your app settings, open the Google Play subscription management screen with subscriptionManagementUrl(productIdentifier).