Xamarin形成InAppBillingPlugin PurchaseAsync示例

时间:2018-08-06 18:28:11

标签: xamarin xamarin.forms

我需要有关Xamarin.Forms的InAppBillingPlugin的支持。首先,我根据文档记录进行了配置:https://jamesmontemagno.github.io/InAppBillingPlugin/

应用程序,Google Console(被管理产品)和iTunes Connect网站。

这是我的应用程序代码。

private async Task TryInAppPurchace(string androidProductId, string iOSProductId="")
{
    if (!CrossInAppBilling.IsSupported)
        return;

    //Android
    var products = new string[] { androidProductId };

    if(Device.RuntimePlatform == Device.iOS)
        products = new string[] { iOSProductId };

    var billing = CrossInAppBilling.Current;
    //billing.InTestingMode = true;

    try
    {
        var connected = await billing.ConnectAsync(ItemType.InAppPurchase);

        if (!connected)
        {
            await Application.Current.MainPage.DisplayAlert("Connectivity Error", "Unable to connect to the internet.", "OK");
            return;
        }

        var items = await billing.GetProductInfoAsync(ItemType.InAppPurchase, products);
        var item = items.ToList().First();

        //try to purchase item
        var purchase = await billing.PurchaseAsync(item.ProductId, ItemType.InAppPurchase, item.ProductId);

        //possibility that a null came through.
        if (purchase == null)
        {
            await Application.Current.MainPage.DisplayAlert("Purchase Error", "You are unable to buy this product.", "OK");
            return;
        }
        else
        {
            await _pageService.DisplayAlert("Purchased", "Consumable product.", "OK");

            if (Device.RuntimePlatform == Device.iOS)
                return;

            var consumedItem = await CrossInAppBilling.Current.ConsumePurchaseAsync(purchase.ProductId, purchase.PurchaseToken);

            if (consumedItem != null)
            {
                await Application.Current.MainPage.DisplayAlert("New Package added", "Successfully added new package", "OK");
            }
        }
    }
    catch (InAppBillingPurchaseException purchaseEx)
    {
        var message = string.Empty;
        await Application.Current.MainPage.DisplayAlert("Exeption", purchaseEx.Message, "OK");
        switch (purchaseEx.PurchaseError)
        {
            case PurchaseError.BillingUnavailable:
                message = "BillingUnavailable";
                break;
            case PurchaseError.DeveloperError:
                message = "DeveloperError";
                break;
            case PurchaseError.ItemUnavailable:
                message = "ItemUnavailable";
                break;
            case PurchaseError.GeneralError:
                message = "GeneralError";
                break;
            case PurchaseError.UserCancelled:
                message = "UserCancelled.";
                break;
            case PurchaseError.AppStoreUnavailable:
                message = "AppStoreUnavailable.";
                break;
            case PurchaseError.PaymentNotAllowed:
                message = "PaymentNotAllowed.";
                break;
            case PurchaseError.PaymentInvalid:
                message = "PaymentInvalid";
                break;
            case PurchaseError.InvalidProduct:
                message = "InvalidProduct";
                break;
            case PurchaseError.ProductRequestFailed:
                message = "ProductRequestFailed";
                break;
            case PurchaseError.RestoreFailed:
                message = "RestoreFailed";
                break;
            case PurchaseError.ServiceUnavailable:
                message = "ServiceUnavailable";
                break;
        }
    }
    catch (Exception ex)
    {
        //Something else has gone wrong, log it
        await Application.Current.MainPage.DisplayAlert("Exeption", ex.Message, "OK");
    }
    finally
    {
        await billing.DisconnectAsync();
    }
}

我的问题是我无法再次购买相同的产品,并且正在从Google Play中获取收益:

  

{Plugin.InAppBilling.Abstractions.InAppBillingPurchaseException:   无法处理购买。在   Plugin.InAppBilling.InAppBillingImplementation + d__33.MoveNext   ()[0x00116] in   C:\ projects \ inappbillingplugin \ src \ Plugin.InAppBilling.Android \ InAppBillingImplementation.cs:324

     

---从上一个引发异常的位置开始的堆栈结束---   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   [0x0000c] in:0 at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess   (System.Threading.Tasks.Task任务)[0x0003e]在   :0处   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification   (System.Threading.Tasks.Task任务)在[0x00028]中   :0处   System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd   (System.Threading.Tasks.Task任务)在[0x00008]中   :0处   System.Runtime.CompilerServices.TaskAwaiter`1 [TResult] .GetResult()   [0x00000]在:0处   Plugin.InAppBilling.InAppBillingImplementation + d__32.MoveNext   ()[0x00090] in   C:\ projects \ inappbillingplugin \ src \ Plugin.InAppBilling.Android \ InAppBillingImplementation.cs:264

     

---从上一个引发异常的位置开始的堆栈结束---   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   [0x0000c] in:0 at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess   (System.Threading.Tasks.Task任务)[0x0003e]在   :0处   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification   (System.Threading.Tasks.Task任务)在[0x00028]中   :0处   System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd   (System.Threading.Tasks.Task任务)在[0x00008]中   :0处   System.Runtime.CompilerServices.TaskAwaiter`1 [TResult] .GetResult()   [0x00000]在:0处   DrivingCourse_app.ViewModels.AccessPageViewModel + d__18.MoveNext   ()[0x0025c] in   C:\ Projects \ GIT-REPO \ DrivingCourse_DE \ DrivingCourse_app \ DrivingCourse_app \ DrivingCourse_app \ ViewModels \ AccessPageViewModel.cs:136   }

我的代码有问题吗?该代码适用于每一次购买该产品的产品,因此配置本身和一般过程都可以使用。

有什么想法吗?

0 个答案:

没有答案