So it’s supposed to be pretty easy to implement the code. Yet, despite all the documentation, after several days of trying things, I still can’t get the sample code provided for the InAppBilling plugin to work. I’m really lost, so would greatly appreciate any help from anyone who’s successfully used the plugin (or would be kind enough to try installing the plugin and giving the code a try!).
Here’s the code I have so far, adapted from the sample code provided, and cleaned to be easier to read. NotificationMessageBox is a custom function that displays a message box with the desired text. I am using a shared project, and am just building the iOS side for now:
public async void MakePurchase()
{
if (!CrossInAppBilling.IsSupported)
{
NotificationMessageBox("CrossInAppBilling not supported");
return;
}
var billing = CrossInAppBilling.Current;
try
{
var connected = await billing.ConnectAsync();
if (!connected)
NotificationMessageBox("Not Connected");
else
{
var purchase = await billing.PurchaseAsync("productID", ItemType.InAppPurchase, "payload");
//where productID is the com.website.product format for the item in the app store
if (purchase == null)
{
NotificationMessageBox("Not Purchased");
}
else
{
NotificationMessageBox("Purchased");
}
}
}
catch (InAppBillingPurchaseException purchaseEx)
{
switch (purchaseEx.PurchaseError)
{
//Error handling code removed for sake of simplicity/readability
}
}
finally
{
await billing.DisconnectAsync();
}
CrossInAppBilling.Dispose();
}
All code compiles, and the project builds. Everything works fine until I get to the line:
var purchase = await billing.PurchaseAsync(“productID”, ItemType.InAppPurchase, “payload”);
With a break point on this line, as soon as I take one step, I get the prompt to sign in to the app store. After I sign in, the code then immediately jumps to the catch, skipping over the entire if (purchase == null) statement (it doesn’t even check the if statement).
At this point, the variable “purchase” is still null.
The purchaseEx error that I get is of the type GeneralError, of which unfortunately the only description provided in the documentation is “other error”. Even looking at the source code, it refers to an unknown error received.
Finally, at the end of the code block above, the app crashes.
Interestingly, even when I change the productID to a nonsense string, I get exactly the same behaviour and same error - it appears to fail before it even makes the call to the app store to check the product?
I read somewhere that you can’t test in-app purchases on an emulator, so I tried deploying to a physical device, but got the same behaviour and error messages as well. I’ve tried with a real and a sandbox Apple ID and got the same results (though maybe I incorrectly used the sandbox account?)
Any suggestions as to where I’m going wrong would be greatly appreciated.