Beispiel #1
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 override async 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);
            }
        }