Xamarin forms in app purchase

Saada Rym 21 Reputation points
2021-12-08T03:11:38.373+00:00

Hi

I am trying to implement in app purchase inside my Xamarin app using James NuGet Plugin.InAppBilling

https://jamesmontemagno.github.io/InAppBillingPlugin/GettingStarted.html

here is the code I am using

    public async Task<bool> PurchaseSubscription(string productId, string payload)
    {
        var billing = CrossInAppBilling.Current;
        try
        {
            var connected = await billing.ConnectAsync();
            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);

            //possibility that a null came through.
            if (purchase == null)
            {
                //did not purchase
            }
            else
            {
                DependencyService.Get<IFileService>().WriteFile(productId+","+"true");
                //purchased!
                if (Device.RuntimePlatform == Device.Android)
                {
                   await billing.AcknowledgePurchaseAsync(purchase.PurchaseToken);
                    // Must call AcknowledgePurchaseAsync else the purchase will be refunded
                }
            }
        }
        catch (InAppBillingPurchaseException purchaseEx)
        {
            //Billing Exception handle this based on the type
            Debug.WriteLine("Error: " + purchaseEx);
        }
        catch (Exception ex)
        {
            //Something else has gone wrong, log it
            Debug.WriteLine("Issue connecting: " + ex);
        }
        finally
        {
            await billing.DisconnectAsync();
        }
        return true;
    }

But the app is throwing the following error on the 1st line : var billing = CrossInAppBilling.Current;

System.NotImplementedException: 'This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.'

the nuget package Plugin.InAppBilling version 4.0.2 is installed in my App + App.Android and App.iOS

Can someone already implemented the in app purchase in Xamarin application please let me know if I missed some initializations or point me to an example that already implemented this functionality using Plugin.InAppBilling

Thx for your help
Best regards

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,262 questions
{count} votes