首页 > 解决方案 > Xamarin 表单、Plugin.InAppBilling、Android:成功购买后出错

问题描述

在 Android 中成功购买后出现错误(Xamarin 表单、Plugin.InAppBilling)。但是购买是成功的,因为我可以在仪表板中看到收入。我的信用卡也被扣款了。

这是我的代码:

try
        {
            var productId = "";
            var payLoad = "";

            switch (Device.RuntimePlatform)
            {
                case Device.iOS:
                    productId = "removeAdd";
                    payLoad = "";
                    break;
                case Device.Android:
                    productId = "removeadd";
                    payLoad = "payLoad";
                    break;
            }

            CrossInAppBilling.Current.InTestingMode = true;

            var connected = await CrossInAppBilling.Current.ConnectAsync();

            if (!connected)
            {
                //Couldn't connect to billing, could be offline, alert user
                return;
            }

            //try to purchase item
            var purchase = await CrossInAppBilling.Current.PurchaseAsync(productId, ItemType.Subscription, payLoad);
            if (purchase == null)
            {
                //Not purchased, alert the user
            }
            else
            {
                //Purchased, save this information
                var id = purchase.Id;
                var state = purchase.State;

                Task task = AsyncTask();
            }
        }
        catch (InAppBillingPurchaseException purchaseEx)
        {
            //Billing Exception handle this based on the type
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            //Something bad has occurred, alert user
        }
        finally
        {
            //Disconnect, it is okay if we never connected
            await CrossInAppBilling.Current.DisconnectAsync();
        }

在 MainActivity.cs 中:

    protected override void OnCreate(Bundle savedInstanceState)
    {

        //inApp
        Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;

        LoadApplication(new App());
    }
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        InAppBillingImplementation.HandleActivityResult(requestCode, resultCode, data);
    }

购买变量为空,仅此而已。当我再次尝试购买时,我收到已购买的消息。它适用于IOS。

标签: androidxamarin.formsin-app-purchasecross-platform

解决方案


推荐阅读