1. 程式人生 > >GooglePlay內購In-app Billing 總結~

GooglePlay內購In-app Billing 總結~

startSetup 的操作是檢查是否有許可權和連線到Google Billing service是否成功;這裡回撥的操作是如果成功,呼叫queryInventoryAsync檢視產品id是否可以使用;

查詢完成後會呼叫IabHelper.QueryInventoryFinishedListener 這個回撥介面進行通知,在這個介面中可以獲取商品的詳細資訊SkuDetails和Purchase資訊。

點選購買按鈕,需要呼叫的支付方法

  1. String payload = "";   
  2. mHelper.launchPurchaseFlow(Activity act, String sku, String itemType, int
     requestCode, OnIabPurchaseFinishedListener listener, String payload);  

  1. boolean verifyDeveloperPayload(Purchase p) {  
  2.     String payload = p.getDeveloperPayload();  
  3.     /* 
  4.      * TODO: verify that the developer payload of the purchase is correct. It will be 
  5.      * the same one that you sent when initiating the purchase.
     
  6.      *  
  7.      * WARNING: Locally generating a random string when starting a purchase and  
  8.      * verifying it here might seem like a good approach, but this will fail in the  
  9.      * case where the user purchases an item on one device and then uses your app on  
  10.      * a different device, because on the other device you will not have access to the
     
  11.      * random string you originally generated. 
  12.      * 
  13.      * So a good developer payload has these characteristics: 
  14.      *   
  15.      * 1. If two different users purchase an item, the payload is different between them, 
  16.      *    so that one user's purchase can't be replayed to another user. 
  17.      *  
  18.      * 2. The payload must be such that you can verify it even when the app wasn't the 
  19.      *    one who initiated the purchase flow (so that items purchased by the user on  
  20.      *    one device work on other devices owned by the user). 
  21.      *  
  22.      * Using your own server to store and verify developer payloads across app 
  23.      * installations is recommended. 
  24.      */
  25.     returntrue;  
  26. }  

verifyDeveloperPayload這個方法是在支付完成的時候在回撥裡頭去驗證用的,關於payload的生產,看上面官方給的demo的註釋,大概理解了下:

1.不同的玩家所生成的payload需要不一樣

2.即使玩家在不同裝置上初始化payload,也要可以通過~

大概是這個意思(如果翻譯有問題~砸磚吧~)

下面是支付方法回撥的監聽:

  1. IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {  
  2.      publicvoid onIabPurchaseFinished(IabResult result, Purchase purchase) {  
  3.          Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);  
  4.          if (result.isFailure()) {  
  5.              complain("Error purchasing: " + result);  
  6.              setWaitScreen(false);  
  7.              return;  
  8.          }  
  9.          if (!verifyDeveloperPayload(purchase)) {  
  10.              complain("Error purchasing. Authenticity verification failed.");  
  11.              setWaitScreen(false);  
  12.              return;  
  13.          }  
  14.          Log.d(TAG, "Purchase successful.");  
  15.          if (purchase.getSku().equals(SKU_GAS)) {  
  16.              // bought 1/4 tank of gas. So consume it.
  17.              Log.d(TAG, "Purchase is gas. Starting gas consumption.");  
  18.     //購買成功,呼叫消耗產品
  19.              mHelper.consumeAsync(purchase, mConsumeFinishedListener);  
  20.          }  
  21.          elseif (purchase.getSku().equals(SKU_PREMIUM)) {  
  22.              // bought the premium upgrade!
  23.              Log.d(TAG, "Purchase is premium upgrade. Congratulating user.");  
  24.              alert("Thank you for upgrading to premium!");  
  25.              mIsPremium = true;  
  26.              updateUi();  
  27.              setWaitScreen(false);  
  28.          }  
  29.          elseif (purchase.getSku().equals(SKU_INFINITE_GAS)) {  
  30.              // bought the infinite gas subscription
  31.              Log.d(TAG, "Infinite gas subscription purchased.");  
  32.              alert("Thank you for subscribing to infinite gas!");  
  33.              mSubscribedToInfiniteGas = true;  
  34.              mTank = TANK_MAX;  
  35.              updateUi();  
  36.              setWaitScreen(false);  
  37.          }  
  38.      }  
  39.  };  
上面有中文註釋的地方呼叫了mHelper.consumeAsync這個方法,這裡應該是隻有非管理類的產品才需要呼叫的方法;相當於在購買成功後呼叫消耗(可以理解為消耗一個道具)

在IabHelper.OnConsumeFinishedListener的回撥用於處理成功呼叫成功支付的邏輯(這裡可能還需要一步去呼叫遠端伺服器驗證)