English 中文(简体)
如何在 Maui App 建立 Google 游戏控制台订阅功能
原标题:How to set up google play console subscriptions in Maui app

我试图在我的 Maui 应用程序中实施 Google Play Billing 图书馆。 我需要在应用程序中设置订阅功能并提供代码。 根据这条线索 < href="https://learn.microsoft.com/ en-us/ questions/ Miisses/1347017/ im- curous- play- play- pay- fea" rel= "nofollow noreferrr" > 我上传一个AAB捆绑包与开帐单客户端首时, 就能解开游戏控制台的订阅功能了。 当我去订阅时, 它只是说 当我去订阅时会尝试和设置一个 APK 。 这里是我试图在Androd 目录内使用订阅经理级。 我做错了吗?

using System.Collections.Generic;
using System.Linq;
using Android.App;
using Android.Content;
using Android.BillingClient.Api;

public class SubscriptionManager
{
    private BillingClient billingClient;
    private Context context;
    private Action<bool> subscriptionCallback;

    public SubscriptionManager(Context context)
    {
        this.context = context;
        InitializeBillingClient();
    }

    private void InitializeBillingClient()
    {
        billingClient = BillingClient.NewBuilder(context)
            .EnablePendingPurchases()
            .SetListener(new PurchasesUpdateListener(this))
            .Build();

        billingClient.StartConnection(new SubscriptionConnectionListener(this));
    }

    private class SubscriptionConnectionListener : Java.Lang.Object, IBillingClientStateListener
    {
        private readonly SubscriptionManager subscriptionManager;

        public SubscriptionConnectionListener(SubscriptionManager subscriptionManager)
        {
            this.subscriptionManager = subscriptionManager;
        }

        public void OnBillingServiceDisconnected()
        {
            // Try to restart the connection on the next request to BillingClient
            subscriptionManager.InitializeBillingClient();
        }

        public void OnBillingSetupFinished(BillingResult billingResult)
        {
            if (billingResult.ResponseCode == BillingResponseCode.Ok)
            {
                // BillingClient is ready
            }
            else
            {
                // Handle setup failure
                // Example: Log error or show a message to the user
            }
        }
    }

    public void InitiateSubscriptionPurchase(string skuId, Action<bool> callback)
    {
        subscriptionCallback = callback;
        SkuDetailsParams.Builder paramsBuilder = SkuDetailsParams.NewBuilder();
        paramsBuilder.SetType(BillingClient.SkuType.Subs);

        List<string> skuList = new List<string> { skuId };
        paramsBuilder.SetSkusList(skuList);

        SkuDetailsParams skuDetailsParams = paramsBuilder.Build();

        billingClient.QuerySkuDetailsAsync(skuDetailsParams);
    }

    private void LaunchSubscriptionBillingFlow(SkuDetails skuDetails)
    {
        BillingFlowParams billingFlowParams = BillingFlowParams.NewBuilder()
            .SetSkuDetails(skuDetails)
            .Build();
        BillingResult result = billingClient.LaunchBillingFlow((Activity)context, billingFlowParams);
        if (result.ResponseCode != BillingResponseCode.Ok)
        {
         
            subscriptionCallback?.Invoke(false);
        }
    }

    private class SkuDetailsResponseListener : Java.Lang.Object, ISkuDetailsResponseListener
    {
        private readonly SubscriptionManager subscriptionManager;

        public SkuDetailsResponseListener(SubscriptionManager subscriptionManager)
        {
            this.subscriptionManager = subscriptionManager;
        }

        public void OnSkuDetailsResponse(BillingResult billingResult, IList<SkuDetails> skuDetailsList)
        {
            if (billingResult.ResponseCode == BillingResponseCode.Ok && skuDetailsList != null && skuDetailsList.Count > 0)
            {
                SkuDetails skuDetails = skuDetailsList.FirstOrDefault(); 
                subscriptionManager.LaunchSubscriptionBillingFlow(skuDetails);
            }
            else
            {
               
                subscriptionManager.subscriptionCallback?.Invoke(false);
            }
        }
    }

    internal class PurchasesUpdateListener : Java.Lang.Object, IPurchasesUpdatedListener
    {
        private readonly SubscriptionManager subscriptionManager;

        public PurchasesUpdateListener(SubscriptionManager subscriptionManager)
        {
            this.subscriptionManager = subscriptionManager;
        }

        public void OnPurchasesUpdated(BillingResult billingResult, IList<Purchase> purchases)
        {
            if (billingResult.ResponseCode == BillingResponseCode.Ok && purchases != null)
            {
                foreach (var purchase in purchases)
                {
                    // Handle each purchase
                    if (purchase.PurchaseState == PurchaseState.Purchased)
                    {
                        // Purchase was successful
                        subscriptionManager.subscriptionCallback?.Invoke(true);
                    }
                    else if (purchase.PurchaseState == PurchaseState.Pending)
                    {
                        // Purchase is pending, handle accordingly
                        // Example: Show a message to the user
                    }
                    else
                    {
                        // Purchase failed or was canceled
                        subscriptionManager.subscriptionCallback?.Invoke(false);
                    }
                }
            }
            else
            {
                // Handle purchase update failure
                subscriptionManager.subscriptionCallback?.Invoke(false);
            }
        }
    }
}
问题回答

您需要声明您的 Google 玩计费版本。 像这样

[MetaData(name: "com.google.android.play.billingclient.version", Value= "6.0.1")] public class MainActivity : MauiAppCompatActivity

publish than upload new version aab to google play close test.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签