public IObservable <int> BuyProduct(Product product) { return(Observable.Create <int>(o => { InAppBillingHandler.OnProductPurchasedDelegate purchaseCompleted = (response, purchase) => { o.OnNext(response); o.OnCompleted(); }; this.serviceConnection.BillingHandler.OnProductPurchased += purchaseCompleted; this.serviceConnection.BillingHandler.BuyProduct(product); return () => this.serviceConnection.BillingHandler.OnProductPurchased -= purchaseCompleted; })); }
/// <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()); }