Search in sources :

Example 6 with BillingResult

use of com.android.billingclient.api.BillingResult in project CodenameOne by codenameone.

the class BillingSupport method requireConnection.

private AsyncResource requireConnection() {
    final AsyncResource out;
    synchronized (this) {
        if (pendingConnection != null) {
            return pendingConnection;
        }
        out = new AsyncResource();
        pendingConnection = out;
    }
    if (!activity.isBillingEnabled()) {
        out.error(new UnsupportedOperationException("Billing is not enabled."));
        return out;
    }
    if (billingClient == null) {
        billingClient = BillingClient.newBuilder(activity).setListener(purchasesUpdatedListener).enablePendingPurchases().build();
    }
    if (billingConnected) {
        out.complete(true);
    } else {
        billingClient.startConnection(new com.android.billingclient.api.BillingClientStateListener() {

            @Override
            public void onBillingSetupFinished(com.android.billingclient.api.BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                    billingConnected = true;
                    synchronized (BillingSupport.this) {
                        pendingConnection = null;
                    }
                    consumeAndAcknowlegePurchases();
                    out.complete(true);
                } else {
                    synchronized (BillingSupport.this) {
                        pendingConnection = null;
                    }
                    System.err.println("Failed to connect to billing service: " + billingResult.getDebugMessage());
                    out.error(new IOException(billingResult.getDebugMessage()));
                }
            }

            @Override
            public void onBillingServiceDisconnected() {
                // Try to restart the connection on the next request to
                // Google Play by calling the startConnection() method.
                billingConnected = false;
            }
        });
    }
    return out;
}
Also used : BillingResult(com.android.billingclient.api.BillingResult) IOException(java.io.IOException) AsyncResource(com.codename1.util.AsyncResource)

Example 7 with BillingResult

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

the class InAppPurchaseHelperImpl method execImpl.

