GooglePlay內購In-app Billing 總結~
阿新 • • 發佈:2019-02-07
startSetup 的操作是檢查是否有許可權和連線到Google Billing service是否成功;這裡回撥的操作是如果成功,呼叫queryInventoryAsync檢視產品id是否可以使用;
查詢完成後會呼叫IabHelper.QueryInventoryFinishedListener 這個回撥介面進行通知,在這個介面中可以獲取商品的詳細資訊SkuDetails和Purchase資訊。
點選購買按鈕,需要呼叫的支付方法
- String payload = "";
-
mHelper.launchPurchaseFlow(Activity act, String sku, String itemType, int
- boolean verifyDeveloperPayload(Purchase p) {
- String payload = p.getDeveloperPayload();
- /*
- * TODO: verify that the developer payload of the purchase is correct. It will be
-
* the same one that you sent when initiating the purchase.
- *
- * WARNING: Locally generating a random string when starting a purchase and
- * verifying it here might seem like a good approach, but this will fail in the
- * case where the user purchases an item on one device and then uses your app on
-
* a different device, because on the other device you will not have access to the
- * random string you originally generated.
- *
- * So a good developer payload has these characteristics:
- *
- * 1. If two different users purchase an item, the payload is different between them,
- * so that one user's purchase can't be replayed to another user.
- *
- * 2. The payload must be such that you can verify it even when the app wasn't the
- * one who initiated the purchase flow (so that items purchased by the user on
- * one device work on other devices owned by the user).
- *
- * Using your own server to store and verify developer payloads across app
- * installations is recommended.
- */
- returntrue;
- }
verifyDeveloperPayload這個方法是在支付完成的時候在回撥裡頭去驗證用的,關於payload的生產,看上面官方給的demo的註釋,大概理解了下:
1.不同的玩家所生成的payload需要不一樣
2.即使玩家在不同裝置上初始化payload,也要可以通過~
大概是這個意思(如果翻譯有問題~砸磚吧~)
下面是支付方法回撥的監聽:
- IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
- publicvoid onIabPurchaseFinished(IabResult result, Purchase purchase) {
- Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
- if (result.isFailure()) {
- complain("Error purchasing: " + 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_GAS)) {
- // bought 1/4 tank of gas. So consume it.
- Log.d(TAG, "Purchase is gas. Starting gas consumption.");
- //購買成功,呼叫消耗產品
- mHelper.consumeAsync(purchase, mConsumeFinishedListener);
- }
- elseif (purchase.getSku().equals(SKU_PREMIUM)) {
- // bought the premium upgrade!
- Log.d(TAG, "Purchase is premium upgrade. Congratulating user.");
- alert("Thank you for upgrading to premium!");
- mIsPremium = true;
- updateUi();
- setWaitScreen(false);
- }
- elseif (purchase.getSku().equals(SKU_INFINITE_GAS)) {
- // bought the infinite gas subscription
- Log.d(TAG, "Infinite gas subscription purchased.");
- alert("Thank you for subscribing to infinite gas!");
- mSubscribedToInfiniteGas = true;
- mTank = TANK_MAX;
- updateUi();
- setWaitScreen(false);
- }
- }
- };
在IabHelper.OnConsumeFinishedListener的回撥用於處理成功呼叫成功支付的邏輯(這裡可能還需要一步去呼叫遠端伺服器驗證)