1

開発者のペイロードに関する質問がありました。ドキュメントを読んでいますが、まだ理解していないverifydeveloperpayloadを理解するのが難しいと感じています:

boolean verifyDeveloperPayload(Purchase p) {
        String payload = p.getDeveloperPayload();
        /*
         * 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.
         */
        return true;
    }

私のコードが

public void onClick(View v) {
    String item1;
    // TODO Auto-generated method stub
     switch(v.getId()) {
        case R.id.button1: {
            /* TODO: for security, generate your payload here for verification. See the comments on
             *        verifyDeveloperPayload() for more info. Since this is a SAMPLE, we just use
             *        an empty string, but on a production app you should carefully generate this. */

            item1 = "item1";
            String payload = email+item1;

            mHelper.launchPurchaseFlow(this, SKU_2006, RC_REQUEST,
                    mPurchaseFinishedListener, payload);
            break;
            }
         }

}

ブール値の verifyDeveloperPayload からの String ペイロードは、onClick メソッドからの String ペイロードと等しくなりますか?

ペイロードを比較する方法と場所は?

4

1 に答える 1

8

Androidのドキュメントhereによると、次のように述べられています

5 番目の引数には、注文に関する補足情報を送信するために使用できる「開発者ペイロード」文字列が含まれます (空の文字列の場合もあります)。通常、これは、この購入リクエストを一意に識別する文字列トークンを渡すために使用されます。文字列値を指定すると、Google Play は購入応答とともにこの文字列を返します。その後、この購入についてクエリを実行すると、Google Play はこの文字列を購入の詳細と共に返します。

セキュリティに関する推奨事項:アプリケーションが購入を行ったユーザーを識別するのに役立つ文字列を渡すことをお勧めします。これにより、後でそのユーザーによる正当な購入であることを確認できます。消耗品の場合はランダムに生成された文字列を使用できますが、非消耗品の場合はユーザーを一意に識別する文字列を使用する必要があります。

したがって、製品 ID SKU_2006で購入フローを開始した場合String payload = email+item1;、Google Play は応答で同じペイロードを返すため、ここで次のように取得します。

boolean verifyDeveloperPayload(Purchase p) {
        String payload = p.getDeveloperPayload();
..
}

ここで、コードの観点からシナリオ全体を定義しましょう。

まず、以下のような購入リクエストを開始します

String payload = getUserEmailFromAndroidAccounts() + itemUniqueId;

mHelper.launchPurchaseFlow(new PurchaseFinishListener(itemUniqueId), SKU_GAS, 10001,   
   mPurchaseFinishedListener, payload);

注文が成功すると、Google Play からの応答データが Purchase オブジェクトに格納され、リスナーに返されます。

    private class PurchaseFinishListener implements IabHelper.OnIabPurchaseFinishedListener {
   private final String mItemUniqeId;
    public PurchaseFinishListener(String itemUniqeId) {

            mItemUniqeId = itemUniqeId;
        }

       public void onIabPurchaseFinished(IabResult result, Purchase purchase) 
       {
          if (result.isFailure()) {
             Log.d(TAG, "Error purchasing: " + result);
             return;
          }      
    if (!verifyDeveloperPayLoad(mItemUniqeId , purchase)) {
     Log.d(TAG, "Authenticity verification failed");
             return;
    }

    // set your product as purchased in your DB or server

    }
    }

これで、verifyDeveloperPayLoad(purchase) メソッドは次のようになります。

 private boolean verifyDeveloperPayLoad(String itemUniqueId , Purchase purchase) {
        String responsePayload = purchase.getDeveloperPayload();
        String computedPayload = getUserEmailFromAndroidAccounts() + itemUniqueId;

        return responsePayload != null && responsePayload.equals(computedPayload);
    }

コードをよく観察していれば、ワークフローがどのようになっているのかがわかるはずです。

サブスクリプション ベースの購入と 1 回限りの購入に関する詳細情報は、Android サイトから入手できます。

于 2014-11-20T05:35:54.020 に答える