您好?
這次要透過 Unity 的框架之一 Prime31 來串接應用程式內購買(In-App Purchase)功能。
Prime31 擁有 StoreKit 和 Google IAB 兩個管理員。
我們分別使用
#if UNITY_IOS || UNITY_IPHONE
#elif UNITY_ANDROID
#endif
來建構整合管理員,並發行相同的產品金鑰(Product Key)來控制相關商品的購買。
步驟如下:
1. 匯入 Prime31(Android:ver. 2.10,iOS:ver 2.15)
Prime 31 的 iPhone 與 Android 外掛版本不同。
2. 測試 IAP(iOS)
var listProducts = new string[2]; listProducts[0] = "com.test.crystal100"; listProducts[1] = "com.test.crystal200"; StoreKitManager.productListReceivedEvent += ProductListReceived; //監聽購買事件 StoreKitManager.purchaseSuccessfulEvent += PurchaseSuccessful; StoreKitManager.purchaseFailedEvent += PurchaseFail; StoreKitManager.purchaseCancelledEvent += PurchaseCancelled; //請求產品列表 StoreKitBinding.requestProductData(listProducts);
簡單說明程式碼:
listProducts 是在 iTunes 註冊的產品 ID。對該部分執行 requestProductData 時,會進行檢查以確認該產品是否存在。
您可以在 ProductListReceived 回呼(Callback)中設定資料。
Purchase 函式部分已統一處理 IAB 與 IAP。
3. 測試 IAB(Android)
GoogleIAB.init( key ); GoogleIAB.setAutoVerifySignatures (true); GoogleIABManager.billingSupportedEvent += billingSupportedEvent; GoogleIABManager.billingNotSupportedEvent += billingNotSupportedEvent; GoogleIABManager.queryInventorySucceededEvent += queryInventorySucceededEvent; GoogleIABManager.queryInventoryFailedEvent += queryInventoryFailedEvent; GoogleIABManager.purchaseCompleteAwaitingVerificationEvent += purchaseCompleteAwaitingVerificationEvent; GoogleIABManager.purchaseSucceededEvent += purchaseSucceededEvent; GoogleIABManager.purchaseFailedEvent += purchaseFailedEvent; GoogleIABManager.consumePurchaseSucceededEvent += consumePurchaseSucceededEvent; GoogleIABManager.consumePurchaseFailedEvent += consumePurchaseFailedEvent;
4. 新增類別 IAPManager -> 包含 "GoogleIAB", "StoreKitManager"
使用以下指令進行分歧處理:
#if UNITY_IOS
#elif UNITY_ANDROID
#endif
之後的購買部分則採用共用程式碼處理。
將 Android 與 iOS 的產品 ID 設定一致會比較方便管理。
product_id 建議不要建立太多,依種類(分等級)建立幾個即可,並在自有的資料庫中靈活運用。
5. 實作收據驗證伺服器
採用 PHP 處理。iOS 的情況下,僅需取得 transaction id 即可驗證;而 Google 方面則必須使用 API,因此需要 client-id、secret-key 及 token。(需要準備的東西較多,也較花時間。)
6. 結論
如果說在開發過程中耗時最長的部分,那就是:
Error
The item you requested is not available for purchase
這個錯誤訊息。
從 queryInventorySucceededEvent 內可以順利接收到我註冊的商品列表來看,應用程式與 Developer Console 之間的連接應該是正常的。但是,該錯誤仍然一直出現。
為了解決這個問題,我採取了以下對策:
1. 檢查 APK 的 version code 與 version name(清單中的 version code 與 version name 必須一致)。
2. 檢查權限設定(Permission Setting)。
最終我解決了。
首先,為了判斷測試是否可行,必須先確認是否能透過連結下載。若點擊連結出現 404 not found,代表尚未啟用(此時似乎還沒生效)。就我而言,連結啟用後就能正常運作了。連結啟用大約花了一天的時間。