Search in sources :

Example 6 with VirtualItemNotFoundException

use of com.soomla.store.exceptions.VirtualItemNotFoundException in project android-store by soomla.

the class SoomlaStore method handleVerificationStarted.

/**
     * Posts an event about the start of verification for the purchase, or an unexpected
     * error event if the item was not found.
     *
     * @param purchases List of purchases to handle.
     */
private void handleVerificationStarted(List<IabPurchase> purchases) {
    for (IabPurchase purchase : purchases) {
        String sku = purchase.getSku();
        try {
            PurchasableVirtualItem v = StoreInfo.getPurchasableItem(sku);
            BusProvider.getInstance().post(new VerificationStartedEvent(v));
        } catch (VirtualItemNotFoundException e) {
            SoomlaUtils.LogError(TAG, "(purchaseActionResultCancelled) ERROR : Couldn't find the " + "VirtualCurrencyPack OR MarketItem  with productId: " + sku + ". It's unexpected so an unexpected error is being emitted.");
            BusProvider.getInstance().post(new UnexpectedStoreErrorEvent());
        }
    }
}
Also used : PurchasableVirtualItem(com.soomla.store.domain.PurchasableVirtualItem) VirtualItemNotFoundException(com.soomla.store.exceptions.VirtualItemNotFoundException) VerificationStartedEvent(com.soomla.store.events.VerificationStartedEvent) UnexpectedStoreErrorEvent(com.soomla.store.events.UnexpectedStoreErrorEvent) IabPurchase(com.soomla.store.billing.IabPurchase)

Example 7 with VirtualItemNotFoundException

use of com.soomla.store.exceptions.VirtualItemNotFoundException in project android-store by soomla.

the class SoomlaStore method restoreTransactions.

/**
     * Restoring old purchases for the current user (device).
     */
