2016-02-23 122 views
2

此處我正在實施Google的多種訂閱類型的應用內購買,即monthly,quarterly,half-yearlyyearly。 我爲每個訂閱類型創建SkewId而不是託管或非託管。這些訂閱包將適用於相同類型的工作。但是現在我想知道怎麼才能知道訂閱的日子,付款後打包訂閱?針對同一類型產品的多種訂閱的Android應用內購買

我實現代碼託管類型爲:

mHelper = new IabHelper(this, base64EncodedPublicKey); 
    // enable debug logging (for a production application, you should set 
// this to false). 
    mHelper.enableDebugLogging(true); 
    mHelper.startSetup(new 
           IabHelper.OnIabSetupFinishedListener() { 
            public void onIabSetupFinished(IabResult result) { 
             if (!result.isSuccess()) { 
              Log.d("TAG", "In-app Billing setup failed: " + 
                result); 
             } else { 
              Log.d("TAG", "In-app Billing is set up OK"); 
             } 
            } 
           }); 
} 
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener 
     = new IabHelper.OnIabPurchaseFinishedListener() { 
    public void onIabPurchaseFinished(IabResult result, 
             Purchase purchase) { 
     if (result.isFailure()) { 
      // Handle error 
      Log.d("TAG", "We have gas. Consuming it."); 
      remove_ads_btn_monthly.setEnabled(true); 
      remove_ads_btn_quarterly.setEnabled(true); 
      remove_ads_btn_halfyearly.setEnabled(true); 
      remove_ads_btn_yearly.setEnabled(true); 
      if (plan_duration != 1) 
       toast = new CustomToast(RemoveAdsActivity.this, "You've already subscribed package of " + plan_duration + " months."); 
      else 
       toast = new CustomToast(RemoveAdsActivity.this, "You've already subscribed package of " + plan_duration + " month."); 

      return; 
     } else if (purchase.getSku().equals(SKU_MONTHLY)) { 
      consumeItem(); 
      remove_ads_btn_monthly.setEnabled(false); 
      remove_ads_btn_quarterly.setEnabled(false); 
      remove_ads_btn_halfyearly.setEnabled(false); 
      remove_ads_btn_yearly.setEnabled(false); 
     } else if (purchase.getSku().equals(SKU_HALF_YEARLY)) { 
      consumeItem(); 
      remove_ads_btn_monthly.setEnabled(false); 
      remove_ads_btn_quarterly.setEnabled(false); 
      remove_ads_btn_halfyearly.setEnabled(false); 
      remove_ads_btn_yearly.setEnabled(false); 
     } else if (purchase.getSku().equals(SKU_QUARTERLY)) { 
      consumeItem(); 
      remove_ads_btn_monthly.setEnabled(false); 
      remove_ads_btn_quarterly.setEnabled(false); 
      remove_ads_btn_halfyearly.setEnabled(false); 
      remove_ads_btn_yearly.setEnabled(false); 
     } else if (purchase.getSku().equals(SKU_ANNUALY)) { 
      consumeItem(); 
      remove_ads_btn_monthly.setEnabled(false); 
      remove_ads_btn_quarterly.setEnabled(false); 
      remove_ads_btn_halfyearly.setEnabled(false); 
      remove_ads_btn_yearly.setEnabled(false); 
     } 

    } 
}; 

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

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

     Purchase gasMonthly = inventory.getPurchase(SKU_MONTHLY); 
     Purchase gasYearly = inventory.getPurchase(SKU_ANNUALY); 
     Purchase gasQuarterly = inventory.getPurchase(SKU_QUARTERLY); 
     Purchase gasHalfYearly = inventory.getPurchase(SKU_HALF_YEARLY); 
     if (gasMonthly != null) { 
      mHelper.consumeAsync(inventory.getPurchase(SKU_MONTHLY), mConsumeFinishedListener); 
      mSubscribedTo = true; 
     } else if (gasYearly != null) { 
      mHelper.consumeAsync(inventory.getPurchase(SKU_ANNUALY), mConsumeFinishedListener); 
      mSubscribedTo = true; 
     } else if (gasQuarterly != null) { 
      mHelper.consumeAsync(inventory.getPurchase(SKU_QUARTERLY), mConsumeFinishedListener); 
      mSubscribedTo = true; 
     } else if (gasHalfYearly != null) { 
      mHelper.consumeAsync(inventory.getPurchase(SKU_HALF_YEARLY), mConsumeFinishedListener); 
      mSubscribedTo = true; 
     } else { 
      mSubscribedTo = false; 
     } 
    } 
}; 
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = 
     new IabHelper.OnConsumeFinishedListener() { 
      public void onConsumeFinished(Purchase purchase, 
              IabResult result) { 

       if (result.isSuccess()) { 
        //API 
        remove_ads_btn_monthly.setEnabled(true); 
        remove_ads_btn_quarterly.setEnabled(true); 
        remove_ads_btn_halfyearly.setEnabled(true); 
        remove_ads_btn_yearly.setEnabled(true); 
       } else { 
        // handle error 
       } 
      } 
     }; 

現在,我想知道我怎麼可以自定義我的代碼支付每類申購的,如圖中的圖像。每個訂閱應該在時間段結束後自動激活。

謝謝。

+0

consumeAsync訂閱?沒辦法,你不應該使用。請閱讀文件。訂閱是不消費的 –

+0

我在上面提到我實現了這個託管產品。 – Siri

回答

1

您有兩種選擇來存儲無論服務器購買的時間(然後使用當前時間在您的應用中驗證它)或使用googleplay androidpublisherapi

在回覆中您將獲得PurchasedTime。

{ 
    "kind": "androidpublisher#productPurchase", 
    "purchaseTimeMillis": long, 
    "purchaseState": integer, 
    "consumptionState": integer, 
    "developerPayload": string 
} 

Android : inApp purchase receipt validation google play

Android : inApp purchase receipt validation (Part 2)

除此之外..你需要檢查queryInventory每個SKU的(你只消耗每月)。

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


      Purchase gasMonthly = inventory.getPurchase(SKU_MONTHLY); 
      Purchase gasYearly = inventory.getPurchase(SKU_YEARLY); 
      if (gasMonthly != null) { 
       mHelper.consumeAsync(inventory.getPurchase(SKU_MONTHLY), mConsumeFinishedListener); 
        mSubscribedTo = true; 
      } else if (gasYearly != null) { 
       mHelper.consumeAsync(inventory.getPurchase(SKU_YEARLY), mConsumeFinishedListener); 
        mSubscribedTo = true; 
      } else { 

        mSubscribedTo = false; 
      } 

編輯:

我怎麼能知道,認購天,之後 認繳的包裝?

您將在mPurchaseFinishedListener/mGotInventoryListener上獲得購買的sku項目,並將其保存到sharedpreferences或服務器上。

天驗證。您將從購買中獲得purchaseTime。購買使用purchase.getPurchaseTime()您必須將其存儲在您的應用程序本地或查詢購買的SKU每次獲得時間。

+0

你能告訴我,我應該只使用你的代碼還是我還需要更多的東西? – Siri

+0

nope。你需要使用androidpublisher api –

+0

對我來說這將是一個漫長的過程。我只想隨時啓用消耗任務。 – Siri

相關問題