Search in sources :

Example 16 with BillingResult

use of com.android.billingclient.api.BillingResult in project Android-InAppBilling by LiteKite.

the class BillingManager method querySkuDetailsAsync.

/**
 * Queries SKU Details from Google Play Remote Server of SKU Types (InApp and Subscription).
 *
 * @param skuResultLMap contains SKU ID and Price Details returned by the sku details query.
 * @param params contains list of SKU IDs and SKU Type (InApp or Subscription).
 * @param billingType InApp or Subscription.
 * @param executeWhenFinished contains query for InApp SKU Details that will be run after
 */
private void querySkuDetailsAsync(Map<String, SkuDetails> skuResultLMap, SkuDetailsParams.Builder params, @SkuType String billingType, Runnable executeWhenFinished) {
    final SkuDetailsResponseListener listener = (billingResult, skuDetailsList) -> {
        // Process the result.
        if (billingResult.getResponseCode() != BillingResponseCode.OK) {
            MonetizeApp.printLog(TAG, "Unsuccessful query for type: " + billingType + ". Error code: " + billingResult.getResponseCode());
        } else if (skuDetailsList != null && skuDetailsList.size() > 0) {
            for (SkuDetails skuDetails : skuDetailsList) {
                skuResultLMap.put(skuDetails.getSku(), skuDetails);
            }
        }
        if (executeWhenFinished != null) {
            executeWhenFinished.run();
            return;
        }
        if (skuResultLMap.size() == 0) {
            MonetizeApp.printLog(TAG, "sku error: " + context.getString(R.string.err_no_sku));
        } else {
            MonetizeApp.printLog(TAG, "storing sku list locally");
            storeSkuDetailsLocally(skuResultLMap);
        }
    };
    // Creating a runnable from the request to use it inside our connection retry policy below
    executeServiceRequest(() -> myBillingClient.querySkuDetailsAsync(params.build(), listener));
}
Also used : NetworkManager(com.litekite.monetize.network.NetworkManager) Context(android.content.Context) WorkExecutor(com.litekite.monetize.worker.WorkExecutor) NonNull(androidx.annotation.NonNull) BillingClientStateListener(com.android.billingclient.api.BillingClientStateListener) SkuDetailsResponseListener(com.android.billingclient.api.SkuDetailsResponseListener) AppDatabase(com.litekite.monetize.room.database.AppDatabase) PurchaseHistoryRecord(com.android.billingclient.api.PurchaseHistoryRecord) MonetizeApp(com.litekite.monetize.app.MonetizeApp) HashMap(java.util.HashMap) CallbackProvider(com.litekite.monetize.base.CallbackProvider) Singleton(javax.inject.Singleton) SkuType(com.android.billingclient.api.BillingClient.SkuType) BillingSkuDetails(com.litekite.monetize.room.entity.BillingSkuDetails) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) R(com.litekite.monetize.R) BillingResult(com.android.billingclient.api.BillingResult) PurchasesUpdatedListener(com.android.billingclient.api.PurchasesUpdatedListener) Map(java.util.Map) ConsumeParams(com.android.billingclient.api.ConsumeParams) PurchasesResponseListener(com.android.billingclient.api.PurchasesResponseListener) AcknowledgePurchaseParams(com.android.billingclient.api.AcknowledgePurchaseParams) SkuDetails(com.android.billingclient.api.SkuDetails) BillingResponseCode(com.android.billingclient.api.BillingClient.BillingResponseCode) ConsumeResponseListener(com.android.billingclient.api.ConsumeResponseListener) Set(java.util.Set) SkuDetailsParams(com.android.billingclient.api.SkuDetailsParams) PurchaseHistoryResponseListener(com.android.billingclient.api.PurchaseHistoryResponseListener) StringRes(androidx.annotation.StringRes) FeatureType(com.android.billingclient.api.BillingClient.FeatureType) Purchase(com.android.billingclient.api.Purchase) List(java.util.List) Nullable(androidx.annotation.Nullable) BillingClient(com.android.billingclient.api.BillingClient) AcknowledgePurchaseResponseListener(com.android.billingclient.api.AcknowledgePurchaseResponseListener) BillingPurchaseDetails(com.litekite.monetize.room.entity.BillingPurchaseDetails) BillingFlowParams(com.android.billingclient.api.BillingFlowParams) Activity(android.app.Activity) BillingSkuDetails(com.litekite.monetize.room.entity.BillingSkuDetails) SkuDetails(com.android.billingclient.api.SkuDetails) SkuDetailsResponseListener(com.android.billingclient.api.SkuDetailsResponseListener)

