2016-07-02 32 views
0

我剛剛在我的應用程序中實現了應用程序計費功能。 我只有一個產品來解鎖所有高級功能。
購買本身有效,但是在購買流程之後,我的代碼顯然無法識別購買成功,因爲在購買後出現投訴窗口「Articel已經擁有」。所以不知何故購買流程不被認可是成功的。在我重新啓動應用程序後,專業版功能被解鎖,因爲查詢庫存說,專業功能已被購買。 我的想法,所以這裏是我的代碼:android在應用程序購買 - Articel已經擁有的問題

// SKUs for our products: the premium upgrade (non-consumable) 

字符串SKU_PREMIUM;

// (arbitrary) request code for the purchase flow 

final int RC_REQUEST = 10001;

// The helper object 
IabHelper mHelper; 

// Provides purchase notification while this app is running 
public IabBroadcastReceiver mBroadcastReceiver; 
IabHelper.QueryInventoryFinishedListener mGotInventoryListener; 
AppCompatActivity activity; 
private String payload; 

public InAppBillingHelper(AppCompatActivity context, final IabBroadcastReceiver.IabBroadcastListener contextBroadcastListener, String base64EncodedPublicKey,String skuPremium, boolean debug){ 
    //String base64EncodedPublicKey = "VALENTIN_HERR_SCHAFKOPF_RECHENHELFER"; 
    this.activity = context; 
    SKU_PREMIUM = skuPremium; 
    // Create the helper, passing it our context and the public key to verify signatures with 
    Log.d(TAG, "Creating IAB helper."); 
    mHelper = new IabHelper(context, base64EncodedPublicKey); 

    // enable debug logging (for a production application, you should set this to false). 
    mHelper.enableDebugLogging(debug); 

    // Start setup. This is asynchronous and the specified listener 
    // will be called once setup completes. 
    Log.d(TAG, "Starting setup."); 

    // Listener that's called when we finish querying the items and subscriptions we own 
    mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() { 
     public void onQueryInventoryFinished(IabResult result, Inventory inventory) { 
      Log.d(TAG, "Query inventory finished."); 

      // Have we been disposed of in the meantime? If so, quit. 
      if (mHelper == null) return; 

      // Is it a failure? 
      if (result.isFailure()) { 
       complain("Fehler bei der Bestimmung der App-Lizenz: " + result); 
       // TODO analyse results and output it to the user 
       return; 
      } 

      Log.d(TAG, "Query inventory was successful."); 

      // Do we have the premium upgrade? 
      Purchase premiumPurchase = inventory.getPurchase(SKU_PREMIUM); 
      mIsPremium = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase)); 
      Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM")); 
     } 

      updateUi(); 
      setWaitScreen(false); 
      Log.d(TAG, "Initial inventory query finished; enabling main UI."); 
     } 
    }; 

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 
     public void onIabSetupFinished(IabResult result) { 
      Log.d(TAG, "Setup finished."); 

      if (!result.isSuccess()) { 
       // Oh noes, there was a problem. 
       complain("Problem setting up in-app billing: " + result); 
       return; 
      } 
      // Have we been disposed of in the meantime? If so, quit. 
      if (mHelper == null) return; 

      mBroadcastReceiver = new IabBroadcastReceiver(contextBroadcastListener); 
      IntentFilter broadcastFilter = new IntentFilter(IabBroadcastReceiver.ACTION); 
      try { 
       activity.registerReceiver(mBroadcastReceiver, broadcastFilter); 
      }catch (Exception e) 
      {} 
      // IAB is fully set up. Now, let's get an inventory of stuff we own. 
      Log.d(TAG, "Setup successful. Querying inventory."); 
      try { 
       mHelper.queryInventoryAsync(mGotInventoryListener); 
      } catch (IabHelper.IabAsyncInProgressException e) { 
       complain("Error querying inventory. Another async operation in progress."); 
      } 
     } 
    }); 
} 


public void receivedBroadcast() { 
    // Received a broadcast notification that the inventory of items has changed 
    Log.d(TAG, "Received broadcast notification. Querying inventory."); 
    try { 
     mHelper.queryInventoryAsync(mGotInventoryListener); 
    } catch (IabHelper.IabAsyncInProgressException e) { 
     complain("Error querying inventory. Another async operation in progress."); 
    } 
} 


// User clicked the "Upgrade to Premium". 
public void handlePremiumPurchase() { 
    Log.d(TAG, "Upgrade button clicked; launching purchase flow for upgrade."); 
    setWaitScreen(true); 

    payload = ""; 

    try { 
     mHelper.launchPurchaseFlow(activity, SKU_PREMIUM, RC_REQUEST, 
       mPurchaseFinishedListener, payload); 
    } catch (IabHelper.IabAsyncInProgressException e) { 
     complain("Error launching purchase flow. Another async operation in progress."); 
     setWaitScreen(false); 
    }catch (NullPointerException e) 
    { 
     complain("Fehler beim Kaufversuch. Stelle sicher, dass mit einem Google Konto im PlayStore angemeldet bist und eine stabile Internetverbindung besteht." + 
       "Versuche es nach noch einmal." + 
       ""); 
     setWaitScreen(false); 
    } 
} 

public boolean isPremium() { 
    return mIsPremium; 
} 

public boolean onActivityResult(int requestCode, int resultCode, Intent data) { 
    Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data); 
    if (mHelper == null) return false; 

    // Pass on the activity result to the helper for handling 
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) { 
     // Todo handle 
    } 
    else { 
     Log.d(TAG, "onActivityResult handled by IABUtil."); 
    } 
    return true; 
} 

/** Verifies the developer payload of a purchase. */ 
boolean verifyDeveloperPayload(Purchase p) { 
    String payload = p.getDeveloperPayload(); 

    // Todo payload string verification 

    return true;//payload.equals(this.payload); 
} 

// Callback for when a purchase is finished 
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { 
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) { 
     Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase); 

     // if we were disposed of in the meantime, quit. 
     if (mHelper == null) return; 

     if (result.isFailure()) { 
      if(result.getResponse() == IabHelper.BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED) 
      { 
       mIsPremium = true; 
       updateUi(); 
       //complain("Du besitzt bereits das Premium Paket auf einem deiner angemeldeten Google Konten."); 
      } 
      else { 
       complain("Fehler beim Kauf: " + result); 
      } 
      setWaitScreen(false); 
      return; 
     } 
     if (!verifyDeveloperPayload(purchase)) { 
      complain("Error purchasing. Authenticity verification failed."); 
      setWaitScreen(false); 
      return; 
     } 

     Log.d(TAG, "Purchase successful."); 

     if (purchase.getSku().equals(SKU_PREMIUM)) { 
      // bought the premium upgrade! 
      Log.d(TAG, "Purchase is premium upgrade. Congratulating user."); 
      alert("Danke für das Premium Upgrade! Ich wünsche dir weiterhin viel Spaß bei der Nutzung!"); 
      mIsPremium = true; 
      updateUi(); 
      setWaitScreen(false); 
     } 

    } 
}; 

btw:該代碼是** trivial drive *示例的修改版本。 在此先感謝!

回答

1

儘管它不是最乾淨的解決方案,但是如果您有「已經擁有的錯誤」,只需再次查詢庫存。然後根據庫存查詢結果更新UI。

0

謝謝@DerAdler。我按照你說的方式執行它,並且它工作。