Search in sources :

Example 6 with ConsumeResponseListener

use of com.android.billingclient.api.ConsumeResponseListener 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)

Example 7 with ConsumeResponseListener

use of com.android.billingclient.api.ConsumeResponseListener 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 8 with ConsumeResponseListener

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

the class BillingManager method consumeAsync.

/**
 * 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 purchaseToken a token that uniquely identifies a purchase for a given item and user
 *                      pair.
 */
private void consumeAsync(final String purchaseToken) {
    // onActivityResult()
    if (tokensToBeConsumed == null) {
        tokensToBeConsumed = new HashSet<>();
    } else if (tokensToBeConsumed.contains(purchaseToken)) {
        BaseActivity.printLog(TAG, "Token was already scheduled to be consumed - skipping...");
        return;
    }
    tokensToBeConsumed.add(purchaseToken);
    // Generating Consume Response listener
    final ConsumeResponseListener onConsumeListener = new ConsumeResponseListener() {

        @Override
        public void onConsumeResponse(@BillingResponse int responseCode, String purchaseToken) {
            // If billing service was disconnected, we try to reconnect 1 time
            // (feel free to introduce your retry policy here).
            BaseActivity.printLog(TAG, "Consume Response, Purchase Token: " + purchaseToken);
            logErrorType(responseCode);
        }
    };
    // 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
            myBillingClient.consumeAsync(purchaseToken, onConsumeListener);
        }
    };
    executeServiceRequest(consumeRequest);
}
Also used : ConsumeResponseListener(com.android.billingclient.api.ConsumeResponseListener) BillingResponse(com.android.billingclient.api.BillingClient.BillingResponse)

Example 9 with ConsumeResponseListener

use of com.android.billingclient.api.ConsumeResponseListener 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 10 with ConsumeResponseListener

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

the class BillingManager method clearPurchases.

/**
 * Consumes all purchases.
 * Used only for testing one-time purchases
 */
public void clearPurchases() {
    if (purchases != null) {
        for (Purchase purchase : purchases) {
            ConsumeResponseListener listener = new ConsumeResponseListener() {

                @Override
                public void onConsumeResponse(@NonNull BillingResult billingResult, @NonNull String s) {
                    if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                    // Handle the success of the consume operation.
                    // For example, increase the number of coins inside the user's basket.
                    }
                }
            };
            ConsumeParams consumeParams = ConsumeParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
            mBillingClient.consumeAsync(consumeParams, listener);
        }
    }
}
Also used : Purchase(com.android.billingclient.api.Purchase) ConsumeResponseListener(com.android.billingclient.api.ConsumeResponseListener) ConsumeParams(com.android.billingclient.api.ConsumeParams) NonNull(androidx.annotation.NonNull) BillingResult(com.android.billingclient.api.BillingResult)

Aggregations

ConsumeResponseListener (com.android.billingclient.api.ConsumeResponseListener)14 BillingResult (com.android.billingclient.api.BillingResult)12 ConsumeParams (com.android.billingclient.api.ConsumeParams)10 Purchase (com.android.billingclient.api.Purchase)4 NonNull (androidx.annotation.NonNull)3 AcknowledgePurchaseResponseListener (com.android.billingclient.api.AcknowledgePurchaseResponseListener)3 Nullable (androidx.annotation.Nullable)2 AcknowledgePurchaseParams (com.android.billingclient.api.AcknowledgePurchaseParams)2 BillingClient (com.android.billingclient.api.BillingClient)2 BillingClientStateListener (com.android.billingclient.api.BillingClientStateListener)2 BillingFlowParams (com.android.billingclient.api.BillingFlowParams)2 PurchasesUpdatedListener (com.android.billingclient.api.PurchasesUpdatedListener)2 SkuDetails (com.android.billingclient.api.SkuDetails)2 SkuDetailsParams (com.android.billingclient.api.SkuDetailsParams)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Activity (android.app.Activity)1 Context (android.content.Context)1 Log (android.util.Log)1 LayoutInflater (android.view.LayoutInflater)1