Search in sources :

Example 21 with BillingResult

use of com.android.billingclient.api.BillingResult in project AmazeFileManager by TeamAmaze.

the class Billing method onPurchasesUpdated.

@Override
public void onPurchasesUpdated(BillingResult response, @Nullable List<Purchase> purchases) {
    if (response.getResponseCode() == BillingClient.BillingResponseCode.OK && purchases != null) {
        for (Purchase purchase : purchases) {
            ConsumeResponseListener listener = (responseCode1, purchaseToken) -> {
                // we consume the purchase, so that user can perform purchase again
                Toast.makeText(activity, R.string.donation_thanks, Toast.LENGTH_LONG).show();
            };
            ConsumeParams consumeParams = ConsumeParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
            billingClient.consumeAsync(consumeParams, listener);
        }
    }
}
Also used : BillingClientStateListener(com.android.billingclient.api.BillingClientStateListener) NonNull(androidx.annotation.NonNull) ArrayList(java.util.ArrayList) BuildConfig(com.amaze.filemanager.BuildConfig) BillingResult(com.android.billingclient.api.BillingResult) PurchasesUpdatedListener(com.android.billingclient.api.PurchasesUpdatedListener) Toast(android.widget.Toast) ConsumeParams(com.android.billingclient.api.ConsumeParams) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) DonationViewHolder(com.amaze.filemanager.adapters.holders.DonationViewHolder) Log(android.util.Log) LayoutInflater(android.view.LayoutInflater) SkuDetails(com.android.billingclient.api.SkuDetails) ConsumeResponseListener(com.android.billingclient.api.ConsumeResponseListener) SkuDetailsParams(com.android.billingclient.api.SkuDetailsParams) ViewGroup(android.view.ViewGroup) List(java.util.List) Purchase(com.android.billingclient.api.Purchase) Nullable(androidx.annotation.Nullable) BillingClient(com.android.billingclient.api.BillingClient) AdapterDonationBinding(com.amaze.filemanager.databinding.AdapterDonationBinding) R(com.amaze.filemanager.R) BillingFlowParams(com.android.billingclient.api.BillingFlowParams) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) BasicActivity(com.amaze.filemanager.ui.activities.superclasses.BasicActivity) AppConfig(com.amaze.filemanager.application.AppConfig) Purchase(com.android.billingclient.api.Purchase) ConsumeResponseListener(com.android.billingclient.api.ConsumeResponseListener) ConsumeParams(com.android.billingclient.api.ConsumeParams)

Example 22 with BillingResult

use of com.android.billingclient.api.BillingResult in project Osmand by osmandapp.

the class InAppPurchaseHelperImpl method getSkuDetailsResponseListener.

