Search in sources :

Example 1 with BillingResult

use of com.android.billingclient.api.BillingResult in project zype-android by zype.

the class BillingManager method querySkuDetailsAsync.

/**
 * Query SKUs available for purchase
 */
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.Builder params = SkuDetailsParams.newBuilder();
            params.setSkusList(skuList).setType(itemType);
            mBillingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {

                @Override
                public void onSkuDetailsResponse(@NonNull BillingResult billingResult, @Nullable List<SkuDetails> list) {
                    listener.onSkuDetailsResponse(billingResult, list);
                }
            });
        }
    };
    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 2 with BillingResult

use of com.android.billingclient.api.BillingResult in project sms-backup-plus by jberkel.

the class DonationActivity method checkUserDonationStatus.

public static void checkUserDonationStatus(Context context, final DonationStatusListener listener) {
    final BillingClient helper = BillingClient.newBuilder(context).enablePendingPurchases().setListener(new PurchasesUpdatedListener() {

        @Override
        public void onPurchasesUpdated(BillingResult result, @Nullable List<Purchase> purchases) {
            log("onPurchasesUpdated(" + result + ", " + purchases + ")");
        }
    }).build();
    helper.startConnection(new BillingClientStateListener() {

        @Override
        public void onBillingSetupFinished(BillingResult result) {
            log("checkUserHasDonated: onBillingSetupFinished(" + result + ")");
            try {
                if (result.getResponseCode() == OK) {
                    PurchasesResult purchasesResult = helper.queryPurchases(INAPP);
                    if (result.getResponseCode() == OK) {
                        listener.userDonationState(userHasDonated(purchasesResult.getPurchasesList()) ? DONATED : NOT_DONATED);
                    } else {
                        listener.userDonationState(UNKNOWN);
                    }
                } else {
                    listener.userDonationState(result.getResponseCode() == BILLING_UNAVAILABLE ? NOT_AVAILABLE : UNKNOWN);
                }
            } finally {
                try {
                    helper.endConnection();
                } catch (Exception ignored) {
                }
            }
        }

        public void onBillingServiceDisconnected() {
        }
    });
}
Also used : BillingClientStateListener(com.android.billingclient.api.BillingClientStateListener) PurchasesResult(com.android.billingclient.api.Purchase.PurchasesResult) PurchasesUpdatedListener(com.android.billingclient.api.PurchasesUpdatedListener) BillingResult(com.android.billingclient.api.BillingResult) ArrayList(java.util.ArrayList) List(java.util.List) BillingClient(com.android.billingclient.api.BillingClient) Nullable(androidx.annotation.Nullable)

Example 3 with BillingResult

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

the class BillingManager method handleConsumablePurchasesAsync.

/**
 * Consumes InApp Product Purchase after successful purchase of InApp Product Purchase. InApp
 * Products cannot be bought after a purchase was made. We need to consume it after a successful
 * purchase, so that we can purchase again and it will become available for the next time we
 * make purchase of the same product that was bought before.
 *
 * @param purchase the purchase result contains Purchase Details.
 */
