Google Play billing library version 4
The Google Play Billing Library makes it easy to implement In-app Billing in your Android app. With just a few lines of code, you can sell digital products, subscriptions, and more. And, because the library is part of Google Play services, you can use it with all of your Android devices.
Best of all, the Google Play Billing Library is free and open source. So if you’re looking for a way to start selling In-app Billing in your Android app, this is the library for you.
Google Play billing library version 4 | In-App Purchase
The Google play billing library makes it easy to add in-app purchases to your Android app. With just a few lines of code, you can start selling digital goods and services to your users. The library handles all of the complex billing and payment processing for you, so you can focus on building a great app.
Best of all, the Google play billing library is free to use. So if you’re looking to add in-app purchases to your app, you can do it without spending a dime. So what are you waiting for? Get started today and start making money from your app!
Read More : How to get Canva pro for free | LifeTime Free Canva Pro
Google Play billing library version 4 | Free Source code
Copy From Here
implementation ‘com.github.moisoni97:google-inapp-billing:1.0.5’
————————
<ListView
android:id=”@+id/list_view”
android:layout_width=”match_parent”
android:layout_height=”match_parent” />
——————
private BillingConnector billingConnector;
//create a list with consumable ids
private final List<String> consumableIds = new ArrayList<>();
private final ArrayList<String> purchaseItemDisplay = new ArrayList<>();
private ArrayAdapter arrayAdapter;
private ListView listView;
——————————
listView = findViewById(R.id.list_view);
arrayAdapter = new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_list_item_1,purchaseItemDisplay);
listView.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
initializeBillingClient();
listView.setOnItemClickListener((parent, view, position, id) -> billingConnector.purchase(activity_donate.this, consumableIds.get(position)));
——————————–
private void initializeBillingClient() {
consumableIds.add(“1dollar”);
consumableIds.add(“2dollar”);
consumableIds.add(“3dollar”);
consumableIds.add(“4dollar”);
consumableIds.add(“5dollar”);
billingConnector = new BillingConnector(getApplicationContext(), String.valueOf(license_key)) //”license_key” – public developer key from Play Console
.setConsumableIds(consumableIds) //to set consumable ids – call only for consumable products
.autoAcknowledge() //legacy option – better call this. Alternatively purchases can be acknowledge via public method “acknowledgePurchase(PurchaseInfo purchaseInfo)”
.autoConsume() //legacy option – better call this. Alternatively purchases can be consumed via public method consumePurchase(PurchaseInfo purchaseInfo)”
.enableLogging() //to enable logging for debugging throughout the library – this can be skipped
.connect(); //to connect billing client with Play Console
billingConnector.setBillingEventListener(new BillingEventListener() {
@Override
public void onProductsFetched(@NonNull List<SkuInfo> skuDetails) {
notifyList(skuDetails);
}
@Override
public void onPurchasedProductsFetched(@NonNull List<PurchaseInfo> purchases) {
}
@Override
public void onProductsPurchased(@NonNull List<PurchaseInfo> purchases) {
String skuName;
for (PurchaseInfo purchaseInfo : purchases) {
skuName = purchaseInfo.getSkuInfo().getTitle();
Toast.makeText(getApplicationContext(), “Product purchased : ” + skuName, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onPurchaseAcknowledged(@NonNull PurchaseInfo purchase) {
/*
* Grant user entitlement for NON-CONSUMABLE products and SUBSCRIPTIONS here
*
* Even though onProductsPurchased is triggered when a purchase is successfully made
* there might be a problem along the way with the payment and the purchase won’t be acknowledged
*
* Google will refund users purchases that aren’t acknowledged in 3 days
*
* To ensure that all valid purchases are acknowledged the library will automatically
* check and acknowledge all unacknowledged products at the startup
* */
String acknowledgedSku = purchase.getSku();
Log.d(“BillingConnector”, “Acknowledged: ” + acknowledgedSku);
Toast.makeText(getApplicationContext(), “Acknowledged : ” + acknowledgedSku, Toast.LENGTH_SHORT).show();
}
@Override
public void onPurchaseConsumed(@NonNull PurchaseInfo purchase) {
/*
* CONSUMABLE products entitlement can be granted either here or in onProductsPurchased
* */
String consumedSkuTitle = purchase.getSkuInfo().getTitle();
Log.d(“BillingConnector”, “Consumed: ” + consumedSkuTitle);
Toast.makeText(getApplicationContext(), “Consumed : ” + consumedSkuTitle, Toast.LENGTH_SHORT).show();
}
@Override
public void onBillingError(@NonNull BillingConnector billingConnector, @NonNull BillingResponse response) {
switch (response.getErrorType()) {
case CLIENT_NOT_READY:
//TODO – client is not ready yet
break;
case CLIENT_DISCONNECTED:
//TODO – client has disconnected
break;
case SKU_NOT_EXIST:
//TODO – sku does not exist
break;
case CONSUME_ERROR:
//TODO – error during consumption
break;
case ACKNOWLEDGE_ERROR:
//TODO – error during acknowledgment
break;
case ACKNOWLEDGE_WARNING:
/*
* This will be triggered when a purchase can not be acknowledged because the state is PENDING
* A purchase can be acknowledged only when the state is PURCHASED
*
* PENDING transactions usually occur when users choose cash as their form of payment
*
* Here users can be informed that it may take a while until the purchase complete
* and to come back later to receive their purchase
* */
//TODO – warning during acknowledgment
break;
case FETCH_PURCHASED_PRODUCTS_ERROR:
//TODO – error occurred while querying purchased products
break;
case BILLING_ERROR:
//TODO – error occurred during initialization / querying sku details
break;
case USER_CANCELED:
//TODO – user pressed back or canceled a dialog
break;
case SERVICE_UNAVAILABLE:
//TODO – network connection is down
break;
case BILLING_UNAVAILABLE:
//TODO – billing API version is not supported for the type requested
break;
case ITEM_UNAVAILABLE:
//TODO – requested product is not available for purchase
break;
case DEVELOPER_ERROR:
//TODO – invalid arguments provided to the API
break;
case ERROR:
//TODO – fatal error during the API action
break;
case ITEM_ALREADY_OWNED:
//TODO – failure to purchase since item is already owned
break;
case ITEM_NOT_OWNED:
//TODO – failure to consume since item is not owned
break;
}
Log.d(“BillingConnector”, “Error type: ” + response.getErrorType() +
” Response code: ” + response.getResponseCode() + ” Message: ” + response.getDebugMessage());
Toast.makeText(getApplicationContext(), “Error: ” + response.getErrorType() , Toast.LENGTH_SHORT).show();
}
});
}
———————
private void notifyList(List<SkuInfo> skuDetails) {
String sku;
String title;
String price;
purchaseItemDisplay.clear();
for (SkuInfo skuInfo : skuDetails) {
sku = skuInfo.getSku();
title = skuInfo.getTitle();
price = skuInfo.getPrice();
// purchaseItemDisplay.add(title + ” : ” + price);
purchaseItemDisplay.add(“Donate” + ” : ” + price);
arrayAdapter.notifyDataSetChanged();
}
}
—————————–
@Override
protected void onDestroy() {
super.onDestroy();
}
End of Code
Read More: Free RDP Trick (100% Working) | How to get Lifetime Free RDP
If Any Issue
If you have any issues, please feel free to comment below us and we will get back to you as soon as possible. Thank you for your cooperation!
If you enjoy our blog, be sure to Subscribe for more. We release new episodes every week, and we love hearing from our listeners. You can also connect with us on social media to stay up-to-date on all things podcast-related. We can’t wait to chat with you more about all things business!