Java Code Examples for com.android.billingclient.api.BillingClient.BillingResponse#OK

The following examples show how to use com.android.billingclient.api.BillingClient.BillingResponse#OK . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From scroball with MIT License 6 votes vote down vote up
@Override
public void onPurchasesUpdated(int responseCode, List<Purchase> purchases) {
  if (responseCode != BillingResponse.OK) {
    purchaseFailed();
  } else if (purchases != null) {
    for (Purchase purchase : purchases) {
      if (purchase.getSku().equals(REMOVE_ADS_SKU)) {
        RelativeLayout parent = (RelativeLayout) adView.getParent();
        if (parent != null) {
          parent.removeView(adView);
        }
        this.invalidateOptionsMenu();
        this.adsRemoved = true;
        SharedPreferences.Editor editor = application.getSharedPreferences().edit();
        editor.putBoolean(REMOVE_ADS_SKU, true);
        editor.apply();
      }
    }
  }
}
 
Example 2
Source File: MainActivity.java    From FCM-for-Mojo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
    if (responseCode == BillingResponse.OK
            && purchases != null) {
        for (Purchase purchase : purchases) {
            mBillingClient.consumeAsync(purchase.getPurchaseToken(), (responseCode1, purchaseToken) -> {
            });
        }
    }
}
 
Example 3
Source File: AcquireFragment.java    From play-billing-codelab with Apache License 2.0 5 votes vote down vote up
/**
 * Executes query for SKU details at the background thread
 */
private void handleManagerAndUiReady() {
    final List<SkuRowData> inList = new ArrayList<>();
    SkuDetailsResponseListener responseListener = new SkuDetailsResponseListener() {
        @Override
        public void onSkuDetailsResponse(int responseCode,
                List<SkuDetails> skuDetailsList) {
            // If we successfully got SKUs, add a header in front of it
            if (responseCode == BillingResponse.OK && skuDetailsList != null) {
                // Repacking the result for an adapter
                for (SkuDetails details : skuDetailsList) {
                    Log.i(TAG, "Found sku: " + details);
                    inList.add(new SkuRowData(details.getSku(), details.getTitle(),
                            details.getPrice(), details.getDescription(),
                            details.getType()));
                }
                if (inList.size() == 0) {
                    displayAnErrorIfNeeded();
                } else {
                    mAdapter.updateData(inList);
                    setWaitScreen(false);
                }
            }
        }
    };

    // Start querying for in-app SKUs
    List<String> skus = mBillingProvider.getBillingManager().getSkus(SkuType.INAPP);
    mBillingProvider.getBillingManager().querySkuDetailsAsync(SkuType.INAPP, skus, responseListener);
    // Start querying for subscriptions SKUs
    skus = mBillingProvider.getBillingManager().getSkus(SkuType.SUBS);
    mBillingProvider.getBillingManager().querySkuDetailsAsync(SkuType.SUBS, skus, responseListener);
}
 
Example 4
Source File: AcquireFragment.java    From play-billing-codelab with Apache License 2.0 5 votes vote down vote up
/**
 * Executes query for SKU details at the background thread
 */
private void handleManagerAndUiReady() {
    final List<SkuRowData> inList = new ArrayList<>();
    SkuDetailsResponseListener responseListener = new SkuDetailsResponseListener() {
        @Override
        public void onSkuDetailsResponse(int responseCode,
                                         List<SkuDetails> skuDetailsList) {
            if (responseCode == BillingResponse.OK && skuDetailsList != null) {
                // Repacking the result for an adapter
                for (SkuDetails details : skuDetailsList) {
                    Log.i(TAG, "Found sku: " + details);
                    inList.add(new SkuRowData(details.getSku(), details.getTitle(),
                            details.getPrice(), details.getDescription(),
                            details.getType()));
                }
                if (inList.size() == 0) {
                    displayAnErrorIfNeeded();
                } else {
                    mAdapter.updateData(inList);
                    setWaitScreen(false);
                }
            }
        }
    };

    // Start querying for in-app SKUs
    List<String> skus = mBillingProvider.getBillingManager().getSkus(SkuType.INAPP);
    mBillingProvider.getBillingManager().querySkuDetailsAsync(SkuType.INAPP, skus, responseListener);
    // Start querying for subscriptions SKUs
    skus = mBillingProvider.getBillingManager().getSkus(SkuType.SUBS);
    mBillingProvider.getBillingManager().querySkuDetailsAsync(SkuType.SUBS, skus, responseListener);
}
 