private void handleConsumablePurchasesAsync(Purchase purchase) {
    // onActivityResult()
    if (tokensToBeConsumed.contains(purchase.getPurchaseToken())) {
        MonetizeApp.printLog(TAG, "Token was already scheduled to be consumed - skipping...");
        return;
    }
    tokensToBeConsumed.add(purchase.getPurchaseToken());
    // Generating Consume Response listener
    final ConsumeResponseListener listener = (billingResult, purchaseToken) -> {
        // (feel free to introduce your retry policy here).
        if (billingResult.getResponseCode() == BillingResponseCode.OK) {
            MonetizeApp.printLog(TAG, "onConsumeResponse, Purchase Token: " + purchaseToken);
        } else {
            MonetizeApp.printLog(TAG, "onConsumeResponse: " + billingResult.getDebugMessage());
        }
    };
    // Consume the purchase async
    final ConsumeParams consumeParams = ConsumeParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
    // Creating a runnable from the request to use it inside our connection retry policy below
    executeServiceRequest(() -> myBillingClient.consumeAsync(consumeParams, 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) ConsumeResponseListener(com.android.billingclient.api.ConsumeResponseListener) ConsumeParams(com.android.billingclient.api.ConsumeParams)

Example 4 with BillingResult

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

the class BillingManager method queryPurchasesAsync.

/**
 * Queries InApp and Subscribed purchase results from Google Play Locally.
 *
 * @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 queryPurchasesAsync(final List<Purchase> purchases, @SkuType final String skuType, final Runnable executeWhenFinished) {
    PurchasesResponseListener purchasesResponseListener = (billingResult, list) -> {
        if (billingResult.getResponseCode() == BillingResponseCode.OK) {
            purchases.addAll(list);
            if (executeWhenFinished != null) {
                executeWhenFinished.run();
            }
        } else {
            MonetizeApp.printLog(TAG, "queryPurchasesAsync() got an error response code: " + billingResult.getResponseCode());
            logErrorType(billingResult);
        }
        if (executeWhenFinished == null) {
            processPurchases(purchases);
        }
    };
    executeServiceRequest(() -> myBillingClient.queryPurchasesAsync(skuType, purchasesResponseListener));
}
Also used : PurchasesResponseListener(com.android.billingclient.api.PurchasesResponseListener) 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)

Example 5 with BillingResult

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

the class BillingSupport method handlePurchase.

private void handlePurchase(final Purchase purchase) {
    if (handlingPurchase.contains(purchase.getPurchaseToken())) {
        return;
    }
    handlingPurchase.add(purchase.getPurchaseToken());
    final PurchaseCallback pc = getPurchaseCallback();
    if (!verifyDeveloperPayload(purchase)) {
        if (pc != null && pc instanceof PendingPurchaseCallback) {
            final PendingPurchaseCallback ppc = (PendingPurchaseCallback) pc;
            CN.callSerially(new Runnable() {

                public void run() {
                    for (String sku : purchase.getSkus()) {
                        ppc.itemPurchaseError(sku, "Invalid developer payload");
                    }
                }
            });
        }
        handlingPurchase.remove(purchase.getPurchaseToken());
        return;
    }
    if (purchase.getPurchaseState() != Purchase.PurchaseState.PURCHASED) {
        // This will be called again when the purchase completes.
        if (purchase.getPurchaseState() == Purchase.PurchaseState.PENDING && pc != null && pc instanceof PendingPurchaseCallback) {
            final PendingPurchaseCallback ppc = (PendingPurchaseCallback) pc;
            CN.callSerially(new Runnable() {

                @Override
                public void run() {
                    ppc.itemPurchasePending(purchase.getSkus().iterator().next());
                }
            });
        }
        handlingPurchase.remove(purchase.getPurchaseToken());
        return;
    }
    final String sku = purchase.getSkus().iterator().next();
    final Runnable onPurchaseAcknowledged = new Runnable() {

        public void run() {
            if (pc != null) {
                Display.getInstance().callSerially(new Runnable() {

                    @Override
                    public void run() {
                        // Sandbox transactions have no order ID, so we'll make a dummy transaction ID
                        // in this case.
                        String transactionId = (purchase.getOrderId() == null || purchase.getOrderId().isEmpty()) ? "play-sandbox-" + UUID.randomUUID().toString() : purchase.getOrderId();
                        String purchaseJsonStr = purchase.getOriginalJson();
                        try {
                            // In order to verify receipts, we'll need both the order data and the signature
                            // so we'll pack it all into a single JSON string.
                            JSONObject purchaseJson = new JSONObject(purchaseJsonStr);
                            JSONObject rootJson = new JSONObject();
                            rootJson.put("data", purchaseJson);
                            rootJson.put("signature", purchase.getSignature());
                            purchaseJsonStr = rootJson.toString();
                        } catch (JSONException ex) {
                            Logger.getLogger(CodenameOneActivity.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        com.codename1.payment.Purchase.postReceipt(Receipt.STORE_CODE_PLAY, sku, transactionId, purchase.getPurchaseTime(), purchaseJsonStr);
                        pc.itemPurchased(sku);
                    }
                });
                inventory.add(sku, purchase);
                // This is a temp hack to get the last purchase raw data
                // The IAP API needs to be modified to support this on all platforms
                Display.getInstance().setProperty("lastPurchaseData", purchase.getOriginalJson());
            }
        }
    };
    if (!isConsumable(sku)) {
        if (!purchase.isAcknowledged()) {
            runWithConnection(new Runnable() {

                public void run() {
                    billingClient.acknowledgePurchase(AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build(), new AcknowledgePurchaseResponseListener() {

                        @Override
                        public void onAcknowledgePurchaseResponse(final BillingResult billingResult) {
                            handlingPurchase.remove(purchase.getPurchaseToken());
                            if (isFailure(billingResult)) {
                                final PurchaseCallback pc = getPurchaseCallback();
                                if (pc != null) {
                                    Display.getInstance().callSerially(new Runnable() {

                                        @Override
                                        public void run() {
                                            pc.itemPurchaseError(sku, billingResult.getDebugMessage());
                                        }
                                    });
                                }
                            } else {
                                onPurchaseAcknowledged.run();
                            }
                        }
                    });
                }
            });
        } else {
            handlingPurchase.remove(purchase.getPurchaseToken());
        }
        return;
    }
    final ConsumeParams consumeParams = ConsumeParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
    final ConsumeResponseListener listener = new ConsumeResponseListener() {

        @Override
        public void onConsumeResponse(final BillingResult billingResult, String purchaseToken) {
            if (purchase != null)
                handlingPurchase.remove(purchase.getPurchaseToken());
            if (isFailure(billingResult)) {
                final PurchaseCallback pc = getPurchaseCallback();
                if (pc != null) {
                    Display.getInstance().callSerially(new Runnable() {

                        @Override
                        public void run() {
                            pc.itemPurchaseError(sku, billingResult.getDebugMessage());
                        }
                    });
                }
            } else {
                onPurchaseAcknowledged.run();
            }
            if (purchase != null) {
                inventory.erasePurchase(sku);
            }
        }
    };
    if (!purchase.isAcknowledged()) {
        runWithConnection(new Runnable() {

            public void run() {
                billingClient.consumeAsync(consumeParams, listener);
            }
        }).except(new SuccessCallback<Throwable>() {

            public void onSucess(final Throwable t) {
                if (purchase != null)
                    handlingPurchase.remove(purchase.getPurchaseToken());
                final PurchaseCallback pc = getPurchaseCallback();
                if (pc != null) {
                    Display.getInstance().callSerially(new Runnable() {

                        @Override
                        public void run() {
                            pc.itemPurchaseError(sku, t.getMessage());
                        }
                    });
                }
                if (purchase != null) {
                    inventory.erasePurchase(sku);
                }
            }
        });
    } else {
        handlingPurchase.remove(purchase.getPurchaseToken());
    }
}
Also used : ConsumeParams(com.android.billingclient.api.ConsumeParams) AcknowledgePurchaseResponseListener(com.android.billingclient.api.AcknowledgePurchaseResponseListener) JSONException(org.json.JSONException) PendingPurchaseCallback(com.codename1.payment.PendingPurchaseCallback) ConsumeResponseListener(com.android.billingclient.api.ConsumeResponseListener) JSONObject(org.json.JSONObject) PurchaseCallback(com.codename1.payment.PurchaseCallback) PendingPurchaseCallback(com.codename1.payment.PendingPurchaseCallback) 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