Search in sources :

Example 1 with PurchasableVirtualItem

use of com.soomla.store.domain.PurchasableVirtualItem in project android-store by soomla.

the class SoomlaStore method handleSuccessfulPurchase.

/**
 * Checks the state of the purchase and responds accordingly, giving the user an item,
 * throwing an error, or taking the item away and paying the user back.
 *
 * @param purchase purchase whose state is to be checked.
 */
private void handleSuccessfulPurchase(IabPurchase purchase, boolean isRestoring) {
    String sku = purchase.getSku();
    PurchasableVirtualItem pvi;
    try {
        pvi = StoreInfo.getPurchasableItem(sku);
    } catch (VirtualItemNotFoundException e) {
        SoomlaUtils.LogError(TAG, "(handleSuccessfulPurchase - purchase or query-inventory) " + "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(UnexpectedStoreErrorEvent.ErrorCode.PURCHASE_FAIL));
        return;
    }
    switch(purchase.getPurchaseState()) {
        case 0:
            {
                if (purchase.isServerVerified()) {
                    this.finalizeTransaction(purchase, pvi, isRestoring);
                } else {
                    BusProvider.getInstance().post(new UnexpectedStoreErrorEvent(purchase.getVerificationErrorCode() != null ? purchase.getVerificationErrorCode() : UnexpectedStoreErrorEvent.ErrorCode.GENERAL));
                }
                break;
            }
        case 1:
        case 2:
            SoomlaUtils.LogDebug(TAG, "IabPurchase refunded.");
            if (!StoreConfig.friendlyRefunds) {
                pvi.take(1);
            }
            BusProvider.getInstance().post(new MarketRefundEvent(pvi, purchase.getDeveloperPayload()));
            break;
    }
}
Also used : PurchasableVirtualItem(com.soomla.store.domain.PurchasableVirtualItem) VirtualItemNotFoundException(com.soomla.store.exceptions.VirtualItemNotFoundException) MarketRefundEvent(com.soomla.store.events.MarketRefundEvent) UnexpectedStoreErrorEvent(com.soomla.store.events.UnexpectedStoreErrorEvent)

Example 2 with PurchasableVirtualItem

use of com.soomla.store.domain.PurchasableVirtualItem in project android-store by soomla.

the class StoreInfo method initializeWithStoreAssets.

/**
 * Initializes from <code>IStoreAssets</code>.
 * This happens only once - when the game is loaded for the first time.
 *
 * @param storeAssets game economy
 */
private static void initializeWithStoreAssets(IStoreAssets storeAssets) {
    // fall-back here if the json doesn't exist,
    // we load the store from the given {@link IStoreAssets}.
    mCurrencies = new LinkedList<VirtualCurrency>(Arrays.asList(storeAssets.getCurrencies()));
    mCurrencyPacks = new LinkedList<VirtualCurrencyPack>(Arrays.asList(storeAssets.getCurrencyPacks()));
    mGoods = new LinkedList<VirtualGood>(Arrays.asList(storeAssets.getGoods()));
    mCategories = new LinkedList<VirtualCategory>(Arrays.asList(storeAssets.getCategories()));
    mVirtualItems = new HashMap<String, VirtualItem>();
    mPurchasableItems = new HashMap<String, PurchasableVirtualItem>();
    mGoodsCategories = new HashMap<String, VirtualCategory>();
    mGoodsUpgrades = new HashMap<String, List<UpgradeVG>>();
    for (VirtualCurrency vi : mCurrencies) {
        mVirtualItems.put(vi.getItemId(), vi);
    }
    for (VirtualCurrencyPack vi : mCurrencyPacks) {
        mVirtualItems.put(vi.getItemId(), vi);
        PurchaseType purchaseType = vi.getPurchaseType();
        if (purchaseType instanceof PurchaseWithMarket) {
            mPurchasableItems.put(((PurchaseWithMarket) purchaseType).getMarketItem().getProductId(), vi);
        }
    }
    for (VirtualGood vi : mGoods) {
        mVirtualItems.put(vi.getItemId(), vi);
        if (vi instanceof UpgradeVG) {
            List<UpgradeVG> upgrades = mGoodsUpgrades.get(((UpgradeVG) vi).getGoodItemId());
            if (upgrades == null) {
                upgrades = new ArrayList<UpgradeVG>();
                mGoodsUpgrades.put(((UpgradeVG) vi).getGoodItemId(), upgrades);
            }
            upgrades.add((UpgradeVG) vi);
        }
        PurchaseType purchaseType = vi.getPurchaseType();
        if (purchaseType instanceof PurchaseWithMarket) {
            mPurchasableItems.put(((PurchaseWithMarket) purchaseType).getMarketItem().getProductId(), vi);
        }
    }
    for (VirtualCategory category : mCategories) {
        for (String goodItemId : category.getGoodsItemIds()) {
            mGoodsCategories.put(goodItemId, category);
        }
    }
    // Remove this code when no longer needed.
    if (mNonConsumableMigrationNeeded) {
        SoomlaUtils.LogDebug(TAG, "NonConsumables balance migration is required. Doing it now.");
        nonConsBalancesToLTVGs();
    }
    save();
}
Also used : VirtualGood(com.soomla.store.domain.virtualGoods.VirtualGood) PurchasableVirtualItem(com.soomla.store.domain.PurchasableVirtualItem) UpgradeVG(com.soomla.store.domain.virtualGoods.UpgradeVG) PurchaseType(com.soomla.store.purchaseTypes.PurchaseType) VirtualCurrencyPack(com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack) VirtualCategory(com.soomla.store.domain.VirtualCategory) VirtualItem(com.soomla.store.domain.VirtualItem) PurchaseWithVirtualItem(com.soomla.store.purchaseTypes.PurchaseWithVirtualItem) PurchasableVirtualItem(com.soomla.store.domain.PurchasableVirtualItem) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) PurchaseWithMarket(com.soomla.store.purchaseTypes.PurchaseWithMarket) VirtualCurrency(com.soomla.store.domain.virtualCurrencies.VirtualCurrency)

