Java Code Examples for com.android.billingclient.api.BillingResult#getResponseCode()

The following examples show how to use com.android.billingclient.api.BillingResult#getResponseCode() . 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: ActivityBilling.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
private void onPurchase(Intent intent) {
    if (Helper.isPlayStoreInstall()) {
        BillingFlowParams.Builder flowParams = BillingFlowParams.newBuilder();
        if (skuDetails.containsKey(getSkuPro())) {
            Log.i("IAB purchase SKU=" + skuDetails.get(getSkuPro()));
            flowParams.setSkuDetails(skuDetails.get(getSkuPro()));
        }

        BillingResult result = billingClient.launchBillingFlow(this, flowParams.build());
        if (result.getResponseCode() != BillingClient.BillingResponseCode.OK)
            reportError(result, "IAB launch billing flow");
    } else
        try {
            Uri uri = Uri.parse(BuildConfig.PRO_FEATURES_URI + "?challenge=" + getChallenge(this));
            Helper.view(this, uri, true);
        } catch (NoSuchAlgorithmException ex) {
            Log.unexpectedError(getSupportFragmentManager(), ex);
        }
}
 
Example 2
Source File: ActivityBilling.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
private void reportError(BillingResult result, String stage) {
    String message;
    if (result == null)
        message = stage;
    else {
        String debug = result.getDebugMessage();
        message = getBillingResponseText(result) + (debug == null ? "" : " " + debug) + " " + stage;

        // https://developer.android.com/reference/com/android/billingclient/api/BillingClient.BillingResponse#service_disconnected
        if (result.getResponseCode() == BillingClient.BillingResponseCode.SERVICE_DISCONNECTED)
            retry(60);
    }

    EntityLog.log(this, message);

    if (result.getResponseCode() != BillingClient.BillingResponseCode.USER_CANCELED)
        for (IBillingListener listener : listeners)
            listener.onError(message);
}
 
Example 3
Source File: BillingManager.java    From PhoneProfilesPlus with Apache License 2.0 6 votes vote down vote up
@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
    int responseCode = billingResult.getResponseCode();
    //PPApplication.logE(TAG, "onPurchasesUpdated() response: " + responseCode);
    if (responseCode == BillingClient.BillingResponseCode.OK) {
        getFragment().purchaseSuccessful(purchases);

        if (purchases != null) {
            for (Purchase purchase : purchases) {
                consumePurchase(purchase);
            }
        }
    }
    else {
        getFragment().purchaseUnsuccessful(purchases);
        getFragment().displayAnErrorIfNeeded(responseCode);
    }
}
 
Example 4
Source File: PreferencesBillingHelper.java    From CommonUtils with Apache License 2.0 6 votes vote down vote up
@Override
public void onPurchasesUpdated(BillingResult br, @Nullable List<Purchase> purchases) {
    if (br.getResponseCode() == BillingResponseCode.OK) {
        listener.showToast(Toaster.build().message(R.string.thankYou).extra(purchases == null ? null : purchases.toString()));
        if (purchases == null || purchases.isEmpty()) return;

        for (Purchase p : purchases) {
            if (p.isAcknowledged()) continue;

            AcknowledgePurchaseParams params = AcknowledgePurchaseParams.newBuilder()
                    .setPurchaseToken(p.getPurchaseToken())
                    .build();
            billingClient.acknowledgePurchase(params, br1 -> {
                if (br1.getResponseCode() != BillingResponseCode.OK)
                    handleBillingErrors(br1.getResponseCode());
            });
        }
    } else {
        handleBillingErrors(br.getResponseCode());
    }
}
 
Example 5
Source File: AccelerateDevelop.java    From InviZible with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
    if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
        billingServiceConnected = true;
        Log.i(LOG_TAG, "Billing setup is finished");
        //below you can query information about products and purchase
        querySkuDetails(); //query for products
        List<Purchase> purchasesList = queryPurchases(); //query for purchases

        //if the purchase has already been made to give the goods
        if (purchasesList != null) {
            handlePurchases(purchasesList);
        }
    }
}
 
Example 6
Source File: GoogleBillingHelper.java    From Augendiagnose with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onPurchasesUpdated(final BillingResult billingResult, final @Nullable List<Purchase> purchaseList) {
	Log.i(TAG, "Purchase finished: " + billingResult.getResponseCode());

	if (billingResult.getResponseCode() != BillingResponseCode.OK) {
		Log.e(TAG, "Error purchasing: " + billingResult.getDebugMessage());
		if (mOnPurchaseSuccessListener != null) {
			mOnPurchaseSuccessListener.handleFailure();
		}
		return;
	}

	Log.i(TAG, "Purchase successful.");

	if (mOnPurchaseSuccessListener != null && purchaseList != null) {
		boolean hasPurchase = false;
		boolean isPurchased = false;
		for (Purchase purchase : purchaseList) {
			hasPurchase = true;
			if (purchase.getPurchaseState() == PurchaseState.PURCHASED) {
				Log.i(TAG, "Purchase " + purchase.getSku() + " finished");
				isPurchased = true;
				doAcknowledgePurchaseIfRequired(purchase);
			}
		}
		if (hasPurchase) {
			mOnPurchaseSuccessListener.handlePurchase(!mIsPremium, !isPurchased);
			if (isPurchased) {
				mIsPremium = true;
			}
		}
	}
}
 
