I am trying to know if I can check a non-consumable In-App purchase already own or not. Please help me.
Yes you can check a non-consumable
In-App Purchase
already own or not by using android BillingClient
You have to add following dependency
in your build.gradle
implementation 'com.android.billingclient:billing:1.2'
And use following permission
in your AndroidManifest.xml
file.
<uses-permission android:name="com.android.vending.BILLING" />
You can get in-app purchase
history for the current user by running queryPurchaseHistoryAsync
callback on your mBillingClient
.
Here is an example of how I have implemented it in my project. hope it helps you.
private BillingClient mBillingClient;
private SkuDetailsParams.Builder params;
private SkuDetails skuDetails;
protected void getPurchaseHistory() {
mBillingClient = BillingClient.newBuilder(this).setListener(this).build();
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
if (billingResponseCode == BillingClient.BillingResponse.OK) {
// The billing client is ready. You can query purchases here.
Log.d(TAG, "onBillingSetupFinished ");
mBillingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS,
new PurchaseHistoryResponseListener() {
@Override
public void onPurchaseHistoryResponse(@BillingClient.BillingResponse int responseCode,
List<Purchase> purchasesList) {
if (responseCode == BillingClient.BillingResponse.OK
&& purchasesList != null) {
boolean productFound = false;
for (Purchase purchase : purchasesList) {
// check is user purchased your product or not
if (purchase.getSku().equals("your in-app product id")) {
//success! your user has bought your In-App product. Woohoo!
productFound = true;
// you can check user subscription details by using purchase token
checkSubscription(purchase.getPurchaseToken());
prefs().setPurchaseToken(purchase.getPurchaseToken());
}
}
if (!productFound) {
// user not purchase your product yet.
prefs().setIsSubscribed(false);
Log.d(TAG, "product not found");
}
} else {
prefs().setIsSubscribed(false);
Log.d(TAG, "Purchase history response not ok");
}
}
});
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Log.d(TAG, "onBillingServiceDisconnected ");
}
});
}
Happy Coding
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.