From 028f2cae905b9eb05d2c8d044b2529e49d5cedb9 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 23 May 2021 11:59:32 +0200 Subject: [PATCH] Query SKU details on purchase --- .../eu/faircode/email/ActivityBilling.java | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/app/src/play/java/eu/faircode/email/ActivityBilling.java b/app/src/play/java/eu/faircode/email/ActivityBilling.java index ebab713cbd..1e644cb2e4 100644 --- a/app/src/play/java/eu/faircode/email/ActivityBilling.java +++ b/app/src/play/java/eu/faircode/email/ActivityBilling.java @@ -66,14 +66,12 @@ import java.security.PublicKey; import java.security.Signature; import java.security.spec.X509EncodedKeySpec; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; -import java.util.HashMap; import java.util.List; -import java.util.Map; public class ActivityBilling extends ActivityBase implements PurchasesUpdatedListener, FragmentManager.OnBackStackChangedListener { private BillingClient billingClient = null; - private Map skuDetails = new HashMap<>(); private List listeners = new ArrayList<>(); static final String ACTION_PURCHASE = BuildConfig.APPLICATION_ID + ".ACTION_PURCHASE"; @@ -218,15 +216,34 @@ public class ActivityBilling extends ActivityBase implements PurchasesUpdatedLis private void onPurchase(Intent intent) { if (Helper.isPlayStoreInstall()) { - BillingFlowParams.Builder flowParams = BillingFlowParams.newBuilder(); - if (skuDetails.containsKey(getSkuPro())) { - Log.i("IAB purchase SKU=" + skuDetails.get(getSkuPro())); - flowParams.setSkuDetails(skuDetails.get(getSkuPro())); - } + String sku = getSkuPro(); + Log.i("IAB purchase SKU=" + sku); - BillingResult result = billingClient.launchBillingFlow(this, flowParams.build()); - if (result.getResponseCode() != BillingClient.BillingResponseCode.OK) - reportError(result, "IAB launch billing flow"); + SkuDetailsParams.Builder builder = SkuDetailsParams.newBuilder(); + builder.setSkusList(Arrays.asList(sku)); + builder.setType(BillingClient.SkuType.INAPP); + billingClient.querySkuDetailsAsync(builder.build(), + new SkuDetailsResponseListener() { + @Override + public void onSkuDetailsResponse(BillingResult r, List skuDetailsList) { + if (r.getResponseCode() == BillingClient.BillingResponseCode.OK) { + if (skuDetailsList.size() == 0) + reportError(null, "Unknown SKU=" + sku); + else { + SkuDetails skuDetails = skuDetailsList.get(0); + Log.i("IAB purchase details=" + skuDetails); + + BillingFlowParams.Builder flowParams = BillingFlowParams.newBuilder(); + flowParams.setSkuDetails(skuDetails); + + BillingResult result = billingClient.launchBillingFlow(ActivityBilling.this, flowParams.build()); + if (result.getResponseCode() != BillingClient.BillingResponseCode.OK) + reportError(result, "IAB launch billing flow"); + } + } else + reportError(r, "IAB query SKUs"); + } + }); } else try { Uri uri = Uri.parse(BuildConfig.PRO_FEATURES_URI + @@ -439,7 +456,6 @@ public class ActivityBilling extends ActivityBase implements PurchasesUpdatedLis if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) { for (SkuDetails skuDetail : skuDetailsList) { Log.i("IAB SKU detail=" + skuDetail); - skuDetails.put(skuDetail.getSku(), skuDetail); for (IBillingListener listener : listeners) listener.onSkuDetails(skuDetail.getSku(), skuDetail.getPrice()); }