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.
Now create a BillingManager.java file in your apps com.example.app folder and initialize BillingClient inside it.
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.
Just below that QuerySku details in a method
In your onClick method where you want to make purchases start querying for SKUs
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.
As you have not set a list adapter madapter you will have to first set list adapter.
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
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.
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 ListgetSkus(@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 ListskuList, final SkuDetailsResponseListener listener) {
SkuDetailsParams skuDetailsParams = SkuDetailsParams.newBuilder()
.setSkusList(skuList).setType(itemType).build();
mBillingClient.querySkuDetailsAsync(skuDetailsParams,
new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(int responseCode,
ListskuDetailsList) {
listener.onSkuDetailsResponse(responseCode, skuDetailsList);
}
});
}]
In your onClick method where you want to make purchases start querying for SKUs
[ListinAppSkus = mBillingProvider.getBillingManager()
.getSkus(BillingClient.SkuType.INAPP);
mBillingProvider.getBillingManager().querySkuDetailsAsync(BillingClient.SkuType.INAPP,
inAppSkus,
new SkuDetailsResponseListener() {
@Override public void onSkuDetailsResponse(int responseCode,
ListskuDetailsList) {
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) {
ListinList = 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.