Example 3 with PurchasableVirtualItem

use of com.soomla.store.domain.PurchasableVirtualItem 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 4 with PurchasableVirtualItem

use of com.soomla.store.domain.PurchasableVirtualItem 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 5 with PurchasableVirtualItem

use of com.soomla.store.domain.PurchasableVirtualItem 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)

Aggregations

PurchasableVirtualItem (com.soomla.store.domain.PurchasableVirtualItem)8 UnexpectedStoreErrorEvent (com.soomla.store.events.UnexpectedStoreErrorEvent)4 VirtualItemNotFoundException (com.soomla.store.exceptions.VirtualItemNotFoundException)4 PurchaseWithMarket (com.soomla.store.purchaseTypes.PurchaseWithMarket)3 IabPurchase (com.soomla.store.billing.IabPurchase)2 VirtualCategory (com.soomla.store.domain.VirtualCategory)2 VirtualItem (com.soomla.store.domain.VirtualItem)2 VirtualCurrency (com.soomla.store.domain.virtualCurrencies.VirtualCurrency)2 VirtualCurrencyPack (com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack)2 UpgradeVG (com.soomla.store.domain.virtualGoods.UpgradeVG)2 VirtualGood (com.soomla.store.domain.virtualGoods.VirtualGood)2 PurchaseType (com.soomla.store.purchaseTypes.PurchaseType)2 PurchaseWithVirtualItem (com.soomla.store.purchaseTypes.PurchaseWithVirtualItem)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 IabCallbacks (com.soomla.store.billing.IabCallbacks)1 EquippableVG (com.soomla.store.domain.virtualGoods.EquippableVG)1 LifetimeVG (com.soomla.store.domain.virtualGoods.LifetimeVG)1 SingleUsePackVG (com.soomla.store.domain.virtualGoods.SingleUsePackVG)1