@Override
protected void execImpl(@NonNull final InAppPurchaseTaskType taskType, @NonNull final InAppCommand runnable) {
    billingManager = new BillingManager(ctx, BASE64_ENCODED_PUBLIC_KEY, new BillingManager.BillingUpdatesListener() {

        @Override
        public void onBillingClientSetupFinished() {
            logDebug("Setup finished.");
            BillingManager billingManager = getBillingManager();
            // Have we been disposed of in the meantime? If so, quit.
            if (billingManager == null) {
                stop(true);
                return;
            }
            if (!billingManager.isServiceConnected()) {
                // Oh noes, there was a problem.
                // complain("Problem setting up in-app billing: " + result);
                notifyError(taskType, billingManager.getBillingClientResponseMessage());
                stop(true);
                return;
            }
            runnable.run(InAppPurchaseHelperImpl.this);
        }

        @Override
        public void onConsumeFinished(String token, BillingResult billingResult) {
        }

        @Override
        public void onPurchasesUpdated(final List<Purchase> purchases) {
            BillingManager billingManager = getBillingManager();
            // Have we been disposed of in the meantime? If so, quit.
            if (billingManager == null) {
                stop(true);
                return;
            }
            if (activeTask == InAppPurchaseTaskType.REQUEST_INVENTORY) {
                List<String> skuInApps = new ArrayList<>();
                for (InAppPurchase purchase : getInAppPurchases().getAllInAppPurchases(false)) {
                    skuInApps.add(purchase.getSku());
                }
                for (Purchase p : purchases) {
                    skuInApps.add(p.getSku());
                }
                billingManager.querySkuDetailsAsync(BillingClient.SkuType.INAPP, skuInApps, new SkuDetailsResponseListener() {

                    @Override
                    public void onSkuDetailsResponse(@NonNull BillingResult billingResult, final List<SkuDetails> skuDetailsListInApps) {
                        // Is it a failure?
                        if (billingResult.getResponseCode() != BillingClient.BillingResponseCode.OK) {
                            logError("Failed to query inapps sku details: " + billingResult.getResponseCode());
                            notifyError(InAppPurchaseTaskType.REQUEST_INVENTORY, billingResult.getDebugMessage());
                            stop(true);
                            return;
                        }
                        List<String> skuSubscriptions = new ArrayList<>();
                        for (InAppSubscription subscription : getInAppPurchases().getAllInAppSubscriptions()) {
                            skuSubscriptions.add(subscription.getSku());
                        }
                        for (Purchase p : purchases) {
                            skuSubscriptions.add(p.getSku());
                        }
                        skuSubscriptions.addAll(subscriptionStateMap.keySet());
                        BillingManager billingManager = getBillingManager();
                        // Have we been disposed of in the meantime? If so, quit.
                        if (billingManager == null) {
                            stop(true);
                            return;
                        }
                        billingManager.querySkuDetailsAsync(BillingClient.SkuType.SUBS, skuSubscriptions, new SkuDetailsResponseListener() {

                            @Override
                            public void onSkuDetailsResponse(@NonNull BillingResult billingResult, final List<SkuDetails> skuDetailsListSubscriptions) {
                                // Is it a failure?
                                if (billingResult.getResponseCode() != BillingClient.BillingResponseCode.OK) {
                                    logError("Failed to query subscriptipons sku details: " + billingResult.getResponseCode());
                                    notifyError(InAppPurchaseTaskType.REQUEST_INVENTORY, billingResult.getDebugMessage());
                                    stop(true);
                                    return;
                                }
                                List<SkuDetails> skuDetailsList = new ArrayList<>(skuDetailsListInApps);
                                skuDetailsList.addAll(skuDetailsListSubscriptions);
                                InAppPurchaseHelperImpl.this.skuDetailsList = skuDetailsList;
                                getSkuDetailsResponseListener(runnable.userRequested()).onSkuDetailsResponse(billingResult, skuDetailsList);
                            }
                        });
                    }
                });
            }
            for (Purchase purchase : purchases) {
                InAppSubscription subscription = getSubscriptions().getSubscriptionBySku(purchase.getSku());
                if (!purchase.isAcknowledged() || (subscription != null && !subscription.isPurchased())) {
                    onPurchaseFinished(purchase);
                }
            }
        }

        @Override
        public void onPurchaseCanceled() {
            stop(true);
        }
    });
}
Also used : SkuDetails(com.android.billingclient.api.SkuDetails) InAppPurchase(net.osmand.plus.inapp.InAppPurchases.InAppPurchase) Purchase(com.android.billingclient.api.Purchase) InAppPurchase(net.osmand.plus.inapp.InAppPurchases.InAppPurchase) BillingManager(net.osmand.plus.inapp.util.BillingManager) ArrayList(java.util.ArrayList) InAppSubscription(net.osmand.plus.inapp.InAppPurchases.InAppSubscription) SkuDetailsResponseListener(com.android.billingclient.api.SkuDetailsResponseListener) NonNull(androidx.annotation.NonNull) BillingResult(com.android.billingclient.api.BillingResult) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with BillingResult

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

the class BillingManager method consumeAsync.

public void consumeAsync(final ConsumeParams consumeParams) {
    // If we've already scheduled to consume this token - no action is needed (this could happen
    // if you received the token when querying purchases inside onReceive() and later from
    // onActivityResult()
    final String purchaseToken = consumeParams.getPurchaseToken();
    if (mTokensToBeConsumed == null) {
        mTokensToBeConsumed = new HashSet<>();
    } else if (mTokensToBeConsumed.contains(purchaseToken)) {
        LOG.info("Token was already scheduled to be consumed - skipping...");
        return;
    }
    mTokensToBeConsumed.add(purchaseToken);
    // Generating Consume Response listener
    final ConsumeResponseListener onConsumeListener = new ConsumeResponseListener() {

        @Override
        public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
            // If billing service was disconnected, we try to reconnect 1 time
            // (feel free to introduce your retry policy here).
            mBillingUpdatesListener.onConsumeFinished(purchaseToken, billingResult);
        }
    };
    // Creating a runnable from the request to use it inside our connection retry policy below
    Runnable consumeRequest = new Runnable() {

        @Override
        public void run() {
            // Consume the purchase async
            mBillingClient.consumeAsync(consumeParams, onConsumeListener);
        }
    };
    executeServiceRequest(consumeRequest);
}
Also used : ConsumeResponseListener(com.android.billingclient.api.ConsumeResponseListener) BillingResult(com.android.billingclient.api.BillingResult)