Example 5
Source File: AcquireFragment.java    From play-billing-codelab with Apache License 2.0 5 votes vote down vote up
/**
 * Executes query for SKU details at the background thread
 */
private void handleManagerAndUiReady() {
    final List<SkuRowData> inList = new ArrayList<>();
    SkuDetailsResponseListener responseListener = new SkuDetailsResponseListener() {
        @Override
        public void onSkuDetailsResponse(int responseCode,
                                         List<SkuDetails> skuDetailsList) {
            if (responseCode == BillingResponse.OK && skuDetailsList != null) {
                // Repacking the result for an adapter
                for (SkuDetails details : skuDetailsList) {
                    Log.i(TAG, "Found sku: " + details);
                    inList.add(new SkuRowData(details.getSku(), details.getTitle(),
                            details.getPrice(), details.getDescription(),
                            details.getType()));
                }
                if (inList.size() == 0) {
                    displayAnErrorIfNeeded();
                } else {
                    mAdapter.updateData(inList);
                    setWaitScreen(false);
                }
            }
        }
    };

    // Start querying for in-app SKUs
    List<String> skus = mBillingProvider.getBillingManager().getSkus(SkuType.INAPP);
    mBillingProvider.getBillingManager().querySkuDetailsAsync(SkuType.INAPP, skus, responseListener);
    // Start querying for subscriptions SKUs
    skus = mBillingProvider.getBillingManager().getSkus(SkuType.SUBS);
    mBillingProvider.getBillingManager().querySkuDetailsAsync(SkuType.SUBS, skus, responseListener);
}
 
Example 6
Source File: MainActivity.java    From scroball with MIT License 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
    case R.id.settings_item:
      Intent intent = new Intent(getBaseContext(), SettingsActivity.class);
      startActivityForResult(intent, 1);
      return true;
    case R.id.remove_ads_item:
      BillingFlowParams.Builder builder =
          new BillingFlowParams.Builder()
              .setSku(REMOVE_ADS_SKU)
              .setType(BillingClient.SkuType.INAPP);
      int responseCode = billingClient.launchBillingFlow(this, builder.build());
      if (responseCode != BillingResponse.OK) {
        purchaseFailed();
      }
      return true;
    case R.id.privacy_policy_item:
      Intent browserIntent =
          new Intent(
              Intent.ACTION_VIEW,
              Uri.parse("https://scroball.peterjosling.com/privacy_policy.html"));
      startActivity(browserIntent);
      return true;
    case R.id.logout_item:
      logout();
      return true;
    default:
      return super.onOptionsItemSelected(item);
  }
}
 
Example 7
Source File: GooglePlayBillingApi.java    From Cashier with Apache License 2.0 5 votes vote down vote up
@Override
public int isBillingSupported(@SkuType String itemType) {
    throwIfUnavailable();

    if (SkuType.INAPP.equalsIgnoreCase(itemType) && billing.isReady()) {
        return BillingResponse.OK;
    } else if (SkuType.SUBS.equalsIgnoreCase(itemType)) {
        return billing.isFeatureSupported(FeatureType.SUBSCRIPTIONS);
    }

    return BillingResponse.FEATURE_NOT_SUPPORTED;
}
 
