Version 5, The error is caused because the OldPurchaseToken is invalid or the subscription has expired.
You need to check if the user has an active subscription and return the Purchase to get the OldPurchaseToken using the .queryPurchasesAsync(....) from the billingClient
Here's an example to restore the purchase.
void restorePurchases() {
billingClient = BillingClient.newBuilder(this)
.enablePendingPurchases()
.setListener((billingResult, list) -> {}).build();
final BillingClient finalBillingClient = billingClient;
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingServiceDisconnected() {
}
@Override
public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
finalBillingClient.queryPurchasesAsync(
QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.SUBS).build(), (billingResult1, list) -> {
if (billingResult1.getResponseCode() == BillingClient.BillingResponseCode.OK) {
if (list.size() > 0) {
Log.d("Test1234",list.get(0).getPurchaseToken()); // This is the OldPurchaseToken
prefs.setPremium(1); // set 1 to activate premium feature
showSnackBar(btn_restore_fab, "Successfully restored");
} else {
showSnackBar(btn_restore_fab, "Oops, No purchase found.");
prefs.setPremium(0); // set 0 to de-activate premium feature
}
}
});
}
}
});
}