/*==================== 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*/ StoreTransaction transaction) { SoomlaUtils.LogDebug(TAG, "TODO handleSuccessfulPurchase"); try { PurchasableVirtualItem pvi = StoreInfo.getPurchasableItem(transaction.ProductId); if (pvi == null) return; SoomlaUtils.LogDebug(TAG, "IabPurchase successful."); consumeIfConsumable(pvi, transaction); Dictionary<string, string> extra = new Dictionary<string, string>(); extra.Add("receipt", transaction.ReceiptXml); // Post event of general market purchase success BusProvider.Instance.Post(new MarketPurchaseEvent(pvi, null, extra)); pvi.give(1); // Post event of item purchase success with receipt as payload BusProvider.Instance.Post(new ItemPurchasedEvent(pvi, null)); } catch (VirtualItemNotFoundException e) { SoomlaUtils.LogError(TAG, "(handleSuccessfulPurchase - purchase or query-inventory) " + "ERROR : Couldn't find the " + " VirtualCurrencyPack OR MarketItem with productId: " + transaction.ReceiptXml + ". It's unexpected so an unexpected error is being emitted." + " " + e.Message); BusProvider.Instance.Post(new UnexpectedStoreErrorEvent("Couldn't find the productId " + "of a product after purchase or query-inventory." + " " + e.Message)); return; } }
/// <summary> Consumes a MANAGED product. </summary> /// /// <param name="productId"> Identifier for the product. </param> public async void Consume(StoreTransaction transaction) { SoomlaUtils.LogDebug(TAG, "WStorePlugin consume " + transaction.ProductId); try { Store.FulfillmentResult result = Store.FulfillmentResult.NothingToFulfill; if (StoreConfig.STORE_TEST_MODE) { result = await CurAppSimulator.ReportConsumableFulfillmentAsync(transaction.ProductId, transaction.TransactionId); } else { result = await CurApp.ReportConsumableFulfillmentAsync(transaction.ProductId, transaction.TransactionId); } if (result == Store.FulfillmentResult.Succeeded) pendingTransactions.Remove(transaction); } catch (InvalidOperationException e) { SoomlaUtils.LogDebug(TAG, e.Message); } }
/** * Consumes the given purchase, or writes error message to log if unable to consume * * @param purchase purchase to be consumed */ private void consumeIfConsumable(PurchasableVirtualItem pvi, StoreTransaction transaction) { try { if (!(pvi is SingleUseVG)) { if (pvi.GetPurchaseType() is PurchaseWithMarket) { PurchaseWithMarket pwm = (PurchaseWithMarket)pvi.GetPurchaseType(); string productId = pwm.getMarketItem().getProductId(); if (transaction.ProductId == productId) StoreManager.GetInstance().Consume(transaction); } } } catch (Exception e) { SoomlaUtils.LogDebug(TAG, "Error while consuming: itemId: " + pvi.getItemId()); BusProvider.Instance.Post(new UnexpectedStoreErrorEvent(e.Message)); } }
private async void DoPurchase(string productId) { try { bool licenceActiv = false; Store.PurchaseResults purchaseResults = null; if (StoreConfig.STORE_TEST_MODE) { SoomlaUtils.LogDebug(TAG, "DoPurchase test mode " + productId); purchaseResults = await CurAppSimulator.RequestProductPurchaseAsync(productId); licInfosMock = CurAppSimulator.LicenseInformation; licenceActiv = licInfosMock.ProductLicenses[productId].IsActive; } else { SoomlaUtils.LogDebug(TAG, "DoPurchase real store" + productId); purchaseResults = await CurApp.RequestProductPurchaseAsync(productId); licInfos = CurApp.LicenseInformation; licenceActiv = licInfos.ProductLicenses[productId].IsActive; } if (!licenceActiv || purchaseResults.Status == Store.ProductPurchaseStatus.NotPurchased || purchaseResults.Status == Store.ProductPurchaseStatus.AlreadyPurchased ) { SoomlaUtils.LogDebug(TAG,"Purchase cancelled " + productId); OnItemPurchaseCancelCB(productId, false); } // Purchase succeeded or has been pending fulfillment StoreTransaction transaction = new StoreTransaction() { TransactionId = purchaseResults.TransactionId, ProductId = productId, ReceiptXml = purchaseResults.ReceiptXml }; pendingTransactions.Add(transaction); OnItemPurchasedCB(transaction); } catch (Exception ex) { // When the user does not complete the purchase (e.g. cancels or navigates back from the Purchase Page), an exception with an HRESULT of E_FAIL is expected. SoomlaUtils.LogDebug(TAG,ex.Message); OnItemPurchaseCancelCB(productId, true); } }