Example 9 with BillingResult

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

the class BillingManager method consumePurchase.

/*
    public void startPurchaseFlow(final String skuId, final String billingType) {
        // Specify a runnable to start when connection to Billing client is established
        Runnable executeOnConnectedService = new Runnable() {
            @Override
            public void run() {
                BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
                        .setType(billingType)
                        .setSku(skuId)
                        .build();
                int responseCode = mBillingClient.launchBillingFlow(mActivity, billingFlowParams);
                PPApplication.logE(TAG, "startPurchaseFlow responseCode="+responseCode);
                getFragment().displayAnErrorIfNeeded(responseCode);
            }
        };

        // If Billing client was disconnected, we retry 1 time and if success, execute the query
        startServiceConnectionIfNeeded(executeOnConnectedService);
    }
    */
private void consumePurchase(final Purchase purchase) {
    // Specify a runnable to start when connection to Billing client is established
    Runnable executeOnConnectedService = new Runnable() {

        @Override
        public void run() {
            ConsumeParams consumeParams = ConsumeParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
            mBillingClient.consumeAsync(consumeParams, new ConsumeResponseListener() {

                @Override
                public void onConsumeResponse(@NonNull BillingResult billingResult, @NonNull String purchaseToken) {
                // PPApplication.logE(TAG, "onConsumeResponse() response: " + billingResult.getResponseCode());
                /*if (responseCode == BillingClient.BillingResponse.OK) {
                                    // Handle the success of the consume operation.
                                    // For example, increase the number of player's coins,
                                    // that provide temporary benefits
                                }*/
                }
            });
        }
    };
    // If Billing client was disconnected, we retry 1 time and if success, execute the query
    startServiceConnectionIfNeeded(executeOnConnectedService);
}
Also used : ConsumeParams(com.android.billingclient.api.ConsumeParams) ConsumeResponseListener(com.android.billingclient.api.ConsumeResponseListener) BillingResult(com.android.billingclient.api.BillingResult)

Example 10 with BillingResult

use of com.android.billingclient.api.BillingResult in project apcupsd-monitor by norkator.

the class MainMenu method inAppPurchase.

public void inAppPurchase(final String IAP_ITEM_SKU) {
    if (mBillingClient.isReady()) {
        List<String> skuList = new ArrayList<>();
        skuList.add(IAP_ITEM_SKU);
        SkuDetailsParams skuDetailsParams = SkuDetailsParams.newBuilder().setSkusList(skuList).setType(BillingClient.SkuType.INAPP).build();
        mBillingClient.querySkuDetailsAsync(skuDetailsParams, new SkuDetailsResponseListener() {

            @Override
            public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {
                try {
                    BillingFlowParams flowParams = BillingFlowParams.newBuilder().setSkuDetails(skuDetailsList.get(0)).build();
                    mBillingClient.launchBillingFlow(MainMenu.this, flowParams);
                } catch (IndexOutOfBoundsException e) {
                    genericErrorDialog(getString(R.string.error), e.toString());
                }
            }
        });
    } else {
        genericErrorDialog("Billing service", "Billing service is not initialized yet. Please try again later.");
        initInAppBilling();
    }
}
Also used : SkuDetails(com.android.billingclient.api.SkuDetails) SkuDetailsResponseListener(com.android.billingclient.api.SkuDetailsResponseListener) ArrayList(java.util.ArrayList) SkuDetailsParams(com.android.billingclient.api.SkuDetailsParams) BillingResult(com.android.billingclient.api.BillingResult) BillingFlowParams(com.android.billingclient.api.BillingFlowParams)

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