【发布时间】:2014-05-04 15:35:55
【问题描述】:
编辑:由于有些人仍在查看此线程,我想提一下,这是非常过时的,因为它是关于应用程序购买中的 v2,现在已弃用。请查看latest(当前为 v3)文档,非常简单
关于这个问题有很多线程,我想我理解这个问题,但是,目前我无法测试实际购买,因为我目前没有有效的信用卡,谷歌接受的,只有一个大师,这是不被接受的。这就是我寻求帮助的原因(不是购买验证,而是验证我的思维过程是否良好)。
首先,问题来自新的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; 行,并将android.test.purchased id 替换为我提供的真实SKU 的真实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));
}
}
});
}
}
});
}
提前致谢!
【问题讨论】:
-
试试这个来帮助你测试应用内购买stackoverflow.com/a/22088718