How to add in app purchases in Android

here is the most up to date in-app purchases tutorial which will help you in implementing in-app purchases in your app.

     Adding in-app purchases to Android is a very lengthy and complicated tutorial and from what I have seen on various in-app purchases tutorials on the internet, most of them are either outdated or have a shit ton of errors in them. I did get some working tutorials but there were rather incomplete and a little old and Google as you know keeps on updating libraries and codes in Android so here is the most up to date in-app purchases tutorial which will help you in implementing in-app purchases in your app.
How to add in app purchases in Android

How to add in-app purchases in Android

How to add in app purchases in Android
This in-app tutorial uses billing client 1.0 and does not have any AIDL implementation as Google has already deprecated support for Google play billing using AIDL as of August 2019 as stated in the official documentation page of Google Play billing using AIDL.

To get started to open your app level build.gradle file and add the following dependency line to it.

[implementation 'com.android.billingclient:billing:1.0']

Now create a BillingManager.java file in your apps com.example.app folder and initialize BillingClient inside it.

[private final BillingClient mBillingClient;
private final Activity mActivity;
public BillingManager(Activity activity) {
    mActivity = activity;
mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();

mBillingClient.startConnection(new BillingClientStateListener() {
        @Override        public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponse) {
            if (billingResponse == BillingClient.BillingResponse.OK) {
                Log.i(TAG, "onBillingSetupFinished() response: " + billingResponse);
            } else {
                Log.w(TAG, "onBillingSetupFinished() error code: " + billingResponse);
            }
        }
        @Override        public void onBillingServiceDisconnected() {
            Log.w(TAG, "onBillingServiceDisconnected()");
        }
    });
}]

Now build the application and test it out on your device. Your log should show "onBillingSetupFinished() response: 0". This indicates a successful connection to the billing client. If you get any other error then it means your application is not published on PlayStore. For more information read How to test in-app purchases on Android

Add a list of all SKUs in your code.

[private static final HashMap> SKUS;
static{
    SKUS = new HashMap<>();
    SKUS.put(BillingClient.SkuType.INAPP, Arrays.asList("gas", "premium"));
    SKUS.put(BillingClient.SkuType.SUBS, Arrays.asList("gold_monthly", "gold_yearly"));
}
public List getSkus(@BillingClient.SkuType String type) {
    return SKUS.get(type);
}]

Just below that QuerySku details in a method

[public void querySkuDetailsAsync(@BillingClient.SkuType final String itemType,
final List skuList, final SkuDetailsResponseListener listener) {
    SkuDetailsParams skuDetailsParams = SkuDetailsParams.newBuilder()
            .setSkusList(skuList).setType(itemType).build();
    mBillingClient.querySkuDetailsAsync(skuDetailsParams,
            new SkuDetailsResponseListener() {
                @Override
public void onSkuDetailsResponse(int responseCode,
List skuDetailsList) {
listener.onSkuDetailsResponse(responseCode, skuDetailsList);
 }
});
}]

In your onClick method where you want to make purchases start querying for SKUs

[List inAppSkus = mBillingProvider.getBillingManager()
.getSkus(BillingClient.SkuType.INAPP);
mBillingProvider.getBillingManager().querySkuDetailsAsync(BillingClient.SkuType.INAPP,
        inAppSkus,
        new SkuDetailsResponseListener() {
            @Override            public void onSkuDetailsResponse(int responseCode,
                                             List skuDetailsList) {
                if (responseCode == BillingClient.BillingResponse.OK                        && skuDetailsList != null) {
                    for (SkuDetails details : skuDetailsList) {
                        Log.w(TAG, "Got a SKU: " + details);
                    }
                }
            }
});]

You will now see the details of your SKUs in your log file. Replace the if loop above with this code so that your SKU list will be updated.

[if (responseCode == BillingClient.BillingResponse.OK && skuDetailsList != null) {
List inList = new ArrayList<>();
for (SkuDetails details : skuDetailsList) {
        Log.i(TAG, "Found sku: " + details);
        inList.add(new SkuRowData(details.getSku(), details.getTitle(), details.getPrice(),
                details.getDescription(), details.getType()));
        if (inList.size() == 0) {
            //displayAnErrorIfNeeded();
        } else {
            mAdapter.updateData(inList);
            //setWaitScreen(false);
        }
    }
}]

As you have not set a list adapter madapter you will have to first set list adapter.

[public void onManagerReady(BillingProvider billingProvider) {
mBillingProvider = billingProvider;
    if (mRecyclerView != null) {
        mAdapter = new SkusAdapter(mBillingProvider);
        if (mRecyclerView.getAdapter() == null) {
            mRecyclerView.setAdapter(mAdapter);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        }
        handleManagerAndUiReady();
    }
}]

You will have to declare mRecycler view using mRecyclerView = (RecyclerView) findViewById(R.id.list); where the list is the id of your list view where you will display all the purchases.
To start billing flow add this method into BillingManager.java

[public void startPurchaseFlow(String skuId, String billingType) {
// TODO: Implement launch billing flow here    BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
            .setType(billingType).setSku(skuId).build();
    mBillingClient.launchBillingFlow(mActivity,billingFlowParams);
}]

Reformat the whole code by selecting the whole code pressing CTRL+A and then using CTRL+ALT+L. Now, just open the app and you have successfully implemented in-app purchases.

If you are still facing difficulties you can check out in-app purchase Codelab tutorial.
Name

Affiliate marketing tutorial,2,Android,60,Android Development,22,Android tutorials,41,Blogging tactics,8,Deals,1,DSA,4,Elite Youtube Tutorial,4,Firebase,1,Flutter,4,Fuchsia,1,Hosting Guides,3,Leetcode,4,Miscellaneous,10,Mobile Gaming,17,NTN,94,Problogging,7,PUBG,16,Reviews,1,SEO Tutorial,5,Start a blog,9,Start a channel,6,Top 5,4,Travel Blogging,4,Windows,12,Windows Security,4,Windows tutorials,7,Wordpress Tutorials,4,Youtube Tutorials,11,
ltr
item
A Passionate Developers Creation: How to add in app purchases in Android
How to add in app purchases in Android
here is the most up to date in-app purchases tutorial which will help you in implementing in-app purchases in your app.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBr5h7FNQj6ugCHi42IbleVl0Bam3q00kTX_KQzOEkxPt3HqkbS2ueXn5egvoxcyrdWTKxGUiI2uXTnHUgo2hIBLshii1NPW-a5YnUO2RtC-7IuE_cSSEmn9uts1dwisr5F8MLbOY0U1U/s1600/ZomboDroid+27072019195151.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBr5h7FNQj6ugCHi42IbleVl0Bam3q00kTX_KQzOEkxPt3HqkbS2ueXn5egvoxcyrdWTKxGUiI2uXTnHUgo2hIBLshii1NPW-a5YnUO2RtC-7IuE_cSSEmn9uts1dwisr5F8MLbOY0U1U/s72-c/ZomboDroid+27072019195151.jpg
A Passionate Developers Creation
https://www.apdevc.com/2019/07/how-to-add-in-app-purchases-in-android.html
https://www.apdevc.com/
https://www.apdevc.com/
https://www.apdevc.com/2019/07/how-to-add-in-app-purchases-in-android.html
true
3690156770086819421
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content