public void restoreTransactions() {
    if (mInAppBillingService == null) {
        SoomlaUtils.LogError(TAG, "Billing service is not loaded. Can't invoke restoreTransactions.");
        return;
    }
    mInAppBillingService.initializeBillingService(new IabCallbacks.IabInitListener() {

        @Override
        public void success(boolean alreadyInBg) {
            if (!alreadyInBg) {
                notifyIabServiceStarted();
            }
            SoomlaUtils.LogDebug(TAG, "Setup successful, restoring purchases");
            IabCallbacks.OnRestorePurchasesListener restorePurchasesListener = new IabCallbacks.OnRestorePurchasesListener() {

                @Override
                public void success(List<IabPurchase> purchases) {
                    SoomlaUtils.LogDebug(TAG, "Transactions restored");
                    if (purchases.size() > 0) {
                        if (SoomlaConfig.logDebug) {
                            String ownedSkus = "";
                            for (IabPurchase purchase : purchases) {
                                ownedSkus += purchase.getSku() + " / ";
                            }
                            SoomlaUtils.LogDebug(TAG, "Got owned items: " + ownedSkus);
                        }
                        handleSuccessfulPurchases(purchases, true, new HandleSuccessfulPurchasesFinishedHandler() {

                            @Override
                            public void onFinished() {
                                // Restore transactions always finished successfully even if
                                // something wrong happened when handling a specific item.
                                BusProvider.getInstance().post(new RestoreTransactionsFinishedEvent(true));
                            }
                        });
                    } else {
                        BusProvider.getInstance().post(new RestoreTransactionsFinishedEvent(true));
                    }
                }

                @Override
                public void fail(String message) {
                    BusProvider.getInstance().post(new RestoreTransactionsFinishedEvent(false));
                    handleErrorResult(UnexpectedStoreErrorEvent.ErrorCode.GENERAL, message);
                }

                @Override
                public void verificationStarted(List<IabPurchase> purchases) {
                    handleVerificationStarted(purchases);
                }
            };
            IabCallbacks.OnRestorePurchasesListener restoreSubscriptionsListener = new IabCallbacks.OnRestorePurchasesListener() {

                @Override
                public void success(List<IabPurchase> purchases) {
                    // collect subscription ids list
                    List<String> subscriptionIds = new ArrayList<String>();
                    for (IabPurchase purchase : purchases) {
                        subscriptionIds.add(purchase.getSku());
                    }
                    // collect subscriptionVG list
                    List<VirtualGood> subscriptions = new ArrayList<VirtualGood>();
                    for (VirtualGood good : StoreInfo.getGoods()) {
                        if ((good.getPurchaseType() instanceof PurchaseWithMarket) && ((PurchaseWithMarket) good.getPurchaseType()).isSubscription()) {
                            subscriptions.add(good);
                        }
                    }
                    // give unset subscriptions and take expired
                    for (VirtualGood subscription : subscriptions) {
                        String productId = ((PurchaseWithMarket) subscription.getPurchaseType()).getMarketItem().getProductId();
                        if (subscriptionIds.contains(productId)) {
                            // TODO: is here should be 1 to give? Maybe current item has not only just 0/1 state
                            subscription.give(1, false);
                        } else {
                            try {
                                subscription.take(StoreInventory.getVirtualItemBalance(subscription.getItemId()), false);
                            } catch (VirtualItemNotFoundException ex) {
                            // possibly unreachable block
                            }
                        }
                    }
                // TODO: Should we notify user about repaired or expired subscriptions?
                }

                @Override
                public void fail(String message) {
                    SoomlaUtils.LogDebug(TAG, "Subscriptions restoring failed: " + message);
                }

                @Override
                public void verificationStarted(List<IabPurchase> purchases) {
                // should we do it in subscription restoring? possibly it should be empty
                }
            };
            try {
                mInAppBillingService.restorePurchasesAsync(restorePurchasesListener);
            } catch (IllegalStateException ex) {
                SoomlaUtils.LogError(TAG, "Can't proceed with restorePurchases. error: " + ex.getMessage());
                restorePurchasesListener.fail("Can't proceed with restorePurchases. error: " + ex.getMessage());
            }
        }

        @Override
        public void fail(String message) {
            reportIabInitFailure(message);
        }
    });
}
Also used : VirtualGood(com.soomla.store.domain.virtualGoods.VirtualGood) VirtualItemNotFoundException(com.soomla.store.exceptions.VirtualItemNotFoundException) IabCallbacks(com.soomla.store.billing.IabCallbacks) IabPurchase(com.soomla.store.billing.IabPurchase) ArrayList(java.util.ArrayList) List(java.util.List) PurchaseWithMarket(com.soomla.store.purchaseTypes.PurchaseWithMarket) RestoreTransactionsFinishedEvent(com.soomla.store.events.RestoreTransactionsFinishedEvent)

Example 8 with VirtualItemNotFoundException

use of com.soomla.store.exceptions.VirtualItemNotFoundException in project android-store by soomla.

the class SoomlaStore method buyWithMarket.

/**
     * Starts a purchase process in the market.
     *
     * @param marketItem The item to purchase - this item has to be defined EXACTLY the same in
     *                   the market
     * @param isSubscription determines if subscription is purchasing
     * @param payload A payload to get back when this purchase is finished.
     * @throws IllegalStateException
     */
