Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Google Billing 6 library #528

Merged
18 changes: 11 additions & 7 deletions app/src/main/java/com/qonversion/android/app/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.qonversion.android.sdk.automations.dto.QActionResult
import com.qonversion.android.sdk.automations.dto.QActionResultType
import com.qonversion.android.sdk.automations.dto.QScreenPresentationConfig
import com.qonversion.android.sdk.automations.dto.QScreenPresentationStyle
import com.qonversion.android.sdk.dto.QPurchaseModel
import com.qonversion.android.sdk.dto.entitlements.QEntitlement
import com.qonversion.android.sdk.dto.QonversionError
import com.qonversion.android.sdk.dto.products.QProduct
Expand All @@ -34,7 +35,7 @@ private const val TAG = "HomeFragment"
class HomeFragment : Fragment() {
lateinit var binding: FragmentHomeBinding

private val productIdSubs = "main"
private val productIdSubs = "weekly"
private val productIdInApp = "in_app"
private val entitlementPlus = "plus"
private val entitlementStandart = "standart"
Expand Down Expand Up @@ -135,18 +136,21 @@ class HomeFragment : Fragment() {
binding.buttonRestore.text = getStr(R.string.restore_purchases)

val subscription = products[productIdSubs]
if (subscription != null) {
subscription?.storeDetails?.defaultSubscriptionOfferDetails?.let {
binding.buttonSubscribe.text = String.format(
"%s %s / %s", getStr(R.string.subscribe_for),
subscription.prettyPrice, subscription.duration?.name
"%s %s / %d %s",
getStr(R.string.subscribe_for),
it.basePlan?.price?.formattedPrice,
it.basePlan?.billingPeriod?.unitCount,
it.basePlan?.billingPeriod?.unit?.name,
)
}

val inApp = products[productIdInApp]
if (inApp != null) {
inApp?.storeDetails?.inAppOfferDetails?.let {
binding.buttonInApp.text = String.format(
"%s %s", getStr(R.string.buy_for),
inApp.prettyPrice
it.price.formattedPrice
)
}
}
Expand All @@ -172,7 +176,7 @@ class HomeFragment : Fragment() {
private fun purchase(productId: String) {
Qonversion.shared.purchase(
requireActivity(),
productId,
QPurchaseModel(productId, "trial-plus-intro"),
callback = object : QonversionEntitlementsCallback {
override fun onSuccess(entitlements: Map<String, QEntitlement>) {
when (productId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.*;
import com.android.billingclient.api.SkuDetailsParams;
import com.android.billingclient.api.ProductDetails;
import com.android.billingclient.api.QueryProductDetailsParams;
import com.qonversion.android.sdk.Qonversion;

import java.util.Collections;
Expand All @@ -22,12 +22,11 @@

public class ManualTrackingActivity extends AppCompatActivity {

private static final String SKU_ID = "your_sku_id";
private static final String PRODUCT_ID = "your_product_id";

private BillingClient client;

@SuppressWarnings("deprecation")
private final Map<String, SkuDetails> skuDetails = new HashMap<>();
private final Map<String, ProductDetails> productDetails = new HashMap<>();

@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
Expand All @@ -36,18 +35,16 @@ public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableB
client = BillingClient
.newBuilder(this)
.enablePendingPurchases()
.setListener((billingResult, list) -> {
.setListener((billingResult, purchases) -> {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
if (list != null && !list.isEmpty()) {
if (purchases != null && !purchases.isEmpty()) {
Qonversion.getSharedInstance().syncPurchases();
}
}
})
.build();


launchBilling();

}

private void launchBilling() {
Expand All @@ -56,18 +53,21 @@ private void launchBilling() {
public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {

@SuppressWarnings("deprecation")
final SkuDetailsParams params = SkuDetailsParams
final QueryProductDetailsParams.Product product = QueryProductDetailsParams.Product
.newBuilder()
.setProductId(PRODUCT_ID)
.setProductType(BillingClient.ProductType.INAPP)
.build();

final QueryProductDetailsParams params = QueryProductDetailsParams
.newBuilder()
.setSkusList(Collections.singletonList(SKU_ID))
.setType(BillingClient.SkuType.INAPP)
.setProductList(Collections.singletonList(product))
.build();

//noinspection deprecation
client.querySkuDetailsAsync(params, (queryBillingResult, list) -> {
client.queryProductDetailsAsync(params, (queryBillingResult, details) -> {
if (queryBillingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
if (list != null && !list.isEmpty()) {
skuDetails.put(SKU_ID, list.get(0));
if (!details.isEmpty()) {
productDetails.put(PRODUCT_ID, details.get(0));
}
launchBillingFlow();
}
Expand All @@ -84,11 +84,16 @@ public void onBillingServiceDisconnected() {
}

private void launchBillingFlow() {
@SuppressWarnings("deprecation")
final BillingFlowParams.ProductDetailsParams productDetailsParams = BillingFlowParams.ProductDetailsParams
.newBuilder()
.setProductDetails(Objects.requireNonNull(productDetails.get(PRODUCT_ID)))
.build();

final BillingFlowParams params = BillingFlowParams
.newBuilder()
.setSkuDetails(Objects.requireNonNull(skuDetails.get(SKU_ID)))
.setProductDetailsParamsList(Collections.singletonList(productDetailsParams))
.build();

client.launchBillingFlow(this, params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,87 @@ package com.qonversion.android.app
import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import com.android.billingclient.api.*
import com.android.billingclient.api.BillingClient
import com.android.billingclient.api.BillingClientStateListener
import com.android.billingclient.api.BillingFlowParams
import com.android.billingclient.api.BillingResult
import com.android.billingclient.api.ProductDetails
import com.android.billingclient.api.QueryProductDetailsParams
import com.qonversion.android.sdk.Qonversion
import java.util.*

class ManualTrackingActivityKt : AppCompatActivity() {
private var client: BillingClient? = null
@Suppress("DEPRECATION")
private val skuDetails: MutableMap<String, SkuDetails?> =
HashMap()
private lateinit var client: BillingClient

override fun onCreate(
savedInstanceState: Bundle?,
persistentState: PersistableBundle?
) {
private val productDetails: MutableMap<String, ProductDetails> = HashMap()

override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
client = BillingClient
.newBuilder(this)
.enablePendingPurchases()
.setListener { billingResult, list ->
.setListener { billingResult, purchases ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
if (!list.isNullOrEmpty()) {
if (!purchases.isNullOrEmpty()) {
Qonversion.shared.syncPurchases()
}
}
}
.build()

launchBilling()
}

private fun launchBilling() {
client!!.startConnection(object : BillingClientStateListener {
client.startConnection(object : BillingClientStateListener {
override fun onBillingSetupFinished(billingResult: BillingResult) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
querySkuDetailsAsync()
queryProductDetailsAsync()
}
}

override fun onBillingServiceDisconnected() { // ignore in example
override fun onBillingServiceDisconnected() {
// ignore in example
}
})
}

private fun querySkuDetailsAsync() {
@Suppress("DEPRECATION")
val params =
SkuDetailsParams
.newBuilder()
.setSkusList(listOf(SKU_ID))
.setType(BillingClient.SkuType.INAPP)
.build()
private fun queryProductDetailsAsync() {
val product = QueryProductDetailsParams.Product
.newBuilder()
.setProductId(PRODUCT_ID)
.setProductType(BillingClient.ProductType.INAPP)
.build()

val params = QueryProductDetailsParams
.newBuilder()
.setProductList(listOf(product))
.build()

@Suppress("DEPRECATION")
client!!.querySkuDetailsAsync(
params
) { billingResult, list ->
client.queryProductDetailsAsync(params) { billingResult, details ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
if (list!!.isNotEmpty()) {
skuDetails[SKU_ID] = list[0]
if (details.isNotEmpty()) {
productDetails[PRODUCT_ID] = details.first()
}
launchBillingFlow()
}
}
}

private fun launchBillingFlow() {
@Suppress("DEPRECATION")
val params =
BillingFlowParams
.newBuilder()
.setSkuDetails(skuDetails[SKU_ID]!!)
.build()
client!!.launchBillingFlow(this, params)
val productDetailsParams = BillingFlowParams.ProductDetailsParams
.newBuilder()
.setProductDetails(productDetails[PRODUCT_ID]!!)
.build()

val params = BillingFlowParams
.newBuilder()
.setProductDetailsParamsList(listOf(productDetailsParams))
.build()

client.launchBillingFlow(this, params)
}

companion object {
private const val SKU_ID = "your_sku_id"
private const val PRODUCT_ID = "your_product_id"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class OfferingsFragment : Fragment() {
}

private fun purchase(product: QProduct) {
Qonversion.shared.purchase(requireActivity(), product, callback = object :
Qonversion.shared.purchase(requireActivity(), product.toPurchaseModel(), callback = object :
QonversionEntitlementsCallback {
override fun onSuccess(entitlements: Map<String, QEntitlement>) {
Toast.makeText(context, "Purchase succeeded", Toast.LENGTH_LONG).show()
Expand Down
Loading
Loading