Example 17 with BillingResult

use of com.android.billingclient.api.BillingResult in project Android-InAppBilling by LiteKite.

the class BillingManager method acknowledgeNonConsumablePurchasesAsync.

/**
 * If you do not acknowledge a purchase, the Google Play Store will provide a refund to the
 * users within a few days of the transaction. Therefore you have to implement
 * [BillingClient.acknowledgePurchaseAsync] inside your app.
 *
 * @param purchase list of Purchase Details returned from the queries.
 */
private void acknowledgeNonConsumablePurchasesAsync(Purchase purchase) {
    final AcknowledgePurchaseParams params = AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
    final AcknowledgePurchaseResponseListener listener = billingResult -> {
        if (billingResult.getResponseCode() == BillingResponseCode.OK) {
            MonetizeApp.printLog(TAG, "onAcknowledgePurchaseResponse: " + BillingResponseCode.OK);
        } else {
            MonetizeApp.printLog(TAG, "onAcknowledgePurchaseResponse: " + billingResult.getDebugMessage());
        }
    };
    executeServiceRequest(() -> myBillingClient.acknowledgePurchase(params, listener));
}
Also used : NetworkManager(com.litekite.monetize.network.NetworkManager) Context(android.content.Context) WorkExecutor(com.litekite.monetize.worker.WorkExecutor) NonNull(androidx.annotation.NonNull) BillingClientStateListener(com.android.billingclient.api.BillingClientStateListener) SkuDetailsResponseListener(com.android.billingclient.api.SkuDetailsResponseListener) AppDatabase(com.litekite.monetize.room.database.AppDatabase) PurchaseHistoryRecord(com.android.billingclient.api.PurchaseHistoryRecord) MonetizeApp(com.litekite.monetize.app.MonetizeApp) HashMap(java.util.HashMap) CallbackProvider(com.litekite.monetize.base.CallbackProvider) Singleton(javax.inject.Singleton) SkuType(com.android.billingclient.api.BillingClient.SkuType) BillingSkuDetails(com.litekite.monetize.room.entity.BillingSkuDetails) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) R(com.litekite.monetize.R) BillingResult(com.android.billingclient.api.BillingResult) PurchasesUpdatedListener(com.android.billingclient.api.PurchasesUpdatedListener) Map(java.util.Map) ConsumeParams(com.android.billingclient.api.ConsumeParams) PurchasesResponseListener(com.android.billingclient.api.PurchasesResponseListener) AcknowledgePurchaseParams(com.android.billingclient.api.AcknowledgePurchaseParams) SkuDetails(com.android.billingclient.api.SkuDetails) BillingResponseCode(com.android.billingclient.api.BillingClient.BillingResponseCode) ConsumeResponseListener(com.android.billingclient.api.ConsumeResponseListener) Set(java.util.Set) SkuDetailsParams(com.android.billingclient.api.SkuDetailsParams) PurchaseHistoryResponseListener(com.android.billingclient.api.PurchaseHistoryResponseListener) StringRes(androidx.annotation.StringRes) FeatureType(com.android.billingclient.api.BillingClient.FeatureType) Purchase(com.android.billingclient.api.Purchase) List(java.util.List) Nullable(androidx.annotation.Nullable) BillingClient(com.android.billingclient.api.BillingClient) AcknowledgePurchaseResponseListener(com.android.billingclient.api.AcknowledgePurchaseResponseListener) BillingPurchaseDetails(com.litekite.monetize.room.entity.BillingPurchaseDetails) BillingFlowParams(com.android.billingclient.api.BillingFlowParams) Activity(android.app.Activity) AcknowledgePurchaseResponseListener(com.android.billingclient.api.AcknowledgePurchaseResponseListener) AcknowledgePurchaseParams(com.android.billingclient.api.AcknowledgePurchaseParams)

Example 18 with BillingResult

use of com.android.billingclient.api.BillingResult in project Android-InAppBilling by LiteKite.

the class BillingManager method queryPurchaseHistoryAsync.

/**
 * Queries InApp and Subscribed purchase results from Google Play Remote Server.
 *
 * @param purchases this list contains all the product purchases made, has InApp and
 *     Subscription purchased results.
 * @param skuType InApp or Subscription.
 * @param executeWhenFinished Once the InApp product purchase results are given, then
 *     subscription based purchase results are queried and results are placed into the {@link
 *     #myPurchasesResultList}
 */
