Google play內購 Iab
(原文地址:http://blog.csdn.net/yupu56/article/details/49452107)
Google Play 內購 In-App-Billing在Android專案或者Cocos2dx/Unity專案中的整合.
最近在做一個遊戲的海外版,需要加內購,碰到一些坑,這裡記錄下來,希望能對大家有個幫助。
參考教程:
Google Play In-app Billing官方教程Google Play In-app Billing 踩過的那些坑
StackOverflow 論壇
Google Play 內購In-app-billing 總結~
開發者需要做的準備:
1.翻牆Android手機和電腦。
2.Google play 後臺應用,並且把內購專案建立好併發布成功。能夠得到內購產品的SKU即ProductID,和專案64位的祕鑰。
3.內購產品的說明:
a.產品的id是唯一的字串定義,比如com.engine.produce01,後臺新增產品後需要啟用。
b.In-app Billing 的 API 有個 v2 版本和 v3 版本,v2 版本已經不支援了,直接整 v3 版本的吧,Google Play 沒有可重復購買商品這個概念,所有的“商品/充值檔”使用者成功購買過一次之後就不允許再次購買了。所以為了實現像應用內購買充值這種可重複購買的“商品/充值檔”,Google Play 提供了一個“消耗”藉口(Consuming In-app Products)。使用者購買完商品後,調一下“消耗”介面,這樣使用者下次就可以繼續購買了。
使用IAB的流程:
1.首先確定你的SKU和Request值(隨便填)
- <pre name="code" class="java">static final String SKU_PACKAGE1 = "android.test.purchased";
- static final String SKU_PACKAGE2 = "cinderella_product02";
- static final String SKU_PACKAGE3 = "cinderella_infinite";
- // (arbitrary) request code for the purchase flow
- static final int RC_REQUEST = 10001;
2.IabHelper類初始化方法,這裡的base64EncodedPublicKey是googleplay後臺的釋出產品的時候生成提供的
- mHelper=new IabHelper(this, base64EncodedPublicKey);
3.startSetup 的操作是檢查是否有許可權和連線到Google Billing service是否成功.
這裡回撥的操作是如果成功,呼叫queryInventoryAsync檢視產品id是否可以使用,查詢完成後會呼叫IabHelper.QueryInventoryFinishedListener 這個回撥介面進行通知,在這個介面中可以獲取商品的詳細資訊SkuDetails和Purchase資訊。
- 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;
- // IAB is fully set up. Now, let's get an inventory of stuff
- // we own.
- Log.d(TAG, "Setup successful. Querying inventory.");
- mHelper.queryInventoryAsync(mGotInventoryListener);
- }
- });
4.點選購買按鈕呼叫方法,主要是mHelper.launchPurchaseFlow()方法
- // User clicked the "Buy Gas" button
- public void onBuyGasButtonClicked(View arg0) {
- Log.d(TAG, "Buy gas button clicked.");
- // launch the gas purchase UI flow.
- // We will be notified of completion via mPurchaseFinishedListener
- setWaitScreen(true);
- Log.d(TAG, "Launching purchase flow for gas.");
- /*
- * TODO: for security, generate your payload here for verification. See
- * the comments on verifyDeveloperPayload() for more info. Since this is
- * a SAMPLE, we just use an empty string, but on a production app you
- * should carefully generate this.
- */
- String payload = "";
- mHelper.launchPurchaseFlow(this, SKU_PACKAGE1, RC_REQUEST, mPurchaseFinishedListener, payload);
- }
5.verifyDeveloperPayload方法用來在伺服器做驗證的,起到確認訂單的作用,小遊戲就免了吧!
- /** Verifies the developer payload of a purchase. */
- 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.
- */
- return true;
- }
6.下面是執行完購買後的監聽方法
- // 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()) {
- 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_PACKAGE1)) {
- // bought 1/4 tank of gas. So consume it.
- Log.d(TAG, "Purchase1 is gas. Starting gas consumption.");
- mHelper.consumeAsync(purchase, mConsumeFinishedListener);
- } else if (purchase.getSku().equals(SKU_PACKAGE2)) {
- // bought the premium upgrade!
- Log.d(TAG, "Purchase2 is premium upgrade. Congratulating user.");
- mHelper.consumeAsync(purchase, mConsumeFinishedListener);
- } else if (purchase.getSku().equals(SKU_PACKAGE3)) {
- // bought the premium upgrade!
- Log.d(TAG, "Purchase3 is premium upgrade. Congratulating user.");
- }
- }
- };
7.執行完購買回撥後,消耗型商品需要呼叫消耗方法
這句的意思就是消耗掉你剛買的商品,消耗是指在googleplay上的消耗,為什麼呢?因為GooglePlay 的In-app-Billing V3.0版本,已經沒有管理,非管理的商品,或者像蘋果iOS那邊消耗性和非消耗性的商品了,在後臺新建商品的時候,你會發現全部是受管理的商品,所以在我們購買了消耗型的商品後,在程式碼中執行mHelper.consumeAsync(purchase,mConsumeFinishedListener);就行了,代表這個商品被消耗了,你還可以購買。下面是消耗後的回撥方法:
- // Called when consumption is complete
- IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
- public void onConsumeFinished(Purchase purchase, IabResult result) {
- Log.d(TAG, "Consumption finished. Purchase: " + purchase + ", result: " + result);
- // if we were disposed of in the meantime, quit.
- if (mHelper == null)
- return;
- // We know this is the "gas" sku because it's the only one we
- // consume,
- // so we don't check which sku was consumed. If you have more than
- // one
- // sku, you probably should check...
- if (result.isSuccess()) {
- // successfully consumed, so we apply the effects of the item in
- // our
- // game world's logic, which in our case means filling the gas
- // tank a bit
- Log.d(TAG, "Consumption successful. Provisioning.");
- //saveData();
- } else {
- complain("Error while consuming: " + result);
- }
- updateUi();
- setWaitScreen(false);
- Log.d(TAG, "End consumption flow.");
- }
- };
8.最後補充一點,官方例子的方法設計非常合理,一些輔助方法的書寫和使用,很經典,值得我們的借鑑。
- // updates UI to reflect model
- public void updateUi() {
- }
- // Enables or disables the "please wait" screen.
- void setWaitScreen(boolean set) {
- findViewById(R.id.screen_main).setVisibility(set ? View.GONE : View.VISIBLE);
- findViewById(R.id.screen_wait).setVisibility(set ? View.VISIBLE : View.GONE);
- }
- void complain(String message) {
- Log.e(TAG, "**** TrivialDrive Error: " + message);
- alert("Error: " + message);
- }
- void alert(String message) {
- AlertDialog.Builder bld = new AlertDialog.Builder(this);
- bld.setMessage(message);
- bld.setNeutralButton("OK", null);
- Log.d(TAG, "Showing alert dialog: " + message);
- bld.create().show();
- }
相關文章
- Google Play 應用內購買限制國家地區政策更新Go
- 《抖音》及海外版《TikTok》在App Store和Google Play應用內購總收入突破7500萬美元APPGo
- IAB&埃森哲:企業內部廣告程式化購買報告
- IAB:媒體內部壓力影響程式設計(含RTB)購買普及程式設計
- Google Play 家庭政策深度解析Go
- Google Play 助力更多開發者成功Go
- Google Play 開發者賬戶被封Go
- Google 應用內購 (1): 申請身份證明Go
- Genymotion下安裝Google Play ServicesGo
- 全球Google Play市場分析報告Go
- Google Play 放置遊戲產品型別分析Go遊戲型別
- 和 Google Play 一起展望未來Go
- 如何下載 Google Play 應用的apkGoAPK
- 四月刊 | Google Play 開發者 FAQGo
- IAB:2021年歐洲廣告程式化購買報告
- iAB:智慧手機讓網路購物不再有假日淡季
- 為什麼 Google Play 也要推訂閱制?Go
- Google Play ASO之安卓APP頁面優化Go安卓APP優化
- 2018年Google Play Award釋出Go
- Google Play Store啟用AdWords搜尋廣告Go
- Apple Google Play排行榜演算法APPGo演算法
- IAB:2019年歐洲廣告程式化購買報告
- 開發者注意了,! Google Play兒童保護政策對應用內容與廣告提出了新要求Go
- Google:2015年Google Play最佳應用和最佳遊戲應用Go遊戲
- GDSA真是為了PK掉Google Play而生的嗎?Go
- 上傳APP到Google Play許可權問題APPGo
- 一起看 I/O | Google Play 更新一覽Go
- 首發 | Google Play 開發者 FAQ 第一期Go
- 剛剛,澳門Google Play應用商店上線啦!Go
- Google Play 和神奇女俠是什麼關係?Go
- 海信a7cc 解鎖root安裝google playGo
- [譯] Google Play 控制檯指南:Google Play 控制檯能為你做的都不僅僅是釋出應用這麼簡單而已Go
- 滿足 Google Play 目標 API 等級 (targetSdkLevel) 的要求GoAPI
- Google Play強制所有手遊分級,廣告也要分級Go
- Google Play將降低抽成比例,部分情況低至15%Go
- Google Play:2018年度最佳榜單出爐Go
- Google Play & App Store 年度最佳應用及遊戲盤點GoAPP遊戲
- 2023年Google Play商店最佳遊戲榜單Go遊戲