Exemplo n.º 1
0
        /// <summary>
        /// Purchase a specific product or subscription
        /// </summary>
        /// <param name="productId">Sku or ID of product</param>
        /// <param name="itemType">Type of product being requested</param>
        /// <param name="payload">Developer specific payload</param>
        /// <param name="verifyPurchase">Verify purchase implementation</param>
        /// <returns></returns>
        /// <exception cref="InAppBillingPurchaseException">If an error occurs during processing</exception>
        public async override Task <InAppBillingPurchase> PurchaseAsync(string productId, ItemType itemType, IInAppBillingVerifyPurchase verifyPurchase = null)
        {
            // Get purchase result from store or simulator
            var purchaseResult = await CurrentAppMock.RequestProductPurchaseAsync(InTestingMode, productId);


            if (purchaseResult == null)
            {
                return(null);
            }

            if (string.IsNullOrWhiteSpace(purchaseResult.ReceiptXml))
            {
                return(null);
            }

            // Transform it to InAppBillingPurchase
            return(purchaseResult.ReceiptXml.ToInAppBillingPurchase(purchaseResult.Status).FirstOrDefault());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Consume a purchase
        /// </summary>
        /// <param name="productId">Id/Sku of the product</param>
        /// <param name="payload">Developer specific payload of original purchase</param>
        /// <param name="itemType">Type of product being consumed.</param>
        /// <param name="verifyPurchase">Verify Purchase implementation</param>
        /// <returns>If consumed successful</returns>
        /// <exception cref="InAppBillingPurchaseException">If an error occures during processing</exception>
        public async override Task <InAppBillingPurchase> ConsumePurchaseAsync(string productId, ItemType itemType, string payload, IInAppBillingVerifyPurchase verifyPurchase = null)
        {
            var items = await CurrentAppMock.GetAvailableConsumables(InTestingMode);

            var consumable = items.FirstOrDefault(i => i.ProductId == productId);

            if (consumable == null)
            {
                throw new InAppBillingPurchaseException(PurchaseError.ItemUnavailable);
            }

            var result = await CurrentAppMock.ReportConsumableFulfillmentAsync(InTestingMode, productId, consumable.TransactionId);

            switch (result)
            {
            case FulfillmentResult.ServerError:
                throw new InAppBillingPurchaseException(PurchaseError.GeneralError);

            case FulfillmentResult.NothingToFulfill:
                throw new InAppBillingPurchaseException(PurchaseError.ItemUnavailable);

            case FulfillmentResult.PurchasePending:
            case FulfillmentResult.PurchaseReverted:
                throw new InAppBillingPurchaseException(PurchaseError.GeneralError);

            case FulfillmentResult.Succeeded:
                return(new InAppBillingPurchase
                {
                    AutoRenewing = false,
                    Id = consumable.TransactionId.ToString(),
                    Payload = payload,
                    ProductId = consumable.ProductId,
                    PurchaseToken = consumable.TransactionId.ToString(),
                    State = PurchaseState.Purchased,
                    TransactionDateUtc = DateTime.UtcNow
                });

            default:
                return(null);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets product information
        /// </summary>
        /// <param name="itemType">Type of item</param>
        /// <param name="productIds">Product Ids</param>
        /// <returns></returns>
        public async override Task <IEnumerable <InAppBillingProduct> > GetProductInfoAsync(ItemType itemType, params string[] productIds)
        {
            // Get list of products from store or simulator
            var listingInformation = await CurrentAppMock.LoadListingInformationAsync(InTestingMode);

            var products = new List <InAppBillingProduct>();

            foreach (var productId in productIds)
            {
                // Check if requested product exists
                if (!listingInformation.ProductListings.ContainsKey(productId))
                {
                    continue;
                }

                // Get product and transform it to an InAppBillingProduct
                var product = listingInformation.ProductListings[productId];

                products.Add(new InAppBillingProduct
                {
                    Name           = product.Name,
                    Description    = product.Description,
                    ProductId      = product.ProductId,
                    LocalizedPrice = product.FormattedPrice,
                    WindowsExtras  = new InAppBillingProductWindowsExtras
                    {
                        FormattedBasePrice = product.FormattedBasePrice,
                        ImageUri           = product.ImageUri,
                        IsOnSale           = product.IsOnSale,
                        SaleEndDate        = product.SaleEndDate,
                        Tag          = product.Tag,
                        IsConsumable = product.ProductType == ProductType.Consumable,
                        IsDurable    = product.ProductType == ProductType.Durable,
                        Keywords     = product.Keywords
                    }
                    //CurrencyCode = product.CurrencyCode // Does not work at the moment, as UWP throws an InvalidCastException when getting CurrencyCode
                });
            }

            return(products);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Consume a purchase with a purchase token.
        /// </summary>
        /// <param name="productId">Id or Sku of product</param>
        /// <param name="purchaseToken">Original Purchase Token</param>
        /// <returns>If consumed successful</returns>
        /// <exception cref="InAppBillingPurchaseException">If an error occures during processing</exception>
        public async override Task <bool> ConsumePurchaseAsync(string productId, string purchaseToken)
        {
            var result = await CurrentAppMock.ReportConsumableFulfillmentAsync(InTestingMode, productId, new Guid(purchaseToken));

            switch (result)
            {
            case FulfillmentResult.ServerError:
                throw new InAppBillingPurchaseException(PurchaseError.AppStoreUnavailable);

            case FulfillmentResult.NothingToFulfill:
                throw new InAppBillingPurchaseException(PurchaseError.ItemUnavailable);

            case FulfillmentResult.PurchasePending:
            case FulfillmentResult.PurchaseReverted:
                throw new InAppBillingPurchaseException(PurchaseError.GeneralError);

            case FulfillmentResult.Succeeded:
                return(true);

            default:
                return(false);
            }
        }