1

我在應用程序結算中遇到了一些問題,我想在應用程序內部創建一個可以多次購買的產品。但谷歌在應用程序結算方面的做法是,在購買產品之前必須首先使用產品。我試着用下面的代碼:Android:購買後購買產品(應用內結算)

public class HomeFragment extends Fragment { 

    private WebView homesite; 
    private String homeTabUrl; 
    private ProgressBar pBar; 
    private Bundle webViewBundle; 
    private Boolean buddyProfile = false; 
    private Boolean buddyProfile2 = false; 
    String url=""; 

    private Context mContext; 
    final static String REQUEST_SUCCESS = "COMPLETED"; 


    //Amount point 
    private static final int CREDIT_AMOUNT_100 = 100; 
    private static final int CREDIT_AMOUNT_500 = 500; 

    public static final String PAY_UID_100 = "messages"; 
    public static final String PAY_UID_500 = "messages2"; 


    private int mCreditAmount; 
    private IabHelper mHelper; 

    private static final int PAYINAPP_REQUESTCODE = 10001; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setHasOptionsMenu(true); 
     setRetainInstance(true); 
     StaticMembers.loadHomeTab = true; 

     purchaseCredit(CREDIT_AMOUNT_100); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     //Logger.error("YES-HERE IN ONACTIVITY RESULT"); 
     if (checkReultPayInApp(requestCode,resultCode, data)) { 
      return; 
     } 

