是否可以从Xamarin Android应用程序直接使用Google Play BillingClient?

时间:2018-07-08 16:54:26

标签: android xamarin xamarin.android google-play subscriptions

我正在努力将订阅集成到Xamarin Android应用中。我可以在网上找到所有针对Xamarin的示例,这些示例都使用了Montemagno等人的Plugin.InAppBilling

这当然非常方便,但是出于适当的考虑,我想看看直接使用BillingClient类需要做什么,如Google文档中所述(例如,在这里:{ {3}}。

奇怪的是,Xamarin文档显示了如何直接使用iOS计费类,而不是Android计费类。我以为Xamarin.GooglePlayServices NuGet软件包可能包含我需要的软件包,但是我没有看到似乎与计费直接相关的软件包。

任何指针将不胜感激。

2 个答案:

答案 0 :(得分:4)

TL; DR:是的,您可以...(我用它...)

“实施Google Play结算”文档中显示的类是包装器类,这些包装器最终会使用结算AIDL。该AIDL接口是实际的计费API,并且AIDL定义了对其的IPC调用(这不仅用于跨进程方法调用,而且还用于安全性)...

Android Studio通过Gradle自动导入计费包装器和AIDL,并在将计费api依赖项添加到build.gradle文件时为您进行设置。 (当然,这不是通过Visual Studio和MSBuild的选择...)

Montemagno的Android应用程序内计费直接使用AIDL接口方法,并以跨平台方式包装这些方法...

要使用Java包装器类,基本步骤是从Jcenter Maven存储库中获取aar(billing-1.1.aar),创建一个Xamarin.Android绑定库,添加aar,修复警告(和名称空间)并在您的Xamarin.Android应用程序项目中引用该库...

现在,您可以直接使用带有一些较小的Xamarin.Android/C#规范化的Google文档,即:

// I did not alter the original Java namespace in the binding lib
using Com.Android.Billingclient.Api; 

~~~

var billingClient = BillingClient
    .NewBuilder(this)
    .SetListener(this)
    .Build();

~~~

var flowParams = BillingFlowParams
    .NewBuilder()
    .SetSku("StackOverflowXamarinTag")
    .SetType("Answer")
    .Build();

答案 1 :(得分:0)

使用 Android.BillingClient.Api.BillingClient; https://www.nuget.org/packages/Xamarin.Android.Google.BillingClient/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.BillingClient.Api;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using static Android.BillingClient.Api.BillingClient;

namespace Android.Basic.Core
{
    public  class BilingSupport : Java.Lang.Object, IPurchasesUpdatedListener, IBillingClientStateListener, ISkuDetailsResponseListener, IConsumeResponseListener
    {
        Context Context;
        public IList<SkuDetails> SkuDetails;
        Android.BillingClient.Api.BillingClient billingClient;
        private List<string> skuList = new List<string>() { "get_5_coins", "get_10_coins" };
       
        public BilingSupport(Context context)
        {
            this.Context = context;
            billingClient = Android.BillingClient.Api.BillingClient.NewBuilder(this.Context).SetListener(this).Build();
            billingClient.StartConnection(this);
        }
       
        public void LoadPurchases()
        {

            if (billingClient.IsReady)
            {
                var paramse = SkuDetailsParams.NewBuilder().SetSkusList(skuList).SetType(Android.BillingClient.Api.BillingClient.SkuType.Inapp).Build();
                billingClient.QuerySkuDetails(paramse, this);
            }
        }
        public void PurchaseNow(SkuDetails skuDetails)
        {
            var billingFlowParams = BillingFlowParams.NewBuilder().SetSkuDetails(skuDetails).Build();
            billingClient.LaunchBillingFlow(this.Context as Activity, billingFlowParams);
        }
        

        public void OnBillingServiceDisconnected()
        {
            Console.WriteLine("BILLING | onBillingServiceDisconnected | DISCONNECTED");
        }

        public void OnBillingSetupFinished(BillingResult p0)
        {
            if (p0.ResponseCode == BillingResponseCode.Ok)
            {

                Console.WriteLine("BILLING | startConnection | RESULT OK");

            }
            else
            {
                Console.WriteLine("BILLING | startConnection | RESULT: $billingResponseCode");
            }
        }
        //Response code 7 in OnPurchasesUpdated.It means: Item Already Owned.
        public void OnPurchasesUpdated(BillingResult p0, IList<Purchase> p1)
        {
            
        }
        public void OnSkuDetailsResponse(BillingResult p0, IList<SkuDetails> p1)
        {
            if (p0.ResponseCode == BillingResponseCode.Ok)
            {
                Console.WriteLine("querySkuDetailsAsync, responseCode: $responseCode");
                InitProductAdapter(p1);

            }
            else
            {
                Console.WriteLine("Can't querySkuDetailsAsync, responseCode: $responseCode");
            }
        }
        public event EventHandler<IList<SkuDetails>> SkuDetailsLoaded;
        public void InitProductAdapter(IList<SkuDetails> skuDetails)
        {
            this.SkuDetails = skuDetails;
            this.SkuDetailsLoaded(this, this.SkuDetails);
        }
        public void ClearOrConsumeAllPurchases()
        {
            
            var querylist = billingClient.QueryPurchases(Android.BillingClient.Api.BillingClient.SkuType.Inapp).PurchasesList;
            foreach (var query in querylist)
            {
                var consumeParams = ConsumeParams.NewBuilder().SetPurchaseToken(query.PurchaseToken).Build();
                billingClient.Consume(consumeParams, this);
            }        

        }

        public void OnConsumeResponse(BillingResult p0, string p1)
        {
            if (p0.ResponseCode == BillingResponseCode.Ok && p1 != null)
            {
                Console.WriteLine("onPurchases Updated consumeAsync, purchases token removed: $purchaseToken");
            }
            else
            {
                Console.WriteLine("onPurchases some troubles happened: $responseCode");
            }
        }

       
    }
}