// Listener that's called when we finish querying the items and subscriptions we own
private SkuDetailsResponseListener getSkuDetailsResponseListener(boolean userRequested) {
    return new SkuDetailsResponseListener() {

        @NonNull
        private List<String> getAllOwnedSubscriptionSkus() {
            List<String> result = new ArrayList<>();
            BillingManager billingManager = getBillingManager();
            if (billingManager != null) {
                for (Purchase p : billingManager.getPurchases()) {
                    if (getInAppPurchases().getInAppSubscriptionBySku(p.getSku()) != null) {
                        result.add(p.getSku());
                    }
                }
            }
            for (Entry<String, SubscriptionStateHolder> entry : subscriptionStateMap.entrySet()) {
                SubscriptionState state = entry.getValue().state;
                if (state == SubscriptionState.PAUSED || state == SubscriptionState.ON_HOLD) {
                    String sku = entry.getKey();
                    if (!result.contains(sku)) {
                        result.add(sku);
                    }
                }
            }
            return result;
        }

        @Override
        public void onSkuDetailsResponse(@NonNull BillingResult billingResult, List<SkuDetails> skuDetailsList) {
            logDebug("Query sku details finished.");
            // Have we been disposed of in the meantime? If so, quit.
            if (getBillingManager() == null) {
                stop(true);
                return;
            }
            // Is it a failure?
            if (billingResult.getResponseCode() != BillingClient.BillingResponseCode.OK) {
                logError("Failed to query inventory: " + billingResult.getResponseCode());
                notifyError(InAppPurchaseTaskType.REQUEST_INVENTORY, billingResult.getDebugMessage());
                stop(true);
                return;
            }
            logDebug("Query sku details was successful.");
            /*
				 * Check for items we own. Notice that for each purchase, we check
				 * the developer payload to see if it's correct! See
				 * verifyDeveloperPayload().
				 */
            List<String> allOwnedSubscriptionSkus = getAllOwnedSubscriptionSkus();
            for (InAppSubscription s : getSubscriptions().getAllSubscriptions()) {
                if (hasDetails(s.getSku())) {
                    Purchase purchase = getPurchase(s.getSku());
                    SkuDetails liveUpdatesDetails = getSkuDetails(s.getSku());
                    if (liveUpdatesDetails != null) {
                        fetchInAppPurchase(s, liveUpdatesDetails, purchase);
                    }
                    allOwnedSubscriptionSkus.remove(s.getSku());
                }
            }
            for (String sku : allOwnedSubscriptionSkus) {
                Purchase purchase = getPurchase(sku);
                SkuDetails liveUpdatesDetails = getSkuDetails(sku);
                if (liveUpdatesDetails != null) {
                    InAppSubscription s = getSubscriptions().upgradeSubscription(sku);
                    if (s == null) {
                        s = new InAppPurchaseLiveUpdatesOldSubscription(liveUpdatesDetails);
                    }
                    fetchInAppPurchase(s, liveUpdatesDetails, purchase);
                }
            }
            InAppPurchase fullVersion = getFullVersion();
            if (hasDetails(fullVersion.getSku())) {
                Purchase purchase = getPurchase(fullVersion.getSku());
                SkuDetails fullPriceDetails = getSkuDetails(fullVersion.getSku());
                if (fullPriceDetails != null) {
                    fetchInAppPurchase(fullVersion, fullPriceDetails, purchase);
                }
            }
            InAppPurchase depthContours = getDepthContours();
            if (hasDetails(depthContours.getSku())) {
                Purchase purchase = getPurchase(depthContours.getSku());
                SkuDetails depthContoursDetails = getSkuDetails(depthContours.getSku());
                if (depthContoursDetails != null) {
                    fetchInAppPurchase(depthContours, depthContoursDetails, purchase);
                }
            }
            InAppPurchase contourLines = getContourLines();
            if (hasDetails(contourLines.getSku())) {
                Purchase purchase = getPurchase(contourLines.getSku());
                SkuDetails contourLinesDetails = getSkuDetails(contourLines.getSku());
                if (contourLinesDetails != null) {
                    fetchInAppPurchase(contourLines, contourLinesDetails, purchase);
                }
            }
            Purchase fullVersionPurchase = getPurchase(fullVersion.getSku());
            boolean fullVersionPurchased = fullVersionPurchase != null;
            if (fullVersionPurchased) {
                ctx.getSettings().FULL_VERSION_PURCHASED.set(true);
            }
            Purchase depthContoursPurchase = getPurchase(depthContours.getSku());
            boolean depthContoursPurchased = depthContoursPurchase != null;
            if (depthContoursPurchased) {
                ctx.getSettings().DEPTH_CONTOURS_PURCHASED.set(true);
            }
            // Do we have the live updates?
            boolean subscribedToLiveUpdates = false;
            boolean subscribedToOsmAndPro = false;
            boolean subscribedToMaps = false;
            List<Purchase> subscriptionPurchases = new ArrayList<>();
            for (InAppSubscription s : getSubscriptions().getAllSubscriptions()) {
                Purchase purchase = getPurchase(s.getSku());
                if (purchase != null || s.getState().isActive()) {
                    if (purchase != null) {
                        subscriptionPurchases.add(purchase);
                    }
                    if (!subscribedToLiveUpdates && purchases.isLiveUpdatesSubscription(s)) {
                        subscribedToLiveUpdates = true;
                    }
                    if (!subscribedToOsmAndPro && purchases.isOsmAndProSubscription(s)) {
                        subscribedToOsmAndPro = true;
                    }
                    if (!subscribedToMaps && purchases.isMapsSubscription(s)) {
                        subscribedToMaps = true;
                    }
                }
            }
            if (!subscribedToLiveUpdates && ctx.getSettings().LIVE_UPDATES_PURCHASED.get()) {
                ctx.getSettings().LIVE_UPDATES_PURCHASED.set(false);
            } else if (subscribedToLiveUpdates) {
                ctx.getSettings().LIVE_UPDATES_PURCHASED.set(true);
            }
            if (!subscribedToOsmAndPro && ctx.getSettings().OSMAND_PRO_PURCHASED.get()) {
                ctx.getSettings().OSMAND_PRO_PURCHASED.set(false);
            } else if (subscribedToOsmAndPro) {
                ctx.getSettings().OSMAND_PRO_PURCHASED.set(true);
            }
            if (!subscribedToMaps && ctx.getSettings().OSMAND_MAPS_PURCHASED.get()) {
                ctx.getSettings().OSMAND_MAPS_PURCHASED.set(false);
            } else if (subscribedToMaps) {
                ctx.getSettings().OSMAND_MAPS_PURCHASED.set(true);
            }
            if (!subscribedToLiveUpdates && !subscribedToOsmAndPro && !subscribedToMaps) {
                onSubscriptionExpired();
            }
            lastValidationCheckTime = System.currentTimeMillis();
            logDebug("User " + (subscribedToLiveUpdates ? "HAS" : "DOES NOT HAVE") + " live updates purchased.");
            logDebug("User " + (subscribedToOsmAndPro ? "HAS" : "DOES NOT HAVE") + " OsmAnd Pro purchased.");
            logDebug("User " + (subscribedToMaps ? "HAS" : "DOES NOT HAVE") + " Maps purchased.");
            OsmandSettings settings = ctx.getSettings();
            settings.INAPPS_READ.set(true);
            List<Purchase> tokensToSend = new ArrayList<>();
            if (subscriptionPurchases.size() > 0) {
                List<String> tokensSent = Arrays.asList(settings.BILLING_PURCHASE_TOKENS_SENT.get().split(";"));
                for (Purchase purchase : subscriptionPurchases) {
                    if (needRestoreUserInfo()) {
                        restoreUserInfo(purchase);
                    }
                    if (!tokensSent.contains(purchase.getSku())) {
                        tokensToSend.add(purchase);
                    }
                }
            }
            List<PurchaseInfo> purchaseInfoList = new ArrayList<>();
            for (Purchase purchase : tokensToSend) {
                purchaseInfoList.add(getPurchaseInfo(purchase));
            }
            onSkuDetailsResponseDone(purchaseInfoList, userRequested);
        }

        private void onSubscriptionExpired() {
            if (!isDepthContoursPurchased(ctx)) {
                ctx.getSettings().getCustomRenderBooleanProperty("depthContours").set(false);
            }
        }
    };
}
Also used : SkuDetails(com.android.billingclient.api.SkuDetails) InAppPurchase(net.osmand.plus.inapp.InAppPurchases.InAppPurchase) Purchase(com.android.billingclient.api.Purchase) InAppPurchaseLiveUpdatesOldSubscription(net.osmand.plus.inapp.InAppPurchasesImpl.InAppPurchaseLiveUpdatesOldSubscription) InAppPurchase(net.osmand.plus.inapp.InAppPurchases.InAppPurchase) ArrayList(java.util.ArrayList) BillingManager(net.osmand.plus.inapp.util.BillingManager) InAppSubscription(net.osmand.plus.inapp.InAppPurchases.InAppSubscription) OsmandSettings(net.osmand.plus.settings.backend.OsmandSettings) SkuDetailsResponseListener(com.android.billingclient.api.SkuDetailsResponseListener) SubscriptionState(net.osmand.plus.inapp.InAppPurchases.InAppSubscription.SubscriptionState) PurchaseInfo(net.osmand.plus.inapp.InAppPurchases.PurchaseInfo) NonNull(androidx.annotation.NonNull) BillingResult(com.android.billingclient.api.BillingResult) ArrayList(java.util.ArrayList) List(java.util.List)