private void queryPurchaseHistoryAsync(final List<PurchaseHistoryRecord> purchases, @SkuType final String skuType, final Runnable executeWhenFinished) {
    PurchaseHistoryResponseListener listener = (billingResult, list) -> {
        if (billingResult.getResponseCode() == BillingResponseCode.OK && list != null) {
            purchases.addAll(list);
            if (executeWhenFinished != null) {
                executeWhenFinished.run();
            }
        } else {
            MonetizeApp.printLog(TAG, "queryPurchaseHistoryAsync() got an error response code: " + billingResult.getResponseCode());
            logErrorType(billingResult);
        }
        if (executeWhenFinished == null) {
            storePurchaseHistoryRecordsLocally(purchases);
        }
    };
    executeServiceRequest(() -> myBillingClient.queryPurchaseHistoryAsync(skuType, listener));
}
Also used : NetworkManager(com.litekite.monetize.network.NetworkManager) Context(android.content.Context) WorkExecutor(com.litekite.monetize.worker.WorkExecutor) NonNull(androidx.annotation.NonNull) BillingClientStateListener(com.android.billingclient.api.BillingClientStateListener) SkuDetailsResponseListener(com.android.billingclient.api.SkuDetailsResponseListener) AppDatabase(com.litekite.monetize.room.database.AppDatabase) PurchaseHistoryRecord(com.android.billingclient.api.PurchaseHistoryRecord) MonetizeApp(com.litekite.monetize.app.MonetizeApp) HashMap(java.util.HashMap) CallbackProvider(com.litekite.monetize.base.CallbackProvider) Singleton(javax.inject.Singleton) SkuType(com.android.billingclient.api.BillingClient.SkuType) BillingSkuDetails(com.litekite.monetize.room.entity.BillingSkuDetails) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) R(com.litekite.monetize.R) BillingResult(com.android.billingclient.api.BillingResult) PurchasesUpdatedListener(com.android.billingclient.api.PurchasesUpdatedListener) Map(java.util.Map) ConsumeParams(com.android.billingclient.api.ConsumeParams) PurchasesResponseListener(com.android.billingclient.api.PurchasesResponseListener) AcknowledgePurchaseParams(com.android.billingclient.api.AcknowledgePurchaseParams) SkuDetails(com.android.billingclient.api.SkuDetails) BillingResponseCode(com.android.billingclient.api.BillingClient.BillingResponseCode) ConsumeResponseListener(com.android.billingclient.api.ConsumeResponseListener) Set(java.util.Set) SkuDetailsParams(com.android.billingclient.api.SkuDetailsParams) PurchaseHistoryResponseListener(com.android.billingclient.api.PurchaseHistoryResponseListener) StringRes(androidx.annotation.StringRes) FeatureType(com.android.billingclient.api.BillingClient.FeatureType) Purchase(com.android.billingclient.api.Purchase) List(java.util.List) Nullable(androidx.annotation.Nullable) BillingClient(com.android.billingclient.api.BillingClient) AcknowledgePurchaseResponseListener(com.android.billingclient.api.AcknowledgePurchaseResponseListener) BillingPurchaseDetails(com.litekite.monetize.room.entity.BillingPurchaseDetails) BillingFlowParams(com.android.billingclient.api.BillingFlowParams) Activity(android.app.Activity) PurchaseHistoryResponseListener(com.android.billingclient.api.PurchaseHistoryResponseListener)

Example 19 with BillingResult

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

the class BillingSupport method _purchase.

public void _purchase(final String item, final String type) {
    if (!areSubscriptionsSupported() && type.equals(BillingClient.SkuType.SUBS)) {
        final PurchaseCallback pc = getPurchaseCallback();
        if (pc == null) {
            return;
        }
        CN.callSerially(new Runnable() {

            @Override
            public void run() {
                pc.itemPurchaseError(item, "Subscriptions are not supported on this device");
            }
        });
        return;
    }
    runWithConnection(new Runnable() {

        public void run() {
            billingClient.querySkuDetailsAsync(SkuDetailsParams.newBuilder().setType(type).setSkusList((List<String>) Arrays.asList(item)).build(), new SkuDetailsResponseListener() {

                @Override
                public void onSkuDetailsResponse(final BillingResult billingResult, final List<SkuDetails> list) {
                    if (isFailure(billingResult)) {
                        final PurchaseCallback pc = getPurchaseCallback();
                        if (pc == null) {
                            return;
                        }
                        CN.callSerially(new Runnable() {

                            @Override
                            public void run() {
                                pc.itemPurchaseError(item, billingResult.getDebugMessage());
                            }
                        });
                        return;
                    }
                    if (list == null || list.isEmpty()) {
                        final PurchaseCallback pc = getPurchaseCallback();
                        if (pc == null) {
                            return;
                        }
                        CN.callSerially(new Runnable() {

                            @Override
                            public void run() {
                                pc.itemPurchaseError(item, "No item could be found in the Playstore with sku " + item);
                            }
                        });
                        return;
                    }
                    for (SkuDetails details : list) {
                        inventory.add(details, type.equals(BillingClient.SkuType.SUBS));
                    }
                    activity.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            billingClient.launchBillingFlow(activity, BillingFlowParams.newBuilder().setSkuDetails(list.iterator().next()).build());
                        }
                    });
                }
            });
        }
    });
}
Also used : SkuDetails(com.android.billingclient.api.SkuDetails) SkuDetailsResponseListener(com.android.billingclient.api.SkuDetailsResponseListener) PurchaseCallback(com.codename1.payment.PurchaseCallback) PendingPurchaseCallback(com.codename1.payment.PendingPurchaseCallback) ArrayList(java.util.ArrayList) List(java.util.List) BillingResult(com.android.billingclient.api.BillingResult)

