C# Xamarin Forms InAppBilling PurchaseAsync not returning after handle of playstore

lb 1 Reputation point
2020-12-22T23:05:41.493+00:00

I am using jamesmontenegro InAppBilling and I have a small issue regarding payments.

The code is working fine but after the payment is handled by Google Play Store, the app doesn't handle the procedure and doesn't return if the purchase was successfully leading to not being able to consume the purchased item.

Here is the simple code
public static async Task<bool> PurchaseItem(string productId, string payload)
{
var billing = CrossInAppBilling.Current;
try
{
var connected = await billing.ConnectAsync(ItemType.InAppPurchase);
if (!connected)
{
//we are offline or can't connect, don't try to purchase
return false;
}

                //check purchases
                var purchase = await billing.PurchaseAsync(productId, ItemType.InAppPurchase, payload);

                //possibility that a null came through.
                if (purchase == null)
                {
                    //did not purchase
                    Debug.WriteLine("purchased but not consumed");
                    return true;
                }
                else if (purchase.State == PurchaseState.Purchased)
                {
                    //purchased, we can now consume the item or do it later

                    //If we are on iOS we are done, else try to consume the purchase
                    //Device.RuntimePlatform comes from Xamarin.Forms, you can also use a conditional flag or the DeviceInfo plugin
                    if (Device.RuntimePlatform == Device.iOS)
                        return true;

                    var consumedItem = await CrossInAppBilling.Current.ConsumePurchaseAsync(purchase.ProductId, purchase.PurchaseToken);

                    if (consumedItem != null)
                    {
                        //Consumed!!
                        Debug.WriteLine("purchased and consumed");
                        return true;
                    }
                }
                return false;
            }

Nothing is actually running after PurchaseAsync is called as the thread changes to Google Play Store. Once the payment is finished and the thread goes back to the app it is not possible to handle anything.

Any ideas? There is a workaround for this but I am just wondering if I am doing something wrong here?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,656 questions
0 comments No comments
{count} votes