public void buyWithMarket(final MarketItem marketItem, final boolean isSubscription, final String payload) throws IllegalStateException {
    if (mInAppBillingService == null) {
        SoomlaUtils.LogError(TAG, "Billing service is not loaded. Can't invoke buyWithMarket.");
        return;
    }
    final PurchasableVirtualItem pvi;
    try {
        pvi = StoreInfo.getPurchasableItem(marketItem.getProductId());
    } catch (VirtualItemNotFoundException e) {
        SoomlaUtils.LogError(TAG, "Couldn't find a purchasable item associated with: " + marketItem.getProductId());
        BusProvider.getInstance().post(new UnexpectedStoreErrorEvent(UnexpectedStoreErrorEvent.ErrorCode.PURCHASE_FAIL));
        return;
    }
    mInAppBillingService.initializeBillingService(new IabCallbacks.IabInitListener() {

        @Override
        public void success(boolean alreadyInBg) {
            if (!alreadyInBg) {
                notifyIabServiceStarted();
            }
            IabCallbacks.OnPurchaseListener purchaseListener = new IabCallbacks.OnPurchaseListener() {

                @Override
                public void success(IabPurchase purchase) {
                    handleSuccessfulPurchase(purchase, false);
                }

                @Override
                public void cancelled(IabPurchase purchase) {
                    handleCancelledPurchase(purchase);
                }

                @Override
                public void alreadyOwned(IabPurchase purchase) {
                    String sku = purchase.getSku();
                    SoomlaUtils.LogDebug(TAG, "Tried to buy an item that was not" + " consumed (maybe it's an already owned " + "non-consumable). productId: " + sku);
                    try {
                        PurchasableVirtualItem pvi = StoreInfo.getPurchasableItem(sku);
                        consumeIfConsumable(purchase, pvi);
                        if (StoreInfo.isItemNonConsumable(pvi)) {
                            SoomlaUtils.LogDebug(TAG, "(alreadyOwned) the user tried to " + "buy a non-consumable that was already " + "owned. itemId: " + pvi.getItemId() + "    productId: " + sku);
                            BusProvider.getInstance().post(new UnexpectedStoreErrorEvent(UnexpectedStoreErrorEvent.ErrorCode.PURCHASE_FAIL));
                        }
                    } catch (VirtualItemNotFoundException e) {
                        SoomlaUtils.LogError(TAG, "(alreadyOwned) ERROR : Couldn't find the " + "VirtualCurrencyPack with productId: " + sku + ". It's unexpected so an unexpected error is being emitted.");
                        BusProvider.getInstance().post(new UnexpectedStoreErrorEvent(UnexpectedStoreErrorEvent.ErrorCode.PURCHASE_FAIL));
                    }
                }

                @Override
                public void fail(String message) {
                    handleErrorResult(UnexpectedStoreErrorEvent.ErrorCode.PURCHASE_FAIL, message);
                }

                @Override
                public void verificationStarted(List<IabPurchase> purchases) {
                    handleVerificationStarted(purchases);
                }
            };
            BusProvider.getInstance().post(new MarketPurchaseStartedEvent(pvi, getInAppBillingService().shouldVerifyPurchases()));
            try {
                if (isSubscription) {
                    mInAppBillingService.launchPurchaseFlow(IabHelper.ITEM_TYPE_SUBS, marketItem.getProductId(), purchaseListener, payload);
                } else {
                    mInAppBillingService.launchPurchaseFlow(IabHelper.ITEM_TYPE_INAPP, marketItem.getProductId(), purchaseListener, payload);
                }
            } catch (IllegalStateException ex) {
                SoomlaUtils.LogError(TAG, "Can't proceed with launchPurchaseFlow. error: " + ex.getMessage());
                purchaseListener.fail("Can't proceed with launchPurchaseFlow. error: " + ex.getMessage());
            }
        }

        @Override
        public void fail(String message) {
            reportIabInitFailure(message);
        }
    });
}
Also used : PurchasableVirtualItem(com.soomla.store.domain.PurchasableVirtualItem) VirtualItemNotFoundException(com.soomla.store.exceptions.VirtualItemNotFoundException) IabCallbacks(com.soomla.store.billing.IabCallbacks) UnexpectedStoreErrorEvent(com.soomla.store.events.UnexpectedStoreErrorEvent) IabPurchase(com.soomla.store.billing.IabPurchase) MarketPurchaseStartedEvent(com.soomla.store.events.MarketPurchaseStartedEvent)

Example 9 with VirtualItemNotFoundException

use of com.soomla.store.exceptions.VirtualItemNotFoundException in project android-store by soomla.

the class SoomlaStore method handleCancelledPurchase.

/**
     * Handles a cancelled purchase by either posting an event containing a
     * <code>PurchasableVirtualItem</code> corresponding to the given purchase, or an unexpected
     * error event if the item was not found.
     *
     * @param purchase cancelled purchase to handle.
     */
