private async void Connect()
        {
            try
            {
                if (!CrossInAppBilling.IsSupported)
                {
                    throw new Exception("The in app billing service is not available on your device");
                }
                bpMain = CrossInAppBilling.Current;
                //bpMain.InTestingMode = true;
                var connected = await bpMain.ConnectAsync();

                if (connected)
                {
                    OnBillingInitialized();
                }
                else
                {
                    OnError("Not connected");
                }
            }
            catch (Exception ex)
            {
                OnError(ex.Message);
            }
        }
Beispiel #2
0
        public static async Task <InAppBillingPurchase?> Buy(string productId, bool consume)
        {
            IInAppBilling billing = CrossInAppBilling.Current;

            try
            {
                if (!CrossInAppBilling.IsSupported || !await billing.ConnectAsync())
                {
                    return(null);
                }

                List <InAppBillingPurchase> existingPurchases = (await billing.GetPurchasesAsync(ItemType.InAppPurchase))
                                                                .Where(p => p.ProductId == productId)
                                                                .ToList();
                foreach (var existingPurchase in existingPurchases)
                {
                    await ProcessPurchase(billing, existingPurchase, consume);
                }

                InAppBillingPurchase purchase = await billing.PurchaseAsync(productId, ItemType.InAppPurchase);

                if (purchase != null && purchase.State == PurchaseState.Purchased)
                {
                    await ProcessPurchase(billing, purchase, consume);

                    return(purchase);
                }
            }
            catch (InAppBillingPurchaseException billingEx)
            {
                if (billingEx.PurchaseError != PurchaseError.UserCancelled &&
                    billingEx.PurchaseError != PurchaseError.ServiceUnavailable)
                {
                    billingEx.Data.Add(nameof(billingEx.PurchaseError), billingEx.PurchaseError);
                    billingEx.Data.Add("Product Id", productId);
                    ExceptionService.LogException(billingEx);
                }
            }
            catch (TaskCanceledException) { }
            catch (Exception ex)
            {
                ExceptionService.LogException(ex);
            }
            finally
            {
                // Disconnect, it is okay if we never connected, this will never throw an exception
                await billing.DisconnectAsync();
            }
            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// Consumes or Acknowledges the purchase based on consume parameter
        /// </summary>
        private static async Task ProcessPurchase(IInAppBilling billing, InAppBillingPurchase purchase, bool consume)
        {
            if (consume)
            {
                if (purchase.ConsumptionState == ConsumptionState.NoYetConsumed)
                {
                    await billing.ConsumePurchaseAsync(purchase.ProductId, purchase.PurchaseToken);
                }
                return;
            }

            if (purchase.IsAcknowledged == false)
            {
                await billing.AcknowledgePurchaseAsync(purchase.PurchaseToken);
            }
        }
Beispiel #4
0
 public PurchasingService(IInAppBilling billing, ILogger <PurchasingService> logger)
 {
     _logger  = logger;
     _billing = billing;
 }
        public async Task <bool> MakePurchase()
        {
            IInAppBilling billing = null;

            if (!CrossInAppBilling.IsSupported)
            {
                return(false);
            }

            try
            {
                billing = CrossInAppBilling.Current;
                var connected = await billing.ConnectAsync();

                if (!connected)
                {
                    return(false);
                }

                try
                {
                    var purchase = await CrossInAppBilling.Current.PurchaseAsync
                                   (
                        CoreSettings.Config.InAppPurchaseProductId,
                        ItemType.InAppPurchase,
                        "apppayload"
                                   );

                    if (purchase == null)
                    {
                        DialogPrompt.ShowMessage(new Prompt()
                        {
                            Title   = "Error",
                            Message = "The purchase failed and you will receive ads forever."
                        });

                        return(false);
                    }
                    else
                    {
                        //Purchased, save this information
                        var id    = purchase.Id;
                        var token = purchase.PurchaseToken;
                        var state = purchase.State;

                        await FileStore.SaveAsync <InAppBillingPurchase>("adsRemoved", purchase);

                        return(true);
                    }
                }
                catch (Exception ex)
                {
                    DialogPrompt.ShowMessage(new Prompt()
                    {
                        Title   = "Error",
                        Message = ex.Message
                    });
                    return(false);
                }
            }
            finally
            {
                await billing?.DisconnectAsync();
            }
        }