use of com.android.billingclient.api.BillingResult 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);
}
}
}
use of com.android.billingclient.api.BillingResult in project Osmand by osmandapp.
the class InAppPurchaseHelperImpl method getSkuDetailsResponseListener.
// Listener that's called when we finish querying the items and subscriptions we own
private SkuDetailsResponseListener getSkuDetailsResponseListener(boolean userRequested) {
return new SkuDetailsResponseListener() {
@NonNull
private List<String> getAllOwnedSubscriptionSkus() {
List<String> result = new ArrayList<>();
BillingManager billingManager = getBillingManager();
if (billingManager != null) {
for (Purchase p : billingManager.getPurchases()) {
if (getInAppPurchases().getInAppSubscriptionBySku(p.getSku()) != null) {
result.add(p.getSku());
}
}
}
for (Entry<String, SubscriptionStateHolder> entry : subscriptionStateMap.entrySet()) {
SubscriptionState state = entry.getValue().state;
if (state == SubscriptionState.PAUSED || state == SubscriptionState.ON_HOLD) {
String sku = entry.getKey();
if (!result.contains(sku)) {
result.add(sku);
}
}
}
return result;
}
@Override
public void onSkuDetailsResponse(@NonNull BillingResult billingResult, List<SkuDetails> skuDetailsList) {
logDebug("Query sku details finished.");
// Have we been disposed of in the meantime? If so, quit.
if (getBillingManager() == null) {
stop(true);
return;
}
// Is it a failure?
if (billingResult.getResponseCode() != BillingClient.BillingResponseCode.OK) {
logError("Failed to query inventory: " + billingResult.getResponseCode());
notifyError(InAppPurchaseTaskType.REQUEST_INVENTORY, billingResult.getDebugMessage());
stop(true);
return;
}
logDebug("Query sku details was successful.");
/*
* Check for items we own. Notice that for each purchase, we check
* the developer payload to see if it's correct! See
* verifyDeveloperPayload().
*/
List<String> allOwnedSubscriptionSkus = getAllOwnedSubscriptionSkus();
for (InAppSubscription s : getSubscriptions().getAllSubscriptions()) {
if (hasDetails(s.getSku())) {
Purchase purchase = getPurchase(s.getSku());
SkuDetails liveUpdatesDetails = getSkuDetails(s.getSku());
if (liveUpdatesDetails != null) {
fetchInAppPurchase(s, liveUpdatesDetails, purchase);
}
allOwnedSubscriptionSkus.remove(s.getSku());
}
}
for (String sku : allOwnedSubscriptionSkus) {
Purchase purchase = getPurchase(sku);
SkuDetails liveUpdatesDetails = getSkuDetails(sku);
if (liveUpdatesDetails != null) {
InAppSubscription s = getSubscriptions().upgradeSubscription(sku);
if (s == null) {
s = new InAppPurchaseLiveUpdatesOldSubscription(liveUpdatesDetails);
}
fetchInAppPurchase(s, liveUpdatesDetails, purchase);
}
}
InAppPurchase fullVersion = getFullVersion();
if (hasDetails(fullVersion.getSku())) {
Purchase purchase = getPurchase(fullVersion.getSku());
SkuDetails fullPriceDetails = getSkuDetails(fullVersion.getSku());
if (fullPriceDetails != null) {
fetchInAppPurchase(fullVersion, fullPriceDetails, purchase);
}
}
InAppPurchase depthContours = getDepthContours();
if (hasDetails(depthContours.getSku())) {
Purchase purchase = getPurchase(depthContours.getSku());
SkuDetails depthContoursDetails = getSkuDetails(depthContours.getSku());
if (depthContoursDetails != null) {
fetchInAppPurchase(depthContours, depthContoursDetails, purchase);
}
}
InAppPurchase contourLines = getContourLines();
if (hasDetails(contourLines.getSku())) {
Purchase purchase = getPurchase(contourLines.getSku());
SkuDetails contourLinesDetails = getSkuDetails(contourLines.getSku());
if (contourLinesDetails != null) {
fetchInAppPurchase(contourLines, contourLinesDetails, purchase);
}
}
Purchase fullVersionPurchase = getPurchase(fullVersion.getSku());
boolean fullVersionPurchased = fullVersionPurchase != null;
if (fullVersionPurchased) {
ctx.getSettings().FULL_VERSION_PURCHASED.set(true);
}
Purchase depthContoursPurchase = getPurchase(depthContours.getSku());
boolean depthContoursPurchased = depthContoursPurchase != null;
if (depthContoursPurchased) {
ctx.getSettings().DEPTH_CONTOURS_PURCHASED.set(true);
}
// Do we have the live updates?
boolean subscribedToLiveUpdates = false;
boolean subscribedToOsmAndPro = false;
boolean subscribedToMaps = false;
List<Purchase> subscriptionPurchases = new ArrayList<>();
for (InAppSubscription s : getSubscriptions().getAllSubscriptions()) {
Purchase purchase = getPurchase(s.getSku());
if (purchase != null || s.getState().isActive()) {
if (purchase != null) {
subscriptionPurchases.add(purchase);
}
if (!subscribedToLiveUpdates && purchases.isLiveUpdatesSubscription(s)) {
subscribedToLiveUpdates = true;
}
if (!subscribedToOsmAndPro && purchases.isOsmAndProSubscription(s)) {
subscribedToOsmAndPro = true;
}
if (!subscribedToMaps && purchases.isMapsSubscription(s)) {
subscribedToMaps = true;
}
}
}
if (!subscribedToLiveUpdates && ctx.getSettings().LIVE_UPDATES_PURCHASED.get()) {
ctx.getSettings().LIVE_UPDATES_PURCHASED.set(false);
} else if (subscribedToLiveUpdates) {
ctx.getSettings().LIVE_UPDATES_PURCHASED.set(true);
}
if (!subscribedToOsmAndPro && ctx.getSettings().OSMAND_PRO_PURCHASED.get()) {
ctx.getSettings().OSMAND_PRO_PURCHASED.set(false);
} else if (subscribedToOsmAndPro) {
ctx.getSettings().OSMAND_PRO_PURCHASED.set(true);
}
if (!subscribedToMaps && ctx.getSettings().OSMAND_MAPS_PURCHASED.get()) {
ctx.getSettings().OSMAND_MAPS_PURCHASED.set(false);
} else if (subscribedToMaps) {
ctx.getSettings().OSMAND_MAPS_PURCHASED.set(true);
}
if (!subscribedToLiveUpdates && !subscribedToOsmAndPro && !subscribedToMaps) {
onSubscriptionExpired();
}
lastValidationCheckTime = System.currentTimeMillis();
logDebug("User " + (subscribedToLiveUpdates ? "HAS" : "DOES NOT HAVE") + " live updates purchased.");
logDebug("User " + (subscribedToOsmAndPro ? "HAS" : "DOES NOT HAVE") + " OsmAnd Pro purchased.");
logDebug("User " + (subscribedToMaps ? "HAS" : "DOES NOT HAVE") + " Maps purchased.");
OsmandSettings settings = ctx.getSettings();
settings.INAPPS_READ.set(true);
List<Purchase> tokensToSend = new ArrayList<>();
if (subscriptionPurchases.size() > 0) {
List<String> tokensSent = Arrays.asList(settings.BILLING_PURCHASE_TOKENS_SENT.get().split(";"));
for (Purchase purchase : subscriptionPurchases) {
if (needRestoreUserInfo()) {
restoreUserInfo(purchase);
}
if (!tokensSent.contains(purchase.getSku())) {
tokensToSend.add(purchase);
}
}
}
List<PurchaseInfo> purchaseInfoList = new ArrayList<>();
for (Purchase purchase : tokensToSend) {
purchaseInfoList.add(getPurchaseInfo(purchase));
}
onSkuDetailsResponseDone(purchaseInfoList, userRequested);
}
private void onSubscriptionExpired() {
if (!isDepthContoursPurchased(ctx)) {
ctx.getSettings().getCustomRenderBooleanProperty("depthContours").set(false);
}
}
};
}
use of com.android.billingclient.api.BillingResult in project Osmand by osmandapp.
the class BillingManager method handlePurchase.
/**
* Handles the purchase
* <p>Note: Notice that for each purchase, we check if signature is valid on the client.
* It's recommended to move this check into your backend.
* See {@link Security#verifyPurchase(String, String, String)}
* </p>
*
* @param purchase Purchase to be handled
*/
private void handlePurchase(final Purchase purchase) {
if (!verifyValidSignature(purchase.getOriginalJson(), purchase.getSignature())) {
LOG.info("Got a purchase: " + purchase + ", but signature is bad. Skipping...");
return;
}
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
// Acknowledge the purchase if it hasn't already been acknowledged.
if (!purchase.isAcknowledged()) {
AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
if (billingResult.getResponseCode() != BillingResponseCode.OK) {
LOG.info("Acknowledge a purchase: " + purchase + " failed (" + billingResult.getResponseCode() + "). " + billingResult.getDebugMessage());
}
}
});
}
} else if (purchase.getPurchaseState() == Purchase.PurchaseState.PENDING) {
LOG.info("Got a purchase: " + purchase + ", but purchase state is pending. Skipping...");
return;
} else {
LOG.info("Got a purchase: " + purchase + ", but purchase state is " + purchase.getPurchaseState() + ". Skipping...");
return;
}
LOG.debug("Got a verified purchase: " + purchase);
mPurchases.add(purchase);
}
use of com.android.billingclient.api.BillingResult in project Osmand by osmandapp.
the class BillingManager method querySkuDetailsAsync.
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 params = SkuDetailsParams.newBuilder().setSkusList(skuList).setType(itemType).build();
mBillingClient.querySkuDetailsAsync(params, new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {
listener.onSkuDetailsResponse(billingResult, skuDetailsList);
}
});
}
};
executeServiceRequest(queryRequest);
}
use of com.android.billingclient.api.BillingResult in project PhoneProfiles by henrichg.
the class DonationFragment method updateGUIAfterBillingConnected.
public void updateGUIAfterBillingConnected() {
// Start querying for SKUs
// PPApplication.logE(TAG, "handleManagerAndUiReady");
final List<String> inAppSkus = mBillingProvider.getBillingManager().getSkus(/*!mDebug, */
BillingClient.SkuType.INAPP);
mBillingProvider.getBillingManager().querySkuDetailsAsync(BillingClient.SkuType.INAPP, inAppSkus, new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(@NonNull BillingResult billingResult, List<SkuDetails> skuDetailsList) {
int responseCode = billingResult.getResponseCode();
// PPApplication.logE(TAG, "onSkuDetailsResponse responseCode="+responseCode);
String[] prices = new String[] { "1 €", "2 €", "3 €", "5 €", "8 €", "13 €", "20 €" };
// PPApplication.logE(TAG, "onSkuDetailsResponse skuDetailsList="+skuDetailsList.size());
if (responseCode == BillingClient.BillingResponseCode.OK && skuDetailsList != null) {
if (skuDetailsList.size() > 0) {
SKU_DETAILS = new ArrayList<>();
for (int i = 0; i < inAppSkus.size(); i++) {
for (int j = 0; j < skuDetailsList.size(); j++) {
if (skuDetailsList.get(j).getSku().equals(inAppSkus.get(i))) {
// PPApplication.logE(TAG, "Found sku: " + skuDetailsList.get(j));
SKU_DETAILS.add(skuDetailsList.get(j));
prices[i] = skuDetailsList.get(j).getPrice();
break;
}
}
}
// update the UI
displayAnErrorIfNeeded(BillingClient.BillingResponseCode.OK);
if (getActivity() != null) {
ArrayAdapter<CharSequence> adapter;
adapter = new ArrayAdapter<CharSequence>(getActivity(), R.layout.donation_spinner, prices);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mGoogleSpinner.setAdapter(adapter);
btGoogle.setEnabled(true);
}
} else {
displayAnErrorIfNeeded(BillingClient.BillingResponseCode.FEATURE_NOT_SUPPORTED);
}
}
}
});
}
Aggregations