I have implement Android in app purchase in my app. There is a problem in purchase. Some user is getting reward without purchase. So, how can I understand if user successfully purchased. If purchase is successfully purchased he will get reward.
Can anyone check if my implement is OK or not? And what I need to add more to understand if user purchase is successful or not. Please help me to solve the issue.
mBillingClient = BillingClient.newBuilder(this).setListener(this).build();
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(int responseCode) {
List skuList = new ArrayList<>();
skuList.add(ITEM_S);
skuList.add(ITEM_G);
skuList.add(ITEM_D);
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
mBillingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(int responseCode, List skuDetailsList) {
// Process the result.
if (responseCode != BillingClient.BillingResponse.OK && skuDetailsList == null) {
}
});
Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.INAPP);
for (Purchase purchase : purchasesResult.getPurchasesList()) {
consumePurchase(purchase);
}
}
@Override
public void onBillingServiceDisconnected() {
}
});
@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
if (responseCode == BillingClient.BillingResponse.OK
&& purchases != null) {
for (Purchase purchase : purchases) {
consumePurchase(purchase);
}
}
}
Do not reward your user while you are consuming existing purchase.
Remove handlePurchase(purchase);
method call from consumePurchase
method.
Instead reward your user if purchase is success. You will get purchase related callback in onPurchasesUpdated
method.
Your implementation will look like this:
@Override
public void onPurchasesUpdated(
BillingResult billingResult,
List<Purchase> purchases) {
if (responseCode == BillingClient.BillingResponse.OK
&& purchases != null) {
for (Purchase purchase : purchases) {
handlePurchase(purchase);
consumePurchase(purchase);
}
}
}
B.N. This is not a solid implementation, this is just a sudo code.