Example 8
Source File: GooglePlayBillingApi.java    From Cashier with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public List<Purchase> getPurchases() {
    throwIfUnavailable();

    List<Purchase> allPurchases = new ArrayList<>();

    logSafely("Querying in-app purchases...");
    Purchase.PurchasesResult inAppPurchasesResult = billing.queryPurchases(SkuType.INAPP);

    if (inAppPurchasesResult.getResponseCode() == BillingResponse.OK) {
        List<Purchase> inAppPurchases = inAppPurchasesResult.getPurchasesList();
        logSafely("In-app purchases: " + TextUtils.join(", ", inAppPurchases));
        allPurchases.addAll(inAppPurchases);
        // Check if we support subscriptions and query those purchases as well
        boolean isSubscriptionSupported =
                billing.isFeatureSupported(FeatureType.SUBSCRIPTIONS) == BillingResponse.OK;
        if (isSubscriptionSupported) {
            logSafely("Querying subscription purchases...");
            Purchase.PurchasesResult subscriptionPurchasesResult = billing.queryPurchases(SkuType.SUBS);

            if (subscriptionPurchasesResult.getResponseCode() == BillingResponse.OK) {
                List<Purchase> subscriptionPurchases = subscriptionPurchasesResult.getPurchasesList();
                logSafely("Subscription purchases: " + TextUtils.join(", ", subscriptionPurchases));
                allPurchases.addAll(subscriptionPurchases);
                return allPurchases;
            } else {
                logSafely("Error in querying subscription purchases with code: "
                        + subscriptionPurchasesResult.getResponseCode());
                return allPurchases;
            }
        } else {
            logSafely("Subscriptions are not supported...");
            return allPurchases;
        }
    } else {
        return null;
    }
}
 
Example 9
Source File: GooglePlayBillingApi.java    From Cashier with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public List<Purchase> getPurchases(String itemType) {
    throwIfUnavailable();

    Purchase.PurchasesResult purchasesResult = billing.queryPurchases(itemType);
    if (purchasesResult.getResponseCode() == BillingResponse.OK) {
        List<Purchase> purchases = purchasesResult.getPurchasesList();
        logSafely(itemType + " purchases: " + TextUtils.join(", ", purchases));
        return purchases;
    }

    return null;
}
 
Example 10
Source File: GooglePlayBillingApi.java    From Cashier with Apache License 2.0 5 votes vote down vote up
@Override
public void onBillingSetupFinished(@BillingResponse int billingResponseCode) {
    logSafely("Service setup finished and connected. Response: " + billingResponseCode);

    if (billingResponseCode == BillingResponse.OK) {
        isServiceConnected = true;

        if (listener != null) {
            listener.initialized(available());
        }
    }
}
 
Example 11
Source File: GooglePlayBillingVendor.java    From Cashier with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void initialized(boolean success) {
    logSafely("Initialized: success = " + success);
    if (!success) {
        logAndDisable("Could not create Google Play Billing instance");
        return;
    }

    try {
        canPurchaseItems =
                api.isBillingSupported(SkuType.INAPP) == BillingResponse.OK;

        canSubscribe =
                api.isBillingSupported(SkuType.SUBS) == BillingResponse.OK;

        available = canPurchaseItems || canSubscribe;
        logSafely("Connected to service and it is " + (available ? "available" : "not available"));
        initializing = false;

        for (InitializationListener listener : initializationListeners) {
            listener.initialized();
        }
        initializationListeners.clear();
    } catch (Exception error) {
        logAndDisable(Log.getStackTraceString(error));
    }
}
 
Example 12
Source File: GooglePlayBillingVendor.java    From Cashier with Apache License 2.0 5 votes vote down vote up
@Override
public void onPurchasesUpdated(@BillingResponse int responseCode,
                               @Nullable List<com.android.billingclient.api.Purchase> purchases) {
    if (purchaseListener == null) {
        pendingProduct = null;
        logSafely("#onPurchasesUpdated called but no purchase listener attached.");
        return;
    }

    switch (responseCode) {
        case BillingResponse.OK:
            if (purchases == null || purchases.isEmpty()) {
                purchaseListener.failure(pendingProduct, new Error(PURCHASE_FAILURE, responseCode));
                clearPendingPurchase();
                return;
            }

            for (com.android.billingclient.api.Purchase purchase : purchases) {
                handlePurchase(purchase, responseCode);
            }
            return;
        case BillingResponse.USER_CANCELED:
            logSafely("User canceled the purchase code: " + responseCode);
            purchaseListener.failure(pendingProduct, getPurchaseError(responseCode));
            clearPendingPurchase();
            return;
        default:
            logSafely("Error purchasing item with code: " + responseCode);
            purchaseListener.failure(pendingProduct, getPurchaseError(responseCode));
            clearPendingPurchase();
    }
}