In App Purchasing using Google BillingClient

nona bona 1 Reputation point
2021-05-13T17:23:38.517+00:00

Hi, I am having a hard time setting up In-app purchases in my Xamarin native android app.

I am using XFInAppBilling which uses the new Xamarin.Android.Google.BillingClient packages from Nuget.
XFInAppBilling also uses Plugin.CurrentActivity.

In my AndroidManifest I have (but i've read with the new 3.0 billing library its not required)

<uses-permission android:name="com.android.vending.BILLING" />

This is how my MainActivity.cs looks like:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    SetContentView(Resource.Layout.activity_main);
    CrossCurrentActivity.Current.Init(this, savedInstanceState);

    var toolbar = FindViewById<AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar);
    SetSupportActionBar(toolbar);

    XFInAppBillingImplementation xFInAppBilling = new XFInAppBillingImplementation();

    bool billingConnected = xFInAppBilling.ConnectAsync().Result;

 }

I have already published my APK to google console for internal testing. And I have an active in-app product listed on Google play console.

When I debug my app from visual studio on an actual device; in the ConnectAsync() method of XFInAppBillingImplementation.cs:

public async Task<bool> ConnectAsync()
{
    try
    {
        _tcsConnect = new TaskCompletionSource<bool>();

        BillingClient = BillingClient.NewBuilder(CurrentContext).EnablePendingPurchases().SetListener(this).Build();

        BillingClient.StartConnection(this);

        return await _tcsConnect?.Task;             //hangs here
    }
    catch (Exception ex)
    {

        throw ex;
    }
}

my app just hangs at the return await _tcsConnect?.Task line.

Looking for guidance to setting up iap.

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-05-30T12:08:57.553+00:00

    Hi
    I had a similar problem with another BillingClient framework.
    Mine was solved by adding await to the function call:

     bool billingConnected = await xFInAppBilling.ConnectAsync();
    

    Hope it helps!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.