private void handleCancelledPurchase(IabPurchase purchase) {
    String sku = purchase.getSku();
    try {
        PurchasableVirtualItem v = StoreInfo.getPurchasableItem(sku);
        BusProvider.getInstance().post(new MarketPurchaseCancelledEvent(v));
    } catch (VirtualItemNotFoundException e) {
        SoomlaUtils.LogError(TAG, "(purchaseActionResultCancelled) ERROR : Couldn't find the " + "VirtualCurrencyPack OR MarketItem  with productId: " + sku + ". It's unexpected so an unexpected error is being emitted.");
        BusProvider.getInstance().post(new UnexpectedStoreErrorEvent());
    }
}
Also used : PurchasableVirtualItem(com.soomla.store.domain.PurchasableVirtualItem) VirtualItemNotFoundException(com.soomla.store.exceptions.VirtualItemNotFoundException) MarketPurchaseCancelledEvent(com.soomla.store.events.MarketPurchaseCancelledEvent) UnexpectedStoreErrorEvent(com.soomla.store.events.UnexpectedStoreErrorEvent)

Example 10 with VirtualItemNotFoundException

use of com.soomla.store.exceptions.VirtualItemNotFoundException in project android-store by soomla.

the class StoreInventory method getGoodCurrentUpgrade.

/**
     * Retrieves the itemId of the current upgrade of the virtual good with the given
     * <code>goodItemId</code>.
     *
     * @param goodItemId id of the virtual good whose upgrade id we want to know
     * @return upgrade id if exists, or empty string otherwise
     * @throws VirtualItemNotFoundException
     */
public static String getGoodCurrentUpgrade(String goodItemId) throws VirtualItemNotFoundException {
    VirtualGood good = (VirtualGood) StoreInfo.getVirtualItem(goodItemId);
    String upgradeVGItemId = StorageManager.getVirtualGoodsStorage().getCurrentUpgrade(good.getItemId());
    UpgradeVG upgradeVG = null;
    try {
        upgradeVG = (UpgradeVG) StoreInfo.getVirtualItem(upgradeVGItemId);
    } catch (VirtualItemNotFoundException e) {
        SoomlaUtils.LogDebug("SOOMLA StoreInventory", "This is BAD! Can't find the current upgrade (" + upgradeVGItemId + ") of: " + good.getItemId());
    }
    if (upgradeVG == null) {
        return "";
    }
    return upgradeVG.getItemId();
}
Also used : VirtualGood(com.soomla.store.domain.virtualGoods.VirtualGood) UpgradeVG(com.soomla.store.domain.virtualGoods.UpgradeVG) VirtualItemNotFoundException(com.soomla.store.exceptions.VirtualItemNotFoundException)

Aggregations

VirtualItemNotFoundException (com.soomla.store.exceptions.VirtualItemNotFoundException)13 PurchasableVirtualItem (com.soomla.store.domain.PurchasableVirtualItem)5 UpgradeVG (com.soomla.store.domain.virtualGoods.UpgradeVG)5 VirtualGood (com.soomla.store.domain.virtualGoods.VirtualGood)5 UnexpectedStoreErrorEvent (com.soomla.store.events.UnexpectedStoreErrorEvent)4 IabPurchase (com.soomla.store.billing.IabPurchase)3 TextView (android.widget.TextView)2 IabCallbacks (com.soomla.store.billing.IabCallbacks)2 VirtualItem (com.soomla.store.domain.VirtualItem)2 InsufficientFundsException (com.soomla.store.exceptions.InsufficientFundsException)2 SharedPreferences (android.content.SharedPreferences)1 Typeface (android.graphics.Typeface)1 ListView (android.widget.ListView)1 IStoreAssets (com.soomla.store.IStoreAssets)1 GooglePlayIabService (com.soomla.store.billing.google.GooglePlayIabService)1 VirtualItemStorage (com.soomla.store.data.VirtualItemStorage)1 VirtualCurrency (com.soomla.store.domain.virtualCurrencies.VirtualCurrency)1 EquippableVG (com.soomla.store.domain.virtualGoods.EquippableVG)1 GoodUpgradeEvent (com.soomla.store.events.GoodUpgradeEvent)1 ItemPurchaseStartedEvent (com.soomla.store.events.ItemPurchaseStartedEvent)1