Example 20 with BillingResult

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

the class BillingSupport method getProducts.

@Override
public Product[] getProducts(String[] skus, boolean fromCacheOnly) {
    if (inventory != null) {
        final ArrayList pList = new ArrayList<Product>();
        final ArrayList moreskusList = new ArrayList<Product>();
        for (int i = 0; i < skus.length; i++) {
            String sku = skus[i];
            if (inventory.hasDetails(sku)) {
                pList.add(inventory.getProduct(sku));
            } else {
                moreskusList.add(sku);
            }
        }
        // if the inventory does not all the requestes sku make an update.
        if (moreskusList.size() > 0 && !fromCacheOnly) {
            final int[] complete = new int[1];
            final Object lock = new Object();
            runWithConnection(new Runnable() {

                public void run() {
                    billingClient.querySkuDetailsAsync(SkuDetailsParams.newBuilder().setType(BillingClient.SkuType.INAPP).setSkusList((List<String>) moreskusList).build(), new SkuDetailsResponseListener() {

                        @Override
                        public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> list) {
                            synchronized (lock) {
                                if (isFailure(billingResult)) {
                                    complete[0]++;
                                    lock.notifyAll();
                                    return;
                                }
                                for (SkuDetails details : list) {
                                    inventory.add(details);
                                }
                                complete[0]++;
                                lock.notifyAll();
                            }
                        }
                    });
                }
            }).except(new SuccessCallback<Throwable>() {

                public void onSucess(Throwable t) {
                    synchronized (lock) {
                        complete[0]++;
                        lock.notifyAll();
                    }
                }
            });
            if (areSubscriptionsSupported()) {
                runWithConnection(new Runnable() {

                    public void run() {
                        billingClient.querySkuDetailsAsync(SkuDetailsParams.newBuilder().setType(BillingClient.SkuType.SUBS).setSkusList((List<String>) moreskusList).build(), new SkuDetailsResponseListener() {

                            @Override
                            public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> list) {
                                synchronized (lock) {
                                    if (isFailure(billingResult)) {
                                        complete[0]++;
                                        lock.notifyAll();
                                        return;
                                    }
                                    for (SkuDetails details : list) {
                                        inventory.add(details, true);
                                    }
                                    complete[0]++;
                                    lock.notifyAll();
                                }
                            }
                        });
                    }
                }).except(new SuccessCallback<Throwable>() {

                    public void onSucess(Throwable t) {
                        synchronized (lock) {
                            complete[0]++;
                            lock.notifyAll();
                        }
                    }
                });
            } else {
                synchronized (lock) {
                    complete[0]++;
                    lock.notifyAll();
                }
            }
            while (complete[0] < 2) {
                CN.invokeAndBlock(new Runnable() {

                    @Override
                    public void run() {
                        synchronized (lock) {
                            while (complete[0] < 2) {
                                try {
                                    lock.wait(1000);
                                } catch (InterruptedException ex) {
                                }
                            }
                        }
                    }
                });
            }
            // inventory = mHelper.queryInventory(true, moreskusList);
            return getProducts(skus, true);
        }
        Product[] productsOut = new Product[pList.size()];
        productsOut = (Product[]) pList.toArray(productsOut);
        return productsOut;
    }
    return null;
}
Also used : SkuDetails(com.android.billingclient.api.SkuDetails) ArrayList(java.util.ArrayList) Product(com.codename1.payment.Product) SkuDetailsResponseListener(com.android.billingclient.api.SkuDetailsResponseListener) JSONObject(org.json.JSONObject) BillingResult(com.android.billingclient.api.BillingResult)

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