In App Billing

JenalynParagadosJenalynParagados USMember ✭✭
edited July 2016 in Xamarin.Android

Hi,

I have implemented In App Billing and tested it in my device but I don't know what happened that my app will not respond and then it closes the App. I could not figure out what's the error since I use my device to test it and no return logs. What are the things should I modify in my codes? And how will I detect the errors or bugs during testing in my device? Is there a way to know the return logs while testing in the device? Thank you very much for the help.

Here are the codes which I have followed from the API and in the forums:

var iapService = DependencyService.Get<IIAPService>();
string[] productsId = { "search_product", "search_product1", "search_product2" };
var product = iapService.GetProducts(productsId);
await DisplayAlert("Number of Product", product.Result.Count().ToString(), "Cancel");

var purchase = iapService.Purchase("search_product2");
await DisplayAlert("Purchase", purchase.Result.PaymentId, "Cancel");


public interface IIAPService
{
    Task<IEnumerable<InAppProduct>> GetProducts(string[] productsIds);
        Task<NativePurchaseResult> Purchase(string productId);
}

//IAPService

public async Task<IEnumerable<InAppProduct>> GetProducts(string[] productsIds)
        {
            if (!isConnected)
                await Task.Run(async () =>
                {
                    int retryCount = 0;
                    Connect();
                    do
                    {
                        await Task.Delay(250);
                        retryCount++;
                        if (retryCount == 120)
                            throw new Exception();
                    } while (!isConnected);

                });
            var _appProductsResponse = await _serviceConnection.BillingHandler.QueryInventoryAsync(productsIds, ItemType.Product);

            if (_appProductsResponse == null)
                return null;

            var iapProducts = new List<InAppProduct>();

            foreach (var googleProd in _appProductsResponse)
            {
                var prod = new InAppProduct
                {
                    SKU = googleProd.ProductId,
                    Name = googleProd.Title,
                    Description = googleProd.Description,
                    Price = Convert.ToInt32(googleProd.Price)

                };

                // Remove Google stupid app name added to IAP products
                prod.Name = System.Text.RegularExpressions.Regex.Replace(prod.Name, @" ?\(.*?\)", string.Empty);

                iapProducts.Add(prod);

            }
            return iapProducts;
        }

//Purchase

public async Task<NativePurchaseResult> Purchase(string productId)
        {
            NativePurchaseResult nativePurchase = null;

            await Task.Run(async () =>
            {
                if (!isConnected)
                {
                    Connect();
                    do
                    {
                        Task.Delay(250).Wait();
                    } while (!isConnected);
                }
                _purchaseTask = new TaskCompletionSource<NativePurchaseResult>();

                _serviceConnection.BillingHandler.OnProductPurchased += BillingHandler_OnProductPurchased;

                _serviceConnection.BillingHandler.OnProductPurchasedError += BillingHandler_OnProductPurchasedError;

                _serviceConnection.BillingHandler.OnPurchaseConsumedError += BillingHandler_OnPurchaseConsumedError;

                _serviceConnection.BillingHandler.OnUserCanceled += () =>
                {
                    if (_purchaseTask != null)
                        _purchaseTask.TrySetCanceled();
                };
                string token = LoadPurchasedItems(productId);
                if (token != null || token != string.Empty)
                {
                    _serviceConnection.BillingHandler.ConsumePurchase(token);
                }

                _serviceConnection.BillingHandler.BuyProduct(productId, ItemType.Subscription, Guid.NewGuid().ToString("N"));
                nativePurchase = await _purchaseTask.Task;
            });
            return nativePurchase;
        }

private string LoadPurchasedItems(string productId)
        {
            try
            {
                // Ask the open connection's billing handler to get any purchases
                var purchases = _serviceConnection.BillingHandler.GetPurchases(ItemType.Subscription);
                if (purchases != null)
                {
                    foreach (var item in purchases)
                    {
                        if (productId == item.ProductId)
                        {
                            return item.PurchaseToken;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }

            return null;
        }

Best Regards,
Jen

Answers

  • JenalynParagadosJenalynParagados USMember ✭✭

    It was all set up the in developer console with the In App Products. And I have tried testing to buy a product but I need to add the consumePurchases so that the product can be bought many times.

Sign In or Register to comment.