编辑:由于仍有人查看此帖子,我想提醒一下,这是高度过时的内容,因为它涉及到应用内购买版本2,而现在已经不再使用。请查看最新(目前是v3)文档,这很简单。
关于这个问题有很多讨论,我认为我理解了问题,但是目前我无法进行实际购买的测试,因为我没有可接受的信用卡,只有一张不被接受的Maestro卡。因此,我请求帮助(不是购买验证,而是验证我的思路是否正确)。
首先,问题来自新的verifyPurchase
方法。这个新方法检查签名,这应该没问题。然而,Google没有为测试id(例如android.test.purchased
)提供任何签名。这导致以下方法始终失败,并且无论虚假购买是否完成并出现确认对话框,它始终返回false进行验证。这个方法:
public static boolean verifyPurchase(String base64PublicKey, String signedData, String signature) {
//if(BuildConfig.DEBUG) return true;
if (TextUtils.isEmpty(signedData) || TextUtils.isEmpty(base64PublicKey) ||
TextUtils.isEmpty(signature)) {
Log.e(TAG, "Purchase verification failed: missing data.");
return false;
}
PublicKey key = Security.generatePublicKey(base64PublicKey);
return Security.verify(key, signedData, signature);
}
问题出在
TextUtils.isEmpty()
的检查上,由于签名为空,代码将返回false。我的临时测试解决方案在代码中被注释掉了。如果我在测试的方法开始处添加if(BuildConfig.DEBUG) return true;
,一切正常,用户可以使用高级功能,所以问题确实与签名有关。另一个问题是,如果我购买了
android.test.purchased
,我无法查询用户的库存,所以我不能确定,我的检查方法是否有效,用于检查用户是否已购买了高级功能。我的两个问题是:
如果我从
verifyPurchase
中删除if(BuildConfig.DEBUG) return true;
行,并用我提供的真实SKU的真实ID替换android.test.purchased
id,那么我能确定在我的情况下一切都会正常工作吗?重申,添加了调试功能后一切正常。如果您需要看更多的代码,请告诉我!以下方法检查用户是否已购买高级功能,如果是,则设置相应的首选项,否则,如果出现任何问题,它将保持原样。这个方法正确吗?我在
Application
类中执行此操作,每次启动应用程序时,以防止篡改。private void checkPremium()
{
//get the helper up and running
final IabHelper helper=new IabHelper(INSTANCE, getBase64Key());
helper.startSetup(new IabHelper.OnIabSetupFinishedListener()
{
@Override
public void onIabSetupFinished(IabResult result)
{
//if the helper is in a correct state, check the inventory
if(result.isSuccess())
{
helper.queryInventoryAsync(new IabHelper.QueryInventoryFinishedListener()
{
@Override
public void onQueryInventoryFinished(IabResult result, Inventory inv)
{
//if the inventory is reachable, check if we got the premium
if(result.isSuccess())
{
setPremium(inv.hasPurchase(ActWidgetSearch.SKU));
}
}
});
}
}
});
}
谢谢您提前的支持!