Example 23 with BillingResult

use of com.android.billingclient.api.BillingResult in project Osmand by osmandapp.

the class BillingManager method handlePurchase.

/**
 * Handles the purchase
 * <p>Note: Notice that for each purchase, we check if signature is valid on the client.
 * It's recommended to move this check into your backend.
 * See {@link Security#verifyPurchase(String, String, String)}
 * </p>
 *
 * @param purchase Purchase to be handled
 */
private void handlePurchase(final Purchase purchase) {
    if (!verifyValidSignature(purchase.getOriginalJson(), purchase.getSignature())) {
        LOG.info("Got a purchase: " + purchase + ", but signature is bad. Skipping...");
        return;
    }
    if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
        // Acknowledge the purchase if it hasn't already been acknowledged.
        if (!purchase.isAcknowledged()) {
            AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
            mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {

                @Override
                public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
                    if (billingResult.getResponseCode() != BillingResponseCode.OK) {
                        LOG.info("Acknowledge a purchase: " + purchase + " failed (" + billingResult.getResponseCode() + "). " + billingResult.getDebugMessage());
                    }
                }
            });
        }
    } else if (purchase.getPurchaseState() == Purchase.PurchaseState.PENDING) {
        LOG.info("Got a purchase: " + purchase + ", but purchase state is pending. Skipping...");
        return;
    } else {
        LOG.info("Got a purchase: " + purchase + ", but purchase state is " + purchase.getPurchaseState() + ". Skipping...");
        return;
    }
    LOG.debug("Got a verified purchase: " + purchase);
    mPurchases.add(purchase);
}
Also used : AcknowledgePurchaseResponseListener(com.android.billingclient.api.AcknowledgePurchaseResponseListener) AcknowledgePurchaseParams(com.android.billingclient.api.AcknowledgePurchaseParams) BillingResult(com.android.billingclient.api.BillingResult)

