Exemple #1
0
        /// <summary>
        /// Gets a list of all purchased products.
        /// </summary>
        /// <param name="itemType">
        /// The type of product to retrieve. See the <see cref="ItemType"/> class for a list of types.
        /// </param>
        /// <returns>A list of purchased products.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="itemType"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="itemType"/> is not <see cref="ItemType.Product"/> or <see cref="ItemType.Subscription"/>.
        /// </exception>
        /// <exception cref="InAppBillingException">
        /// (asynchronous) Retrieving the purchases failed.
        /// </exception>
        public Task <IReadOnlyList <Purchase> > GetPurchases(string itemType)
        {
            if (itemType == null)
            {
                throw new ArgumentNullException(nameof(itemType));
            }

            if (itemType != ItemType.Product && itemType != ItemType.Subscription)
            {
                throw new ArgumentException("Invalid item type.");
            }

            return(Observable.Create <IReadOnlyList <Purchase> >(o =>
            {
                InAppBillingHandler.InAppBillingProcessingErrorDelegate errorDelegate = message =>
                {
                    o.OnError(new InAppBillingException($"Getting purchases failed: {message}"));
                };

                this.serviceConnection.BillingHandler.InAppBillingProcesingError += errorDelegate;

                IList <Purchase> purchases = this.serviceConnection.BillingHandler.GetPurchases(itemType);

                o.OnNext(purchases == null ? new List <Purchase>() : purchases.ToList());
                o.OnCompleted();

                return () => this.serviceConnection.BillingHandler.InAppBillingProcesingError -= errorDelegate;
            }).ToTask());
        }
Exemple #2
0
        /// <summary>
        /// Consumes the specified <see cref="Purchase"/>.
        /// </summary>
        /// <param name="purchase">The <see cref="Purchase"/> to consume.</param>
        /// <exception cref="ArgumentNullException"><paramref name="purchase"/> is <c>null</c>.</exception>
        /// <exception cref="InAppBillingException">
        /// The <see cref="Purchase"/> couldn't be consumed.
        /// </exception>
        public Task ConsumePurchase(Purchase purchase)
        {
            if (purchase == null)
            {
                throw new ArgumentNullException(nameof(purchase));
            }

            return(Observable.Create <Unit>(o =>
            {
                InAppBillingHandler.OnPurchaseConsumedDelegate consumedDelegate = token =>
                {
                    o.OnNext(Unit.Default);
                    o.OnCompleted();
                };

                InAppBillingHandler.OnPurchaseConsumedErrorDelegate consumedErrorDelegate = (responseCode, token) =>
                {
                    o.OnError(new InAppBillingException("Failed to consume purchase"));
                };

                InAppBillingHandler.InAppBillingProcessingErrorDelegate errorDelegate = message =>
                {
                    o.OnError(new InAppBillingException($"Failed to consume purchase: {message}"));
                };

                this.serviceConnection.BillingHandler.OnPurchaseConsumed += consumedDelegate;
                this.serviceConnection.BillingHandler.OnPurchaseConsumedError += consumedErrorDelegate;
                this.serviceConnection.BillingHandler.InAppBillingProcesingError += errorDelegate;

                this.serviceConnection.BillingHandler.ConsumePurchase(purchase);

                return () =>
                {
                    this.serviceConnection.BillingHandler.OnPurchaseConsumed -= consumedDelegate;
                    this.serviceConnection.BillingHandler.OnPurchaseConsumedError -= consumedErrorDelegate;
                    this.serviceConnection.BillingHandler.InAppBillingProcesingError -= errorDelegate;
                };
            }).ToTask());
        }
Exemple #3
0
        /// <summary>
        /// Buys the specified <see cref="Product"/>
        /// </summary>
        /// <param name="product">The product to buy.</param>
        /// <returns>
        /// A future with the result of the operation.
        ///
        /// The result maps to a value in the <see cref="BillingResult"/> class.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="product"/> is <c>null</c></exception>
        public Task <PurchaseResult> BuyProduct(Product product)
        {
            if (product == null)
            {
                throw new ArgumentNullException(nameof(product));
            }

            return(Observable.Create <PurchaseResult>(o =>
            {
                InAppBillingHandler.OnProductPurchasedDelegate purchaseCompleted = (response, purchase, data, signature) =>
                {
                    var res = new PurchaseResult();
                    res.Result = response;
                    res.Purchase = purchase;
                    res.Data = data;
                    res.Signature = signature;

                    o.OnNext(res);
                    o.OnCompleted();
                };

                InAppBillingHandler.OnProductPurchaseErrorDelegate purchaseError = (response, sku) =>
                {
                    var res = new PurchaseResult();
                    res.Result = response;
                    res.Sku = sku;

                    o.OnNext(res);
                    o.OnCompleted();
                };

                InAppBillingHandler.BuyProductErrorDelegate errorDelegate = (code, sku) =>
                {
                    var res = new PurchaseResult();
                    res.Result = code;
                    res.Sku = sku;

                    o.OnNext(res);
                    o.OnCompleted();
                };

                InAppBillingHandler.InAppBillingProcessingErrorDelegate errorDelegate2 = message =>
                {
                    var res = new PurchaseResult();
                    res.Result = BillingResult.Error;

                    o.OnNext(res);
                    o.OnCompleted();
                };

                InAppBillingHandler.OnUserCanceledDelegate canceledDelegate = () =>
                {
                    var res = new PurchaseResult();
                    res.Result = BillingResult.UserCancelled;

                    o.OnNext(res);
                    o.OnCompleted();
                };

                InAppBillingHandler.OnPurchaseFailedValidationDelegate validationErrorDelegate = (purchase, data, signature) =>
                {
                    var res = new PurchaseResult();
                    res.Result = BillingResult.Error;

                    o.OnNext(res);
                    o.OnCompleted();
                };

                this.serviceConnection.BillingHandler.OnProductPurchased += purchaseCompleted;
                this.serviceConnection.BillingHandler.OnProductPurchasedError += purchaseError;
                this.serviceConnection.BillingHandler.BuyProductError += errorDelegate;
                this.serviceConnection.BillingHandler.InAppBillingProcesingError += errorDelegate2;
                this.serviceConnection.BillingHandler.OnUserCanceled += canceledDelegate;
                this.serviceConnection.BillingHandler.OnPurchaseFailedValidation += validationErrorDelegate;

                this.serviceConnection.BillingHandler.BuyProduct(product);

                return () =>
                {
                    this.serviceConnection.BillingHandler.OnProductPurchased -= purchaseCompleted;
                    this.serviceConnection.BillingHandler.OnProductPurchasedError -= purchaseError;
                    this.serviceConnection.BillingHandler.BuyProductError -= errorDelegate;
                    this.serviceConnection.BillingHandler.InAppBillingProcesingError -= errorDelegate2;
                    this.serviceConnection.BillingHandler.OnUserCanceled -= canceledDelegate;
                    this.serviceConnection.BillingHandler.OnPurchaseFailedValidation -= validationErrorDelegate;
                };
            }).ToTask());
        }