2017-01-16 22 views
2

同じ製品を複数回購入することはできません。次のコードでは、消費する製品のコードをどこに書き込むのか分かりません。 誰も私に助言を与えることができますか?複数の同じ製品を複数購入するためにinapp請求を使用する方法

これは私のコードです:

//-- onCreate 

inAppPurchase = new InAppPurchase(this); 


//--- calling on button click 

inAppPurchase.purchase(this, "android.test.purchased", "123"); 


//--- class InAppPurchase 

public class InAppPurchase { 

    public static final String BASE64_ENCODED_PUBLIC_KEY = "XXXXXXXXXXX"; // CONSTRUCT_YOUR_KEY_AND_PLACE_IT_HERE 
    public static final int RC_REQUEST = 10001; 
    String playStoreProductId; 
    IabHelper mHelper; 
    Activity activity; 

    public InAppPurchase() { 
     // TODO Auto-generated constructor stub 
    } 

    public InAppPurchase(Activity activity) { 
     // TODO Auto-generated constructor stub 
     mHelper = new IabHelper(activity, BASE64_ENCODED_PUBLIC_KEY); 
     mHelper.enableDebugLogging(true); 

     // Start setup. This is asynchronous and the specified listener will be called once setup completes. 
     Log.d("", "Starting setup."); 
     mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() 
     { 
      public void onIabSetupFinished(IabResult result) 
      { 
       if (!result.isSuccess()) 
       { 
        Log.e("", "Problem setting up in-app billing: " + result); 
       } 
       else 
       { 
        Log.e("", "Setup successful. Querying inventory.");  
       } 
      } 
     }); 
    } 

    public void purchase(Activity activity, String playStoreProductId, String orderId) 
    { 
     this.activity = activity; 
     this.playStoreProductId = playStoreProductId; 
     String payload = orderId; 
     mHelper.launchPurchaseFlow(activity, playStoreProductId, RC_REQUEST, mPurchaseFinishedListener, payload);  
    } 

    // Callback for when a purchase is finished 
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() 
    { 
     public void onIabPurchaseFinished(IabResult result, Purchase purchase) 
     { 
      Log.e("", "Purchase finished: " + result + ", purchase: " + purchase); 
      if (result.isFailure()) 
      { 
       Log.e("", "Error purchasing: " + result); 
      } 
      else if(purchase.getSku().equals(playStoreProductId)) 
      { 
       Log.e("", "Purchase product. Starting product consumption." + playStoreProductId); 
       mHelper.queryInventoryAsync(mGotInventoryListener); 
      } 
     } 
    }; 

    // Listener that's called when we finish querying the items and subscriptions we own 
    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() 
    { 
     public void onQueryInventoryFinished(IabResult result, Inventory inventory) 
     { 
      Log.e("", "Query inventory finished."); 
      if (result.isFailure()) 
      { 
       Log.e("", "Failed to query inventory: " + result); 
      } 
      else //if(result.isSuccess()) 
      { 
       Log.e("", " else We have product. Consuming it." + playStoreProductId); 
       Purchase purchase = inventory.getPurchase(playStoreProductId); 
       if (purchase != null) 
       { 
        Log.e("", "We have product. Consuming it."); 
        mHelper.consumeAsync(inventory.getPurchase(playStoreProductId), mConsumeFinishedListener); 
       } 
      } 
     } 
    }; 

    // Called when consumption is complete 
    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() 
    { 
     public void onConsumeFinished(Purchase purchase, IabResult result) 
     { 
      Log.e("", "Consumption finished. Purchase: " + purchase + ", result: " + result); 
      if (result.isSuccess()) 
      { 
       // Save Data 
       Log.e("", "Consumption successful. Provisioning."); 
      } 
      else 
      { 
       Log.e("", "Error while consuming: " + result); 
      } 
     } 
    }; 
} 



protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    Log.e("", "onActivityResult(" + requestCode + "," + resultCode + "," + data); 

    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == InAppPurchase.RC_REQUEST) 
    { 
     int responseCode = data.getIntExtra("RESPONSE_CODE", 0); 
     String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA"); 
     String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE"); 
     Log.e("", ""+responseCode + " " + purchaseData + " " + dataSignature); 
     if (resultCode == RESULT_OK) 
     { 
      if(responseCode == IabHelper.BILLING_RESPONSE_RESULT_OK) 
      { 
       try 
       { 
        JSONObject jo = new JSONObject(purchaseData); 
        String mOrderId = jo.getString("orderId"); 
        String mPackageName = jo.getString("packageName"); 
        String mSku = jo.getString("productId"); 
        long mPurchaseTime = jo.getLong("purchaseTime"); 
        int mPurchaseState = jo.getInt("purchaseState"); 
        String mDeveloperPayload = jo.getString("developerPayload"); 
        String mToken = jo.optString("token", jo.getString("purchaseToken")); 
       } 
       catch (JSONException e) 
       { 
        Log.e("", "Failed to parse purchase data."); 
        e.printStackTrace(); 
       } 
      } 
      else if(responseCode == IabHelper.BILLING_RESPONSE_RESULT_USER_CANCELED) 
      { 

      } 
      else if(responseCode == IabHelper.BILLING_RESPONSE_RESULT_SERVICE_UNAVAILABLE) 
      { 
       Toast.makeText(getApplicationContext(), "Network connection is down", 1).show(); 
      } 
     } 
     else if (resultCode == RESULT_CANCELED) 
     { 
      Toast.makeText(getApplicationContext(), "Payment Canceled", 1).show(); 
     } 
    } 
} 

答えて

1

あなたはこの

public void consumeItem() { 
    mHelper.queryInventoryAsync(mReceivedInventoryListener); 
} 

IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener 
    = new IabHelper.QueryInventoryFinishedListener() { 
     public void onQueryInventoryFinished(IabResult result, 
      Inventory inventory) { 

      if (result.isFailure()) { 
      // Handle failure 
      } else { 
       mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU), 
      mConsumeFinishedListener); 
      } 
    } 
}; 


IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = 
     new IabHelper.OnConsumeFinishedListener() { 
     public void onConsumeFinished(Purchase purchase, 
      IabResult result) { 

    if (result.isSuccess()) { 
      clickButton.setEnabled(true); 
    } else { 
      // handle error 
    } 
    } 
}; 

ような何かをしなければならない購入を消費するには、このarticle

を見てみてくださいをご覧くださいリンク先は上記のとおりです。これは、ステップバイステップの指示を提供します。それに続いて、あなたは購入を消費することに問題はありません。

関連する問題