我正试图在我的android应用程序中实现应用程序购买。我使用应用计费API v3来执行IAP(应用内购买)。作为参考,我遵循谷歌提供的琐碎驱动器样本。在launchPurchaseFlow
方法
public void launchPurchaseFlow(Activity act, String sku, String itemType, int requestCode,
OnIabPurchaseFinishedListener listener, String extraData) {
checkSetupDone("launchPurchaseFlow");
flagStartAsync("launchPurchaseFlow");
IabResult result;
if (itemType.equals(ITEM_TYPE_SUBS) && !mSubscriptionsSupported) {
IabResult r = new IabResult(IABHELPER_SUBSCRIPTIONS_NOT_AVAILABLE,
"Subscriptions are not available.");
if (listener != null) listener.onIabPurchaseFinished(r, null);
return;
}
try {
logDebug("Constructing buy intent for " + sku + ", item type: " + itemType);
Bundle buyIntentBundle = mService.getBuyIntent(3, mContext.getPackageName(), sku, itemType, extraData);
int response = getResponseCodeFromBundle(buyIntentBundle);
if (response != BILLING_RESPONSE_RESULT_OK) {
logError("Unable to buy item, Error response: " + getResponseDesc(response));
result = new IabResult(response, "Unable to buy item");
if (listener != null) listener.onIabPurchaseFinished(result, null);
return;
}
PendingIntent pendingIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT);
logDebug("Launching buy intent for " + sku + ". Request code: " + requestCode);
mRequestCode = requestCode;
mPurchaseListener = listener;
mPurchasingItemType = itemType;
act.startIntentSenderForResult(pendingIntent.getIntentSender(),
requestCode, new Intent(),
Integer.valueOf(0), Integer.valueOf(0),
Integer.valueOf(0));
}
catch (SendIntentException e) {
logError("SendIntentException while launching purchase flow for sku " + sku);
e.printStackTrace();
result = new IabResult(IABHELPER_SEND_INTENT_FAILED, "Failed to send intent.");
if (listener != null) listener.onIabPurchaseFinished(result, null);
}
catch (RemoteException e) {
logError("RemoteException while launching purchase flow for sku " + sku);
e.printStackTrace();
result = new IabResult(IABHELPER_REMOTE_EXCEPTION, "Remote exception while starting purchase flow");
if (listener != null) listener.onIabPurchaseFinished(result, null);
}
}
在上面的代码中,getBuyIntent
不期望nonce
,我们有额外的数据,我们在其中传递developerpayload(特定于每个购买项目)。在这里,我无法弄清楚如何通过nonce,因为它在API V2中通过,并作为成功购买的响应收到。V3中不需要nonce
吗?
谢谢
当使用IAB API v3时,只需使用您从Google Play收到的原始JSON响应,并使用签名和公共RSA对其执行OpenSSL验证。
最好通过您自己的API执行此检查,因为在应用程序中绑定公共RSA密钥是不安全的。下面是一个PHP示例,用于此OpenSSL验证。
如该示例所示,它只是从请求中获取参数:响应数据和签名,并使用公钥对其进行验证。
你只需要在你的应用程序和你的API中篡改原始JSON数据来弄清楚用户买了什么,而不是来验证购买本身的有效性。
所以简而言之:不要担心nonce。IAB API v3删除了它,您不应该担心它的缺失。APIv3返回orderId,可作为nonce使用,以避免重放攻击。