Xamarin InAppBilling订阅

时间:2015-04-09 12:41:16

标签: android xamarin in-app-billing subscription

我正在尝试实施InAppBilling(Android)订阅。我尝试使用component(Xamarin.InAppBilling)。我可以购买订阅但当我试图通过订阅获得购买项目时,没有任何回报给我。您是如何实施IAB(Android)订阅的?有人举个例子吗?

[EDITED]

public class PurchaseManager : IPlayListener
{
    MLActivity2 _act;
    BillingService m_service;
    int m_requestId = 1;

    //String _productId = "subscription.test";

    //atributos IAB
    String _publicKey = "";
    String _productId = "";
    public bool _premium;

    public PurchaseManager(MLActivity2 act)
    {
        _act = act;
        IniciarPurchase();
    }

    public void OnActivityResult (int requestCode, Result resultCode, Intent data)
    {
        m_service.HandleActivityResult(requestCode, (int)resultCode, data);
    }

    public void Finalizar()
    {
        if (m_service != null)
            m_service.Dispose();
    }

    //Process referentes a IAB
    private void IniciarPurchase ()
    {
        Security.ExpectSignature = false;
        m_service = new BillingService(_act, this, _publicKey);
        var connTask = m_service.Connect();
        //Load inventory on start-up
        connTask.ContinueWith(t =>
            {
                if (t.Result)
                {
                    VerificarAssinatura();
                }
            });
    }

    public void Connected()
    {           
    }

    public void Disconnected()
    {
    }

    private void VerificarAssinatura()
    {
        //Existing purchases
        m_service.SendRequest<GetPurchasesResponse>(new GetPurchases(play.billing.v3.Consts.ITEM_TYPE_SUBS, m_requestId++)).ContinueWith(tGP =>
            {
                _act.RunOnUiThread(() =>
                    {
                        if (tGP.Result.Success)
                        {
                            foreach (Purchase item in tGP.Result.PurchasedItems) 
                                MLActivity2.UiShowToast(_act, "", "Order id" + item.OrderId, true);

                            if (tGP.Result.PurchasedItems.Count == 0)
                                MLActivity2.UiShowToast(_act, "", "nao tem item", true);
                        }
                        else
                            MLActivity2.UiShowToast(_act, _act.GetString(Resource.String.msg_erro), "Purchases request failure. Error: " + tGP.Result.Message, false);
                    });
            });
        //Existing purchases
        m_service.SendRequest<GetPurchasesResponse>(new GetPurchases(play.billing.v3.Consts.ITEM_TYPE_INAPP, m_requestId++)).ContinueWith(tGP =>
            {
                _act.RunOnUiThread(() =>
                    {
                        if (tGP.Result.Success)
                        {
                            foreach (Purchase item in tGP.Result.PurchasedItems) 
                                MLActivity2.UiShowToast(_act, "", "Order id" + item.OrderId, true);

                            if (tGP.Result.PurchasedItems.Count == 0)
                                MLActivity2.UiShowToast(_act, "", "nao tem item", true);
                        }
                        else
                            MLActivity2.UiShowToast(_act, _act.GetString(Resource.String.msg_erro), "2222Purchases request failure. Error: " + tGP.Result.Message, false);
                    });
            });
    }

    public void JaPossuiAssinatura()
    {
        MLActivity2.UiShowToast(_act, _act.GetString(Resource.String.assinatura_japossui_titulo), _act.GetString(Resource.String.assinatura_japossui_texto), true);
    }

    public void Comprar ()
    {
        var req = new Buy(_productId, m_requestId++);

        m_service.SendRequest<Response>(req).ContinueWith(t =>
            {
                _act.RunOnUiThread(() =>
                    {
                        if (t.Result.Success)
                            VerificarAssinatura();
                        else
                            MLActivity2.UiShowToast(_act, _act.GetString(Resource.String.msg_erro), "Purchases request failure. Error: " + t.Result.Message, false);
                    });
            }); 
    }

    void CompraComSucesso()
    {
        MLActivity2.UiShowToast(_act, _act.GetString(Resource.String.assinatura_sucesso_titulo), _act.GetString(Resource.String.assinatura_sucesso_texto), true);
    }

    void ErroNaCompra()
    {
        //MLActivity2.UiShowToast(_act, _act.GetString(Resource.String.msg_erro), _act.GetString(Resource.String.assinatura_problema), false);
    }
}

1 个答案:

答案 0 :(得分:0)

添加代码段始终有助于更快地获得正确答案。下次不要忘记;)

当您查询购买时,请确保使用正确的项目类型:订阅(而不是产品)。如果你有两个,你必须做两个查询 - 一个用于产品,一个用于订阅。