应用计费插件中的Xamarin无法正常工作

时间:2017-07-13 07:47:46

标签: xamarin xamarin.android in-app-purchase in-app-billing

我在app billing插件中遇到xamarin问题。我使用了几乎相同的代码,James Montemagno在他的博客中写了这篇文章=> https://blog.xamarin.com/integrating-in-app-purchases-in-mobile-apps/

我尝试了很多东西,但PurchaseAsync方法总是返回null。知道如何解决这个问题吗?提前谢谢。

       private  async void Btn_ClickAsync(object sender, System.EventArgs e)
    {
        RadioGroup radioGroup = FindViewById<RadioGroup>(Resource.Id.radioGroup1);
        RadioButton radioButton = FindViewById<RadioButton>(radioGroup.CheckedRadioButtonId);


        string selectedproduct =  radioButton.Tag.ToString();
        string a = editText1.Text, b = editText2.Text;
        try
        {
            var productId = selectedproduct;
            var connected = await CrossInAppBilling.Current.ConnectAsync();
            if (!connected)
            {
                //Couldn't connect to billing, could be offline, alert user

                new AlertDialog.Builder(this)
       .SetPositiveButton("OK", (sender2, args) =>{})
       .SetMessage("Couldn't connect to billing,")
       .SetTitle("Error")
       .Show();
                return;
            }

            //try to purchase item
            var purchase = await CrossInAppBilling.Current.PurchaseAsync(productId, ItemType.InAppPurchase,"apppayload");

            if (purchase == null)
            {
           //Not purchased, alert the user
                new AlertDialog.Builder(this)
            .SetPositiveButton("OK", (sender2, args) =>{ })
            .SetMessage("Not purchased")
            .SetTitle("Error")
            .Show();
                return;
            }
            else
            {
                //Purchased, save this information
                var id = purchase.Id;
                var token = purchase.PurchaseToken;
                var state = purchase.State;
            }
        }
        catch (InAppBillingPurchaseException purchaseEx)
        {
           Toast.MakeText(ApplicationContext, purchaseEx.InnerException.ToString(), ToastLength.Long).Show();
        }
        finally
        {
            //Disconnect, it is okay if we never connected
           await CrossInAppBilling.Current.DisconnectAsync();
        }
    }

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

2 个答案:

答案 0 :(得分:0)

你需要精确地遵循James Montemagno的指南。

https://jamesmontemagno.github.io/InAppBillingPlugin/GettingStarted.html

我已经完成了使用Xamarin及其库实现这一目标的整个过程。如果配置正确,一切正常。

我肯定错过了Android入门指南底部附近的一些内容,这让我感到困惑了一段时间。

我自己发现的一些事情:

  1. info.plist中的包标识符必须与itunesconnect包ID匹配才能在ios模拟器上查看产品列表
  2. 测试购买,让您选择的许可测试用户购买您的应用内商品,而不会给用户带来任何费用。测试购买仅可用于alpha / beta版本。即使应用程序采用alpha格式,Google也会向普通用户收取费用。

答案 1 :(得分:-1)

更新您的alpha apk文件,然后重试。您必须使用Play应用的商店版本。调试应用内购买很痛苦。