Example 24 with BillingResult

use of com.android.billingclient.api.BillingResult in project Osmand by osmandapp.

the class BillingManager method querySkuDetailsAsync.

public void querySkuDetailsAsync(@SkuType final String itemType, final List<String> skuList, final SkuDetailsResponseListener listener) {
    // Creating a runnable from the request to use it inside our connection retry policy below
    Runnable queryRequest = new Runnable() {

        @Override
        public void run() {
            // Query the purchase async
            SkuDetailsParams params = SkuDetailsParams.newBuilder().setSkusList(skuList).setType(itemType).build();
            mBillingClient.querySkuDetailsAsync(params, new SkuDetailsResponseListener() {

                @Override
                public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {
                    listener.onSkuDetailsResponse(billingResult, skuDetailsList);
                }
            });
        }
    };
    executeServiceRequest(queryRequest);
}
Also used : SkuDetails(com.android.billingclient.api.SkuDetails) SkuDetailsResponseListener(com.android.billingclient.api.SkuDetailsResponseListener) SkuDetailsParams(com.android.billingclient.api.SkuDetailsParams) BillingResult(com.android.billingclient.api.BillingResult)

Example 25 with BillingResult

use of com.android.billingclient.api.BillingResult in project PhoneProfiles by henrichg.

the class DonationFragment method updateGUIAfterBillingConnected.