Example 7
Source File: GoogleIap.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> list) {
    if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
            && list != null) {
        for (Purchase purchase : list) {
            handlePurchase(purchase);
        }
        mPurchasesUpdatedListener.onPurchasesUpdated();
    } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
        // Handle an error caused by a user cancelling the purchase flow.
    } else {
        // Handle any other error codes.
    }
}
 
Example 8
Source File: GoogleIap.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onPurchaseHistoryResponse(BillingResult billingResult, List<PurchaseHistoryRecord> list) {
    if (billingResult.getResponseCode() != BillingClient.BillingResponseCode.OK) {
        EventCollector.logException("queryPurchasesHistory" + billingResult.getDebugMessage());
    }

}
 
Example 9
Source File: PictureInPictureUpgradeActivity.java    From dtube-mobile-unofficial with Apache License 2.0 5 votes vote down vote up
@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
    if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && purchases != null) {
        for (Purchase purchase : purchases) {
            handlePurchase(purchase);
        }
    } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
        // Handle an error caused by a user cancelling the purchase flow.
    } else {
        // Handle any other error codes.
    }
}
 
Example 10
Source File: DefaultBillingManager.java    From SAI with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
    if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
        mBillingStatus.setValue(BillingManagerStatus.OK);
        Log.d(TAG, "Billing service connected");
        loadPurchases();
        loadProducts();
    } else {
        mBillingStatus.setValue(BillingManagerStatus.NOT_AVAILABLE);
        Log.w(TAG, String.format("Unable to connect to billing service, code %d - %s", billingResult.getResponseCode(), billingResult.getDebugMessage()));
    }
}
 
Example 11
Source File: DefaultBillingManager.java    From SAI with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> list) {
    if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.SERVICE_DISCONNECTED) {
        connectBillingService();
    } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
        loadPurchases();
    } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) {
        loadPurchases();
    }
}
 
Example 12
Source File: MainActivity.java    From PixelWatchFace with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
  if (billingResult.getResponseCode() == BillingResponseCode.OK
      && purchases != null) {
    for (Purchase purchase : purchases) {
      handlePurchase(purchase);
    }
  } else if (billingResult.getResponseCode() == BillingResponseCode.USER_CANCELED) {
    Snackbar.make(findViewById(android.R.id.content), "Purchase cancelled", Snackbar.LENGTH_SHORT)
        .show();
  } else {
    // Handle any other error codes.
  }
}
 
Example 13
Source File: PreferencesBillingHelper.java    From CommonUtils with Apache License 2.0 5 votes vote down vote up
private void buyProduct(@NonNull Activity activity, @NonNull SkuDetails product) {
    BillingFlowParams flowParams = BillingFlowParams.newBuilder()
            .setSkuDetails(product)
            .build();

    BillingResult result = billingClient.launchBillingFlow(activity, flowParams);
    if (result.getResponseCode() != BillingResponseCode.OK)
        handleBillingErrors(result.getResponseCode());
}
 
Example 14
Source File: ActivityBilling.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onPurchasesUpdated(BillingResult result, @Nullable List<Purchase> purchases) {
    if (result.getResponseCode() == BillingClient.BillingResponseCode.OK)
        checkPurchases(purchases);
    else
        reportError(result, "IAB purchases updated");
}
 
Example 15
Source File: ActivityBilling.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onBillingSetupFinished(BillingResult result) {
    if (result.getResponseCode() == BillingClient.BillingResponseCode.OK) {
        for (IBillingListener listener : listeners)
            listener.onConnected();

        backoff = 4;
        queryPurchases();
    } else
        reportError(result, "IAB connected");
}
 
Example 16
Source File: PlayBillingWrapper.java    From android-browser-helper with Apache License 2.0 5 votes vote down vote up
@Override
public boolean launchPaymentFlow(SkuDetails sku) {
    BillingFlowParams params = BillingFlowParams
            .newBuilder()
            .setSkuDetails(sku)
            .build();

    BillingResult result = mClient.launchBillingFlow(mActivity, params);

    return result.getResponseCode() == BillingClient.BillingResponseCode.OK;
}
 
