/** * 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(String productId, bool error) { SoomlaUtils.LogDebug(TAG, "TODO handleCancelledPurchase"); try { PurchasableVirtualItem v = StoreInfo.getPurchasableItem(productId); StoreEvents.GetInstance().PostMarketPurchaseCancelledEvent(v); } catch (VirtualItemNotFoundException e) { SoomlaUtils.LogError(TAG, "(purchaseActionResultCancelled) ERROR : Couldn't find the " + "VirtualCurrencyPack OR MarketItem with productId: " + productId + ". It's unexpected so an unexpected error is being emitted."); StoreEvents.GetInstance().PostUnexpectedStoreErrorEvent(e.Message); } }
/** * 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 payload A payload to get back when this purchase is finished. * @throws IllegalStateException */ public void buyWithMarket(MarketItem marketItem, String payload) { SoomlaUtils.LogDebug(TAG, "TODO buyWithMarket"); PurchasableVirtualItem pvi; try { pvi = StoreInfo.getPurchasableItem(marketItem.getProductId()); } catch (VirtualItemNotFoundException e) { String msg = "Couldn't find a purchasable item associated with: " + marketItem.getProductId() + " " + e.Message; SoomlaUtils.LogError(TAG, msg); StoreEvents.GetInstance().PostUnexpectedStoreErrorEvent(msg); return; } StoreEvents.GetInstance().PostMarketPurchaseStartedEvent(pvi); StoreManager.GetInstance().PurchaseProduct(marketItem.getProductId()); /* * 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); * } * * @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 " + * "NonConsumable). productId: " + sku); * * try { * PurchasableVirtualItem pvi = StoreInfo.getPurchasableItem(sku); * consumeIfConsumable(purchase, pvi); * * if (pvi instanceof NonConsumableItem) { * String message = "(alreadyOwned) the user tried to " + * "buy a NonConsumableItem that was already " + * "owned. itemId: " + pvi.getItemId() + * " productId: " + sku; * SoomlaUtils.LogDebug(TAG, message); * BusProvider.getInstance().post(new OnUnexpectedStoreErrorEvent(message)); * } * } catch (VirtualItemNotFoundException e) { * String message = "(alreadyOwned) ERROR : Couldn't find the " + "VirtualCurrencyPack with productId: " + sku + ". It's unexpected so an unexpected error is being emitted."; + SoomlaUtils.LogError(TAG, message); + BusProvider.getInstance().post(new OnUnexpectedStoreErrorEvent(message)); + } + } + + @Override + public void fail(String message) { + handleErrorResult(message); + } + }; + mInAppBillingService.launchPurchaseFlow(marketItem.getProductId(), + purchaseListener, payload); + BusProvider.getInstance().post(new OnMarketPurchaseStartedEvent(pvi)); + } + + @Override + public void fail(String message) { + reportIabInitFailure(message); + } + + }); */ }
/*==================== Common callbacks for success \ failure \ finish ====================*/ /** * 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*/ string productId) { SoomlaUtils.LogDebug(TAG, "TODO handleSuccessfulPurchase"); PurchasableVirtualItem pvi; try { pvi = StoreInfo.getPurchasableItem(productId); } catch (VirtualItemNotFoundException e) { SoomlaUtils.LogError(TAG, "(handleSuccessfulPurchase - purchase or query-inventory) " + "ERROR : Couldn't find the " + " VirtualCurrencyPack OR MarketItem with productId: " + productId + ". It's unexpected so an unexpected error is being emitted." + " " + e.Message); StoreEvents.GetInstance().PostUnexpectedStoreErrorEvent("Couldn't find the productId " + "of a product after purchase or query-inventory." + " " + e.Message); return; } SoomlaUtils.LogDebug(TAG, "IabPurchase successful."); // if the purchasable item is NonConsumableItem and it already exists then we // don't fire any events. // fixes: https://github.com/soomla/unity3d-store/issues/192 if (pvi is NonConsumableItem) { bool exists = StorageManager.getNonConsumableItemsStorage(). nonConsumableItemExists((NonConsumableItem)pvi); if (exists) { return; } } StoreEvents.GetInstance().PostMarketPurchaseEvent(pvi, null, null); pvi.give(1); StoreEvents.GetInstance().PostItemPurchasedEvent(pvi, null); consumeIfConsumable(pvi); /* * String sku = purchase.getSku(); * String developerPayload = purchase.getDeveloperPayload(); * String token = purchase.getToken(); * * 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 OnUnexpectedStoreErrorEvent("Couldn't find the sku " + "of a product after purchase or query-inventory.")); + return; + } + + switch (purchase.getPurchaseState()) { + case 0: + SoomlaUtils.LogDebug(TAG, "IabPurchase successful."); + + // if the purchasable item is NonConsumableItem and it already exists then we + // don't fire any events. + // fixes: https://github.com/soomla/unity3d-store/issues/192 + if (pvi instanceof NonConsumableItem) { + boolean exists = StorageManager.getNonConsumableItemsStorage(). + nonConsumableItemExists((NonConsumableItem) pvi); + if (exists) { + return; + } + } + + BusProvider.getInstance().post(new OnMarketPurchaseEvent + (pvi, developerPayload, token)); + pvi.give(1); + BusProvider.getInstance().post(new OnItemPurchasedEvent(pvi, developerPayload)); + + consumeIfConsumable(purchase, pvi); + + break; + + case 1: + + case 2: + SoomlaUtils.LogDebug(TAG, "IabPurchase refunded."); + if (!StoreConfig.friendlyRefunds) { + pvi.take(1); + } + BusProvider.getInstance().post(new MarketRefundEvent(pvi, developerPayload)); + break; + } */ }
/** * Queries the store for the details for all of the game's market items by product ids. * This operation will "fill" up the MarketItem objects with the information you provided in * the developer console including: localized price (as string), title and description. */ public void refreshMarketItemsDetails(Dictionary <string, MarketProductInfos> marketInfos) { SoomlaUtils.LogDebug(TAG, "TODO refreshMarketItemsDetails"); List <MarketItem> marketItems = new List <MarketItem>(); foreach (var mpi in marketInfos) { String productId = mpi.Value.ProductId; String title = mpi.Value.Name; String price = mpi.Value.FormattedPrice; String desc = mpi.Value.Description; try { PurchasableVirtualItem pvi = StoreInfo. getPurchasableItem(productId); MarketItem mi = ((PurchaseWithMarket) pvi.GetPurchaseType()).getMarketItem(); mi.setMarketTitle(title); mi.setMarketPrice(price); mi.setMarketDescription(desc); marketItems.Add(mi); } catch (VirtualItemNotFoundException e) { String msg = "(refreshInventory) Couldn't find a " + "purchasable item associated with: " + productId + " " + e.Message; SoomlaUtils.LogError(TAG, msg); } } StoreEvents.GetInstance().PostMarketItemsRefreshFinishedEvent(marketItems); /* * mInAppBillingService.initializeBillingService( * new IabCallbacks.IabInitListener() { * * @Override * public void success(boolean alreadyInBg) { * if (!alreadyInBg) { * notifyIabServiceStarted(); * } * SoomlaUtils.LogDebug(TAG, * "Setup successful, refreshing market items details"); * * IabCallbacks.OnFetchSkusDetailsListener fetchSkusDetailsListener = * new IabCallbacks.OnFetchSkusDetailsListener() { * * @Override * public void success(List<IabSkuDetails> skuDetails) { * SoomlaUtils.LogDebug(TAG, "Market items details refreshed"); * * List<MarketItem> marketItems = new ArrayList<MarketItem>(); * if (skuDetails.size() > 0) { * for (IabSkuDetails iabSkuDetails : skuDetails) { * String productId = iabSkuDetails.getSku(); * String price = iabSkuDetails.getPrice(); * String title = iabSkuDetails.getTitle(); * String desc = iabSkuDetails.getDescription(); * * SoomlaUtils.LogDebug(TAG, "Got item details: " + * "\ntitle:\t" + iabSkuDetails.getTitle() + * "\nprice:\t" + iabSkuDetails.getPrice() + * "\nproductId:\t" + iabSkuDetails.getSku() + * "\ndesc:\t" + iabSkuDetails.getDescription()); * * try { * PurchasableVirtualItem pvi = StoreInfo. * getPurchasableItem(productId); * MarketItem mi = ((PurchaseWithMarket) * pvi.getPurchaseType()).getMarketItem(); * mi.setMarketTitle(title); * mi.setMarketPrice(price); * mi.setMarketDescription(desc); * * marketItems.add(mi); * } catch (VirtualItemNotFoundException e) { * String msg = "(refreshInventory) Couldn't find a " + "purchasable item associated with: " + productId; + SoomlaUtils.LogError(TAG, msg); + } + } + } + BusProvider.getInstance().post(new OnMarketItemsRefreshFinishedEvent(marketItems)); + } + + @Override + public void fail(String message) { + + } + }; + + final List<String> purchasableProductIds = StoreInfo.getAllProductIds(); + mInAppBillingService.fetchSkusDetailsAsync(purchasableProductIds, fetchSkusDetailsListener); + + BusProvider.getInstance().post(new OnMarketItemsRefreshStartedEvent()); + } + + @Override + public void fail(String message) { + reportIabInitFailure(message); + } + } + ); */ }