ok~~ 看過In-App Purchase之Part 1之後,我們直接進入程式該如何寫吧。
1.首先,跟其他使用方法一樣,一定要先require store這個Library
-----------------------------------------
store = require (“store”)
-----------------------------------------
2.然後,馬上就要設定store初始話,設定store的listener
-----------------------------------------
store.init(listener)
-----------------------------------------
3.可以向iTune下命令要索取可購買項目的資訊,在下命令之前,要先把您的In-App
Purchase資訊的ProductID準備好,放在一個table裡面。
-----------------------------------------
listOfProductID = {
“com.anscamobile.InAppPurchase.item001”,
“com.anscamobile.InAppPurchase.item002”,
“com.anscamobile.InAppPurchase.item002”
}
-----------------------------------------
然後按照以下語法,下命令去索取資訊,當有資訊返回(event)時,
去找listener(callback function)的function執行動作
store.loadProducts(listOfProductID, listener)
執行此命令後,會回傳以下參數:
event.product
可以購買的物件參數,包含title, description, price, 以及Product ID,都是你在iTune connect裡面設定的資訊
event.invalidProduct
如果你設定的listOfProductID裡面,有包含有問題的或者不存在的項目,就會把無法購買的項目回傳,以檢視你的listOfProduct是不是有問題
因此,在 store.loadProducts的listener裡面,就要把這些資訊儲存起來(尤其是price資訊,因為各國的幣值都不相同,你總不能叫歐洲也用美金買,他會提供歐元的數值,在iTune connect裡面也有說明)
範例程式:
-----------------------------------------
store = require (“store”)
store.init(transactionCallback)
local validProducts, invalidProducts = {}, {}
listOfProductID = {
“com.anscamobile.InAppPurchase.item001”,
“com.anscamobile.InAppPurchase.item002”,
“com.anscamobile.InAppPurchase.item002”
}
store.loadProducts( listOfProducts, loadProductsCallback )
local function loadProductsCallback( event )
validProducts = event.products
invalidProducts = event.invalidProducts
creat_InApp_button()
for i=1, #validProducts do
print(validProducts[i].title)
print(validProducts[i].price)
print(validProducts[i].description)
end
end
-----------------------------------------
以上範例程式,就可以把你在iTune connect裡面設定的購買項目資訊給取出。我在 loadProductsCallback( event )裡面偷藏了一個 creat_InApp_button()這個function,這是幹嘛的呢?你有這些資訊之後,當然需要建立出GUI給玩家選擇吧,不然玩家怎麼買,所以當取得資訊之後(這步驟也確定網路連線與AppID正確),就可以建立相關button或者list提供玩家點選購買。
那當玩家點選購買之後,首先,建議先檢查這個device(iPhone或者iPod)是否可以購買(因為會有父母封鎖小孩手機的購買能力,所以要先檢查),可以利用 store.canMakePurchases 來取得可否購買資訊,如果可以購買,那會return true,檢查範例如下:
-----------------------------------------
if store.canMakePurchases then
store.purchase( {product} )
else
native.showAlert("Store purchases are not available, please try again later",
{ "OK" } )
end
-----------------------------------------
如果可以購買,程式就會下命令 store.purchase( ProductID ) 去購買,然後返回的資訊會回到你上面設定的store.init(listener)的callback function ( transactionCallback)。這邊的程式,主要處理以下幾件事:
1. 成功購買物件(via store.purchase())
2. 玩家取消購買物件(after store.purchase() was called)
3. 購買物件失敗(也會回傳原因)(via store.purchase())
4. 前一次的購買被中斷(可能被電話來中斷),App store會繼續購買
因此,會回傳 event.transaction 參數,包含:
state
productIdentifier
receipt
identifier
originalReceipt
originalIdentifier
originalDate
errorType
errorString
因此,從 event.transaction.state可以得知,玩家購買的狀況是purchased(購買成功)、restored、cancelled(玩家取消購買)、failed(購買失敗)、或者回傳是unknown的資訊。
依照回傳的state分門別類記錄起來。例如:玩家購買成功,那corona程式裡面自行要記錄玩家購買了什麼東東,因為In-App Purchase只提供購物的平台,並不會幫你的程式記錄購買了哪些東西、買了幾個東西。當然,如果是一次性的物品,再次購買時,In-App Purchase系統會告知已經買過了,可以再次下載。但是在程式中一定都要記錄購買了哪些東西。
然而,玩家購買到哪一項物品,In-App Purchase會透過 event.transaction.Identifier回傳是哪一個物品ID被購買,所以要自行判斷哪一個物品被買到,例如:補充藥水被買到,數量要增加並記錄起來。
transactionCallback範例程式:
-----------------------------------------
--利用 identify_buy_what程式判別哪一項物品被買到,並傳回被買到的index
local function identify_buy_what(productIdentifier)
local index=0
if productIdentifier == listOfProducts[1] then
index=1
elseif productIdentifier == listOfProducts[2] then
index=2
else
index=0
end
return index
end
--以下為 transactionCallback程式,需要執行相對應的動作
--"purchased"購買成功,需要知道哪一項物品被購買,然後記錄起來
--"restored"
--"cancelled" 玩家取消購買
--"failed" 購買失敗,可以列出失敗原因給玩家了解
local function transactionCallback( event )
local index=0
if event.transaction.state == "purchased" then
-- 檢查是哪一項物品被購買到,傳回index
index = identify_buy_what(event.transaction.productIdentifier)
--依照不同物品被買到,程式需有相對應的記錄或動作
if index ==1 then
description = "Transaction successful! \n"
description = description .. "You already buy " ..
validProducts[index].title
elseif index == 2 then
description = "Transaction successful! \n"
description = description .. "You already buy " ..
validProducts[index].title .. "\n"
else
--也許購買成功但有出現其他問題,儘量列出給玩家看
description = "I don't know what you are buying.\n"
description = description .. "Please try again later or connect to Apple.\n"
end
elseif event.transaction.state == "restored" then
-- Reminder: your app must store this information somewhere
-- Here we just display some of it
description = "Restoring transaction:" .. "\n Original ID: " ..
event.transaction.originalTransactionIdentifier ..
"\n Original date: " .. event.transaction.originalDate
elseif event.transaction.state == "cancelled" then
-- 玩家取消購買物品
index = identify_buy_what(event.transaction.productIdentifier)
description = "Transaction cancelled by user. \n"
description = description .. "Buying " .. validProducts[index].title .. " failed."
elseif event.transaction.state == "failed" then
-- 購買失敗,列出失敗原因
description = "Transaction failed, type: ",
event.transaction.errorType, event.transaction.errorString
else
-- 若event.transaction.state回傳奇怪資訊,將亦為一種Error,也須列給玩家了解
despriction = "Unknown event"
end
-- 最後,需要告知store 已經完成購買動作,等待下一次購買,若有下載的動作,最好等待程式下載完畢在執行次列程式。
store.finishTransaction( event.transaction )
end
-----------------------------------------
因此,整體的In-App Purchase動作,將如以下程序建立:
1. local store = require("store")
2. store.init (transactionCallback )
3. store.loadProducts( listOfProducts, loadProductsCallback )
4. setupStore()取得可購買物品的資訊後,需建立相關按鈕,或者list提供玩家購買
5. 在setupStore()建立的按鈕或list裡面,若玩家選擇購買某物品,則下命令store.purchase( {product} ),並等待回應
6. 下購買命令後,系統回應資訊會找到 store.init (transactionCallback )所設定的listener,因此需要 function transactionCallback( event )針對購買的結果作相對應動作。
7. 告知store以購買完畢 store.finishTransaction( event.transaction )
整體範例程式,可以直接參考Corona SDK官方的In-App Purchase程式。
有需要我貼出我自己的程式的話,在留言給我唄。
2011年5月16日 星期一
Corona SDK -- Apple In-App Purchase -- Part 2
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言