Xamarin Forms InAppBilling插件Android端购买检查异常排查
问题:Xamarin内购插件查询不到已购项目但购买时提示AlreadyOwned
我正在将部署到Google Play的Beta版应用中测试James Montemagno的Xamarin in-app billing plugin。
bool billingConnected = await billing.ConnectAsync(); if (!billingConnected) return (false, "Cannot connect."); IEnumerable<InAppBillingPurchase> purchases = await billing.GetPurchasesAsync(ItemType.InAppPurchase); //purchase not owned if (purchases == null || purchases.Count() == 0) { return (true, "Purchase not found"); } purchase = purchases.FirstOrDefault(p => string.Compare(p.Id, PRODUCTID_ABC, true) == 0); //purchase not owned if (purchase == null)// THIS IS ALWAYS NULL { return (true, "Purchase not found"); }
但尝试进行购买时,我收到错误提示:
- PurchaseError.AlreadyOwned
这是什么原因?
我在Google文档中看到部分查询需要设置“访问Google Play开发者API,并确保已授予查看财务数据权限”。
这与此问题相关吗?这是我遗漏的配置吗?但James Montemagno的插件文档中并未提及此内容。
求解决思路?
解决思路
- 检查商品类型匹配:你调用
GetPurchasesAsync时传入的是ItemType.InAppPurchase,如果你的目标商品是订阅类型,必须换成ItemType.Subscription,否则无法查询到对应购买记录。 - 验证商品ID准确性:确认
PRODUCTID_ABC和Google Play后台配置的商品ID完全一致,哪怕是细微的拼写或大小写差异都可能导致匹配失败。 - 开发者API权限无关性:Google Play开发者API权限主要用于服务器端的购买记录验证,客户端通过插件查询本地缓存的购买记录时不需要开启该权限,无需额外配置。
- 处理测试账号与缓存问题:确保测试账号已添加到Google Play的Beta测试人员列表并接受了测试邀请;同时可尝试清除应用的Google Play服务缓存,或退出并重新登录Google账号,解决购买记录同步延迟的问题。
- 排查插件版本兼容性:使用最新版本的Xamarin in-app billing plugin,旧版本可能存在与Google Play Billing Library适配的bug,导致购买记录查询异常。
- 补充订单确认逻辑:如果是非消耗型商品,需调用
AcknowledgePurchaseAsync确认订单;消耗型商品则调用ConsumePurchaseAsync完成消耗。未完成确认的订单会被Google Play判定为已拥有,但客户端可能无法查询到记录,进而触发AlreadyOwned错误。
内容的提问来源于stack exchange,提问作者jho
相关产品推荐
相关产品推荐

