android in-app billing v3 api中的开发者有效负载应该是多少?

Ami*_*ant 22 android in-app-purchase in-app-billing android-billing

我正在我的应用中实施应用内结算以解锁高级功能.应用内结算功能设置正确.除了"开发者有效载荷"之外,一切似乎都很好.

示例应用程序说

 /*
     * TODO: verify that the developer payload of the purchase is correct. It will be
     * the same one that you sent when initiating the purchase.
     *
     * WARNING: Locally generating a random string when starting a purchase and
     * verifying it here might seem like a good approach, but this will fail in the
     * case where the user purchases an item on one device and then uses your app on
     * a different device, because on the other device you will not have access to the
     * random string you originally generated.
     *
     * So a good developer payload has these characteristics:
     *
     * 1. If two different users purchase an item, the payload is different between them,
     *    so that one user's purchase can't be replayed to another user.
     *
     * 2. The payload must be such that you can verify it even when the app wasn't the
     *    one who initiated the purchase flow (so that items purchased by the user on
     *    one device work on other devices owned by the user).
     *
     * Using your own server to store and verify developer payloads across app
     * installations is recommended.
     */
Run Code Online (Sandbox Code Playgroud)

示例应用程序使用空字符串作为开发人员负载.我的问题是用作开发人员有效负载的字符串是什么?我可以使用用户的主电子邮件ID吗?

Mau*_*lik 1

请检查以下答案,它可能会解决您的问题:

如果您使用消耗品(托管物品),那么您可以使用随机生成的字符串

步骤1:在创建方法之前声明:

         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);
                }
Run Code Online (Sandbox Code Playgroud)

步骤 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);
        }

    }
Run Code Online (Sandbox Code Playgroud)

第 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);
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请检查此链接: 识别用户的令牌

希望它能解决您的问题。

  • *不要*使用随机字符串。用户可以在一台设备上购买商品并希望在另一台设备上拥有它。问题中描述了这一点(第 2 项) (20认同)
  • 当您取消购买并尝试再次购买时也会出现问题。Google Play 每次都会返回第一个有效负载。 (2认同)