您可以使用开发人员有效负载来识别用户并确保安全。
根据您的应用程序计费要求,有两种方法可以生成开发人员有效负载。
1) 如果您使用的是非托管项目(非消耗品),那么您可以简单地使用 UserID 来唯一标识用户,特别是您的应用程序。您可以将开发人员有效负载作为 UserID 发送。
或
如果您将用户的电子邮件 ID 存储到服务器中,您可以将电子邮件地址放入开发人员有效负载中以获得唯一 ID。当您在用户支付产品费用后从 google play 获得响应然后从该用户帐户的服务器数据库中获取它时,匹配您的开发人员有效负载。
本地数据库(如 SQLite):
UserID
(Automatecally
generated by product type userEmailAddress
Sql database)
1 product1 abc@gmail.com
2 product1 xyz@gmail.com
3 product1 pqr@gmail.com
您可以将其作为用户 ID 传递给有效负载
--> 它会在一段时间内产生问题。如果您不想使用服务器数据库,那么您可以简单地忽略开发有效负载,使其成为一个空白字符串,它不会对您的代码产生更多影响。检查 Nikolay Elenkov 的此链接答案:stackoverflow.com/questions/14553515 /
2)如果您使用的是消耗品(管理物品),那么您可以使用随机生成的字符串
step 1: before on create method declare this:
private static final char[] symbols = new char[36];
static {
for (int idx = 0; idx < 10; ++idx)
symbols[idx] = (char) ('0' + idx);
for (int idx = 10; idx < 36; ++idx)
symbols[idx] = (char) ('a' + idx - 10);
}
第 2 步:在您的活动中设置 RandomString 和 SessionIdentifierGenerator 类
public class RandomString {
/*
* static { for (int idx = 0; idx < 10; ++idx) symbols[idx] = (char)
* ('0' + idx); for (int idx = 10; idx < 36; ++idx) symbols[idx] =
* (char) ('a' + idx - 10); }
*/
private final Random random = new Random();
private final char[] buf;
public RandomString(int length) {
if (length < 1)
throw new IllegalArgumentException("length < 1: " + length);
buf = new char[length];
}
public String nextString() {
for (int idx = 0; idx < buf.length; ++idx)
buf[idx] = symbols[random.nextInt(symbols.length)];
return new String(buf);
}
}
public final class SessionIdentifierGenerator {
private SecureRandom random = new SecureRandom();
public String nextSessionId() {
return new BigInteger(130, random).toString(32);
}
}
第 3 步:将有效负载传递到您的购买请求中:
RandomString randomString = new RandomString(36);
System.out.println("RandomString>>>>" + randomString.nextString());
/* String payload = ""; */
// bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ
String payload = randomString.nextString();
Log.e("Random generated Payload", ">>>>>" + payload);
Log.d(TAG, "Launching purchase flow for infinite gas subscription.");
mHelper.launchPurchaseFlow(this, SKU_GAS,
IabHelper.ITEM_TYPE_INAPP, RC_REQUEST,
mPurchaseFinishedListener, payload);
for more inforamation check this link:
http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string
请注意:
安全建议:当您收到来自 Google Play 的购买响应时,请务必检查返回的数据签名、
orderId 和 Purchase 对象中的 developerPayload 字符串
确保您获得预期值。你应该验证
orderId 是您以前没有的唯一值
已处理,并且 developerPayload 字符串与您的令牌匹配
之前与购买请求一起发送。作为进一步的安全
预防措施,您应该自行安全地进行验证
服务器。
check this link:
http://developer.android.com/google/play/billing/billing_integrate.html
for more details check this link:
http://developer.android.com/google/play/billing/billing_best_practices.html
希望对你有所帮助。