Muhammad Aziz
Muhammad Aziz

Reputation: 1

How can I make purchase with RevenueCat by Kotlin?

How can I make a purchase using RevenueCat v6.0.0 by Kotlin, I can see in their Docs :

Purchases.sharedInstance.purchaseWith(
  PurchaseParams.Builder(this, aPackage).build(),
  onError = { error, userCancelled -> /* No purchase */ },
  onSuccess = { storeTransaction, customerInfo ->
    if (customerInfo.entitlements["my_entitlement_identifier"]?.isActive == true) {
      // Unlock that great "pro" content
    }
  }
)

But Unfortunately I can't create this (aPackage) variable, for example if I have a product ID named: "monthly_sub", How can I get it and then pass it in this method (purchaseWith)?

I've tried to apply the docs and expect it to open Google Play paywall to make the purchase.

Upvotes: -1

Views: 350

Answers (1)

Miguel Lasa
Miguel Lasa

Reputation: 518

Bare with me, I might be missing some small thing, but one thing you could do is get your products, and use what you get back to perform the purchase:

  1. Get your products with:

    Purchases.sharedInstance.getProductsWith(
        productIds = listOf("your_product_group_id"),
        type = ProductType.SUBS,
        onError = { error ->
            // Error
        }
    ) { productList ->
        // Just get the first element as an example
        productList.firstOrNull()?.let {
            // You wouldn't do it this way per se, but just to show how can it be done
            performPurchase(storeProduct = it)
        }
    }
    
  2. Perform the purchase:

    fun performPurchase(storeProduct: StoreProduct) {
        Purchases.sharedInstance.purchaseWith(
            purchaseParams = PurchaseParams.Builder(this, storeProduct).build(),
            onError = { error, userCancelled ->
                // Error
            }
        ) { purchase, customerInfo ->
            // Success
        }
    }
    

If you need a Paywall approach I think you can also refer to this piece of code.

Upvotes: 0

Related Questions