public void updateGUIAfterBillingConnected() {
    // Start querying for SKUs
    // PPApplication.logE(TAG, "handleManagerAndUiReady");
    final List<String> inAppSkus = mBillingProvider.getBillingManager().getSkus(/*!mDebug, */
    BillingClient.SkuType.INAPP);
    mBillingProvider.getBillingManager().querySkuDetailsAsync(BillingClient.SkuType.INAPP, inAppSkus, new SkuDetailsResponseListener() {

        @Override
        public void onSkuDetailsResponse(@NonNull BillingResult billingResult, List<SkuDetails> skuDetailsList) {
            int responseCode = billingResult.getResponseCode();
            // PPApplication.logE(TAG, "onSkuDetailsResponse responseCode="+responseCode);
            String[] prices = new String[] { "1 €", "2 €", "3 €", "5 €", "8 €", "13 €", "20 €" };
            // PPApplication.logE(TAG, "onSkuDetailsResponse skuDetailsList="+skuDetailsList.size());
            if (responseCode == BillingClient.BillingResponseCode.OK && skuDetailsList != null) {
                if (skuDetailsList.size() > 0) {
                    SKU_DETAILS = new ArrayList<>();
                    for (int i = 0; i < inAppSkus.size(); i++) {
                        for (int j = 0; j < skuDetailsList.size(); j++) {
                            if (skuDetailsList.get(j).getSku().equals(inAppSkus.get(i))) {
                                // PPApplication.logE(TAG, "Found sku: " + skuDetailsList.get(j));
                                SKU_DETAILS.add(skuDetailsList.get(j));
                                prices[i] = skuDetailsList.get(j).getPrice();
                                break;
                            }
                        }
                    }
                    // update the UI
                    displayAnErrorIfNeeded(BillingClient.BillingResponseCode.OK);
                    if (getActivity() != null) {
                        ArrayAdapter<CharSequence> adapter;
                        adapter = new ArrayAdapter<CharSequence>(getActivity(), R.layout.donation_spinner, prices);
                        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        mGoogleSpinner.setAdapter(adapter);
                        btGoogle.setEnabled(true);
                    }
                } else {
                    displayAnErrorIfNeeded(BillingClient.BillingResponseCode.FEATURE_NOT_SUPPORTED);
                }
            }
        }
    });
}
Also used : SkuDetails(com.android.billingclient.api.SkuDetails) SkuDetailsResponseListener(com.android.billingclient.api.SkuDetailsResponseListener) ArrayList(java.util.ArrayList) BillingResult(com.android.billingclient.api.BillingResult) ArrayAdapter(android.widget.ArrayAdapter)

Aggregations

BillingResult (com.android.billingclient.api.BillingResult)28 SkuDetails (com.android.billingclient.api.SkuDetails)16 SkuDetailsResponseListener (com.android.billingclient.api.SkuDetailsResponseListener)15 ArrayList (java.util.ArrayList)14 BillingClientStateListener (com.android.billingclient.api.BillingClientStateListener)10 ConsumeResponseListener (com.android.billingclient.api.ConsumeResponseListener)10 SkuDetailsParams (com.android.billingclient.api.SkuDetailsParams)10 List (java.util.List)10 NonNull (androidx.annotation.NonNull)9 ConsumeParams (com.android.billingclient.api.ConsumeParams)9 Purchase (com.android.billingclient.api.Purchase)9 AcknowledgePurchaseResponseListener (com.android.billingclient.api.AcknowledgePurchaseResponseListener)8 Nullable (androidx.annotation.Nullable)7 AcknowledgePurchaseParams (com.android.billingclient.api.AcknowledgePurchaseParams)7 BillingClient (com.android.billingclient.api.BillingClient)7 BillingFlowParams (com.android.billingclient.api.BillingFlowParams)7 PurchasesUpdatedListener (com.android.billingclient.api.PurchasesUpdatedListener)7 Activity (android.app.Activity)5 Context (android.content.Context)5 StringRes (androidx.annotation.StringRes)5