Search code examples
androidkotlinin-app-purchaseads

In-App Purchase to Remove Ads - Android/Kotlin


I have some app on Google Store (in Kotlin) with AdMob and now i wanna add in-app purchase to remove ads. I was looking for some good tutorial, but without results, because all of them are veeery old (2016-2017).

Can anyone guide me, how to implement this? I know i can use SharedPreferences, but than if someone reinstall my app or delete memory he will lost purchased item.

Thanks guys for help!

@Edit Purchase is in my MainActivity, user should can buy Remove Ads after click on button who id is "noAdsButton" and id for my product in Google Play it's "no_ads"

 private lateinit var mBillingClient: BillingClient


...



    open fun onPurchasesUpdated(
        billingResult: BillingResult,
        list: List<Purchase>?
    ): Unit {
        if (billingResult.responseCode == BillingClient.BillingResponseCode.OK
            && list != null
        ) {
            for (purchase in list) {
                if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
                    if (!purchase.isAcknowledged) {
                        val acknowledgePurchaseParams =
                            AcknowledgePurchaseParams.newBuilder()
                                .setPurchaseToken(purchase.purchaseToken)
                                .build()
                        mBillingClient.acknowledgePurchase(
                            acknowledgePurchaseParams,
                            AcknowledgePurchaseResponseListener { billingResult ->
                                if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                                    Toast.makeText(
                                        this@MainActivity,
                                        "Ads Removed",
                                        Toast.LENGTH_SHORT
                                    ).show()
                                    preferences.edit().putBoolean("show_ads", false).apply()
                                    startActivity(
                                        Intent(
                                            this@MainActivity,
                                            MainActivity::class.java
                                        )
                                    )
                                    finish()
                                } else {
                                    Toast.makeText(
                                        this@MainActivity,
                                        "Error",
                                        Toast.LENGTH_LONG
                                    ).show()
                                }
                            })
                    }
                }
            }
        } else if (billingResult.responseCode == BillingClient.BillingResponseCode.USER_CANCELED) {
            Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show()
        } else if (billingResult.responseCode == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) {
            preferences.edit().putBoolean("show_ads", false).apply()
            Toast.makeText(this, "Ads removed because you buy before", Toast.LENGTH_LONG).show()
        } else {
            Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show()
        }
    }

when i wanna start purchase should i set onClickListener on button and start function onPurchasesUpdated("what here?")


Solution

  • if someone reinstall your app lost purchased item but he/she can buy again free. I am using SharedPreferences for this and it works.

    You can control this with ITEM_ALREAY_OWNED method

    if(billingResult.getResponseCode() == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) {
        preferences.edit().putBoolean("show_ads", false).apply();
        Toast.makeText(this, "Ads removed because you buy before", Toast.LENGTH_LONG).show();
       }
    

    and all

     @Override
    public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> list) {
        if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
                && list != null) {
            for (Purchase purchase : list) {
                if(purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED){
                    if(!purchase.isAcknowledged()){
                        AcknowledgePurchaseParams acknowledgePurchaseParams =
                                AcknowledgePurchaseParams.newBuilder()
                                        .setPurchaseToken(purchase.getPurchaseToken())
                                        .build();
                        mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
                            @Override
                            public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
                                if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.OK) {
                                    Toast.makeText(MainActivity.this, "Ads Removed", Toast.LENGTH_SHORT).show();
                                    preferences.edit().putBoolean("show_ads", false).apply();
                                    startActivity(new Intent(MainActivity.this, MainActivity.class));
                                    finish();
                                }else{
                                    Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                    }
    
                }
            }
        } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
            Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
        } else if(billingResult.getResponseCode() == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) {
            preferences.edit().putBoolean("show_ads", false).apply();
            Toast.makeText(this, "Ads removed because you buy before", Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
        }
    
    }
    

    and setup billing method

        private void setupBilling() {
        mBillingClient = BillingClient.newBuilder(MainActivity.this).setListener(this).enablePendingPurchases().build();
        mBillingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(BillingResult billingResult) {
                if (billingResult.getResponseCode() ==  BillingClient.BillingResponseCode.OK) {
                    Toast.makeText(MainActivity.this, "Ödeme sistemi etkin", Toast.LENGTH_SHORT).show();
    
                    final List<String> skuList = new ArrayList<> ();
                    skuList.add("remove_ads");
                    final SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
                    params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
                    mBillingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
                        @Override
                        public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> list) {
                            if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && list != null) {
                                BillingFlowParams flowParams = BillingFlowParams.newBuilder()
                                        .setSkuDetails(list.get(0))
                                        .build();
                                mBillingClient.launchBillingFlow(MainActivity.this, flowParams);
                            }
                        }
                    });
                }else{
                    Toast.makeText(MainActivity.this, "Ödeme sistemi için google play hesabını kontrol ediniz", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onBillingServiceDisconnected() {
                Toast.makeText(MainActivity.this, "Ödeme sistemi şuanda geçerli değil", Toast.LENGTH_SHORT).show();
            }
        });
    }