Example 17
Source File: MainActivity.java    From PixelWatchFace with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
  if (billingResult.getResponseCode() != BillingResponseCode.OK) {
    Log.e("acknowledgePurchase", "Error acknowledging purchase, this shouldn't happen");
  }
}
 
Example 18
Source File: ActivityBilling.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
private static String getBillingResponseText(BillingResult result) {
    switch (result.getResponseCode()) {
        case BillingClient.BillingResponseCode.BILLING_UNAVAILABLE:
            // Billing API version is not supported for the type requested
            return "BILLING_UNAVAILABLE";

        case BillingClient.BillingResponseCode.DEVELOPER_ERROR:
            // Invalid arguments provided to the API.
            return "DEVELOPER_ERROR";

        case BillingClient.BillingResponseCode.ERROR:
            // Fatal error during the API action
            return "ERROR";

        case BillingClient.BillingResponseCode.FEATURE_NOT_SUPPORTED:
            // Requested feature is not supported by Play Store on the current device.
            return "FEATURE_NOT_SUPPORTED";

        case BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED:
            // Failure to purchase since item is already owned
            return "ITEM_ALREADY_OWNED";

        case BillingClient.BillingResponseCode.ITEM_NOT_OWNED:
            // Failure to consume since item is not owned
            return "ITEM_NOT_OWNED";

        case BillingClient.BillingResponseCode.ITEM_UNAVAILABLE:
            // Requested product is not available for purchase
            return "ITEM_UNAVAILABLE";

        case BillingClient.BillingResponseCode.OK:
            // Success
            return "OK";

        case BillingClient.BillingResponseCode.SERVICE_DISCONNECTED:
            // Play Store service is not connected now - potentially transient state.
            return "SERVICE_DISCONNECTED";

        case BillingClient.BillingResponseCode.SERVICE_UNAVAILABLE:
            // Network connection is down
            return "SERVICE_UNAVAILABLE";

        case BillingClient.BillingResponseCode.SERVICE_TIMEOUT:
            // The request has reached the maximum timeout before Google Play responds.
            return "SERVICE_TIMEOUT";

        case BillingClient.BillingResponseCode.USER_CANCELED:
            // User pressed back or canceled a dialog
            return "USER_CANCELED";

        default:
            return Integer.toString(result.getResponseCode());
    }
}
 
Example 19
Source File: GoogleBillingHelper.java    From Augendiagnose with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Handle result of inventory query.
 *
 * @param billingResult  The result flag.
 * @param skuDetailsList The retrieved SKU details.
 * @param isSubscription Flag indicating if this was subscription query.
 * @param listener       The listener called when inventory calls are finished.
 */
private void onSkuDetailsResponse(final BillingResult billingResult, final List<SkuDetails> skuDetailsList, final boolean isSubscription,
								  final OnInventoryFinishedListener listener) {
	Log.d(TAG, "Query inventory finished - " + billingResult.getResponseCode() + " - " + isSubscription);

	if (billingResult.getResponseCode() != BillingResponseCode.OK) {
		Log.e(TAG, "Failed to query inventory: " + billingResult.getDebugMessage());
		return;
	}

	Log.d(TAG, "Query inventory was successful.");

	Map<String, Purchase> purchaseMap = new HashMap<>();
	List<SkuPurchase> skuPurchases = new ArrayList<>();

	PurchasesResult purchasesResult = mBillingClient.queryPurchases(isSubscription ? SkuType.SUBS : SkuType.INAPP);
	if (purchasesResult.getResponseCode() != BillingResponseCode.OK) {
		Log.e(TAG, "Failed to query purchases: " + purchasesResult.getBillingResult().getDebugMessage());
		return;
	}
	for (Purchase purchase : purchasesResult.getPurchasesList()) {
		if (purchase.getPackageName().equals(mContext.getPackageName())) {
			purchaseMap.put(purchase.getSku(), purchase);
			if (purchase.getPurchaseState() == PurchaseState.PURCHASED) {
				doAcknowledgePurchaseIfRequired(purchase);
				mIsPremium = true;
			}
		}
	}

	for (SkuDetails skuDetails : skuDetailsList) {
		if (purchaseMap.containsKey(skuDetails.getSku())) {
			skuPurchases.add(new SkuPurchase(skuDetails, purchaseMap.get(skuDetails.getSku())));
		}
		else {
			skuPurchases.add(new SkuPurchase(skuDetails));
		}
	}

	synchronized (this) {
		if (isSubscription) {
			mSubscriptionSkus = skuPurchases;
		}
		else {
			mInAppSkus = skuPurchases;
		}

		if (listener != null && mSubscriptionSkus != null && mInAppSkus != null) {
			listener.handleProducts(mInAppSkus, mSubscriptionSkus);
		}
	}
}