     if (!mHelper.handleActivityResult(requestCode, resultCode, data)) { 
      super.onActivityResult(requestCode, resultCode, data); 
     } 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
    } 



    private void loadData(View v) { 
     initPayinApp(); 
    } 

    private void initPayinApp() { 
     // TODO Auto-generated method stub 
     String base64EncodedPublicKey = "n9goJ2waiwGS3F0E+XpjlMJRnn6rKtaH3lWxGUZQjNJeAAdL78mFeUTAGaZLgX/YOIuERWL5IzLaTXNi69c60oeh489wi3lyGtWbNvXR5EXVNhazti2mZgwvjhUdzW7/73mV0rHZn0f24G3Dpy0zLaTXNi69c60oeh489wi3lyGtWbNvXR5EXVNhazti2mZgwvjhUdzW7/73mV0rHZn0f24G3Dpy0wkIFWt51OnnusIVlJHrwJ8dYz4mUZ6SLFhkXL8NhrRAcZKvUV3WySB55SA5uu1+IoGG7mJw0QPn9goJ2waiwGS3F0E+XpjlMJRnn6rKtaH3lWxGUZQjNJeAAdL78mFeUTAGaZLgX/YOIuERWL5I7uInsqWH+ny1HFDr2wIDAQAB"; 

     mHelper = new IabHelper(getActivity(), base64EncodedPublicKey); 

     mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 
      public void onIabSetupFinished(IabResult result) { 
       if (!result.isSuccess()) { 
        Log.d("YES", "In-app Billing setup failed: " + result); 
       } else { 
        Log.d("YES", "In-app Billing is set up OK"); 
       } 
      } 
     }); 
    } 

    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = 
      new IabHelper.OnConsumeFinishedListener() { 
       public void onConsumeFinished(Purchase purchase, IabResult result) { 
        Logger.error("YES- Only in consume Listener"); 
        if (result.isSuccess()) { 

         Logger.error("YES-in success consume Listener"); 
        } 
        else { 
         Logger.error("YES-!in success consume Listener"); 
        } 
       } 
      }; 


    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener 
      = new IabHelper.OnIabPurchaseFinishedListener() { 
     public void onIabPurchaseFinished(IabResult result, Purchase purchase) 
     { 
      Logger.error("YES-IN SUCCESS: " + result.getMessage()); 
      if (result.isFailure()) { 
       Logger.error("YES-"+result.getMessage()); 
       return; 
      } 
      else if (purchase.getSku().equals(PAY_UID_100)) { 

       Logger.error("YES-100 purchased"); 
       // remove query inventory method from here and put consumeAsync() directly 
       Toast.makeText(getActivity(), "100 points added successfully", Toast.LENGTH_SHORT).show(); 
       mHelper.consumeAsync(purchase, mConsumeFinishedListener); 

      } else if (purchase.getSku().equals(PAY_UID_500)) { 
       Toast.makeText(getActivity(), "500 points added successfully", Toast.LENGTH_SHORT).show(); 
       Logger.error("YES-500 purchased"); 
       mHelper.consumeAsync(purchase, mConsumeFinishedListener); 
      } 

     } 
    }; 

    private void purchaseCredit(int creditAmount) { 
     mCreditAmount = creditAmount; 
     try { 
      if (mHelper != null) mHelper.flagEndAsync(); 
      mHelper.launchPurchaseFlow(getActivity(), getIdPayInAppByAmount(creditAmount), PAYINAPP_REQUESTCODE, 
        mPurchaseFinishedListener, "mypurchasetoken"); 
     } catch (Exception e) { 
      Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show(); 
     } 

    } 

    protected String getIdPayInAppByAmount(int creditAmount) { 
     switch (creditAmount) { 
      case CREDIT_AMOUNT_100: 
       return PAY_UID_100; 
      case CREDIT_AMOUNT_500: 
       return PAY_UID_500; 
     } 
     return null; 
    } 

    private boolean checkReultPayInApp(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     if (requestCode!= PAYINAPP_REQUESTCODE) { 
      return false; 
     } 

     int responseCode = data.getIntExtra("RESPONSE_CODE", 0); 
     String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA"); 
     String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE"); 

     if (resultCode == Activity.RESULT_OK) { 
      // Purchased Status = Purchased 
      try { 
       // Product Details JSON 
       JSONObject jo = new JSONObject(purchaseData); 
       // Purchased Product ID 
       String sku = jo.getString("productId"); 
       addCredit(mCreditAmount); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } else { 

     } 

     return true; 
    } 

    public void addCredit(final int creditAmount) { 
     TaskPost.depositPoint(getActivity(), new User().getEmail(), creditAmount, new MyHttpClient.OnRequestListener() { 

      public void OnStart() { 
       // TODO Auto-generated method stub 
      } 

      @Override 
      public void OnFinish(String result) { 
       if (result.equalsIgnoreCase(REQUEST_SUCCESS)) { 
        Toast.makeText(mContext,"You have just add " + creditAmount+ " success to your account",Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 
    } 
} 

請讓我知道我在做什麼錯誤,我正在做的消費產品,所以我可以做出相應的更改。

回答

1

你應該消費以前購買的物品。

private void initPayinApp() { 
     // TODO Auto-generated method stub 
     String base64EncodedPublicKey = ""; 

     mHelper = new IabHelper(getActivity(), base64EncodedPublicKey); 

     mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 
      public void onIabSetupFinished(IabResult result) { 
       if (!result.isSuccess()) { 
        Log.d("YES", "In-app Billing setup failed: " + result); 
       } else { 
        Log.d("YES", "In-app Billing is set up OK"); 

        //comment this function once all the previously purchased items are consumed successfully 
        reset(); 
       } 
      } 
     }); 
    } 

void reset() { 

    mHelper.queryInventoryAsync(mGotInventoryListener); 
} 


IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() { 

     @Override 
     public void onQueryInventoryFinished(IabResult result, Inventory inventory) { 
      Log.d(TAG, "Query inventory finished."); 
      if (result.isFailure()) { 
       Log.d(TAG, "Failed to query inventory: " + result); 
       return; 
      } 

      Log.d(TAG, "Query inventory was successful."); 
      if(inventory.hasPurchase(PAY_UID_100)) 
      { 
       Log.d(TAG, "already purchased : " + PAY_UID_100); 
       mHelper.consumeAsync(inventory.getPurchase(PAY_UID_100), mConsumeFinishedListener); 
      } 
      if(inventory.hasPurchase(PAY_UID_500)) 
      { 
       Log.d(TAG, "already purchased : " + PAY_UID_500); 
       mHelper.consumeAsync(inventory.getPurchase(PAY_UID_500), mConsumeFinishedListener); 
      } 
     } 
    }; 
+0

感謝您的建議,但您提供的解決方案沒有解決我的問題。 –

+0

它在日誌中打印什麼? – Pratik

+0

這裏是日誌,檢查這個鏈接:http://pasted.co/cf9bc0c9 –