2013年2月28日 星期四

in-App Purchase with Corona(iOS)

不論是遊戲或是工具類的APP

在APP STORE 都有著一個趨勢

"使用者不喜歡付費下載"

是的!大家看到要付錢的APP,經常先略過,看看有沒有免費的

功能差不多的,湊合用就好(至少島民是如此....)

這也是為什麼遊戲幾乎都是免費下載,然後收取內部的道具費用

雜誌平台也是收取每本雜誌的購買費(APP都是Free)

今天粗淺介紹in-App在iOS如何使用

有些前置作業要先完成(這裡將探討紅色內容)
  1. 購買開發者帳號
  2. 填好銀行資訊(匯款用),稅號(台灣不用填)
  3. 在iOS provisioning Protal 中App ID 選項裡建立APP ID
  4. 建立該APP ID 對應的provisioning profile
  5. 在iTunes Store 使用該bundle ID 建立一個APP
  6. 依類型增加可購買道具
  7. 建立測試帳號
Setp5:
選取Manage your Application
新增 1 APP
Step6:增加道具
進入APP內容點選Manage In-APP Purchases
必須先建好欲販賣的道具,在程式裡才能夠從apple 方讀取相關資料

道具分為以下幾種

  • Consumable:一次性道具,如遊戲金幣
  • NonConsumable:永久性道具,如永久經驗加倍
  • Auto-Renewable Subscriptions:自動更新,如雜誌訂閱
  • Free Subcription:ONLY書報攤使用,如免費刊物
  • Non-Renewing Subscription:限定時間道具
選擇販賣道具種類

在這我們示範一個Consumable道具,販賣5金幣

底下是新增道具要填的欄位

Reference Name:開發者參考用

Product ID:程式內呼叫使用,建議使用APP id 後再增加如com.abc.coin5(這裡用coin5)

Cleared For Sale: yes - 可購買, no - 不可購買

Price Tier: 欲販售價格(這裡選Tier 1 - 30台幣,1美元),會根據國家不同秀出不同幣值金十口

再來增加語言(根據OS不同語言將會秀出不同文字)
Step7:新增測試帳號

為了在測試時能夠模擬購買的動作

所以需要增加一個測試帳號,此帳號不能為現有apple 帳號

必須要是一個未使用的email

進入Manage Users 選項
選擇Test User
新增測試帳號
新增成功後,會在該申請E-mail收到一封Apple 寄來的認證信

點擊進入後使用該mail 及 password 做登入完成登入

做到這裡,前置動作就算完成,可以來做程式方面對應的CODING了

程式會用到的兩張圖buttonBuy.png,buttonBuyDown.png

local widget = require ("widget") -- build #1034 之前適用
local store = require("store") -- build #261 後適用
local validProducts, invalidProducts = {}, {} -- 接收有效和無效產品用
-------------------------------------------------------------------------------
-- 處理從商店取得的產品資訊,對每個產品建立按鈕
-------------------------------------------------------------------------------
function unpackValidProducts()
-- 建立購買按鈕
function newBuyButton (index)
local buyThis = function ( product )
-- 判斷是否能夠購買
if store.canMakePurchases then
store.purchase( {product} )
else
native.showAlert("無法購買,稍後再試!",
{ "OK" } )
end
end
-- myButton購買呼叫
function buyThis_closure ( index )
return function ( event )
buyThis (validProducts[index].productIdentifier)
return true
end
end
local myButton = widget.newButton{
left = display.contentWidth*.8,
top = display.contentHeight*.5,
yOffset = -1,
label = validProducts[index].title .. " " ..string.format("%.2f", validProducts[index].price),
default = "buttonBuy.png",
over = "buttonBuyDown.png",
fontSize = 14,
width = display.contentWidth*.4, height = display.contentWidth*.1,
onRelease = buyThis_closure (index)
}
return myButton
end
if not validProducts then
native.showAlert( "In App features not available", "initStore() failed", { "OK" } )
else
-- 列出有效產品按鈕
for i=1, #validProducts do
local myButton = newBuyButton(i)
myButton.x = display.contentWidth - myButton.width - 5
myButton.y = i * 5 + (2 * i - 1) * myButton.height / 2
end
-- 列出無效產品
for i=1, #invalidProducts do
native.showAlert( "Item " .. invalidProducts[i] .. " is invalid.",
{ "OK" } )
end
end
end
-------------------------------------------------------------------------------
-- 取得產品資訊,呼叫 store.loadProducts() 後被喚醒
-------------------------------------------------------------------------------
function loadProductsCallback( event )
validProducts = event.products
invalidProducts = event.invalidProducts
unpackValidProducts ()
end
-------------------------------------------------------------------------------
-- 專門處理交易的事件,在store.init()裡做設置
-------------------------------------------------------------------------------
function transactionCallback( event )
if event.transaction.state == "purchased" then
native.showAlert("state",event.transaction.state,{"ok"})
native.showAlert("",event.transaction.productIdentifier,{"ok"})
elseif event.transaction.state == "restored" then
native.showAlert("restore","restored",{"ok"})
elseif event.transaction.state == "cancelled" then
native.showAlert("cancelled","Transaction cancelled by user.",{"ok"})
elseif event.transaction.state == "failed" then
native.showAlert("failed","Transaction failed, type: ",
event.transaction.errorType, event.transaction.errorString,{"ok"})
else
native.showAlert("Unknown event","Unknown event",{"ok"})
end
-- 告訴商店已完成交易,若有提供下載的資料,下載完後再呼叫
store.finishTransaction( event.transaction )
end
-- 產品列表,有列出的才會做讀取
local listOfProducts =
{
"coin5",
}
-- 連結到商店,需設定CallBack
store.init (transactionCallback )
store.loadProducts( listOfProducts, loadProductsCallback )
view raw main.lua hosted with ❤ by GitHub

**此程式必須要Build For Device 才能執行

**在測試前要先進iTunes與App Store 將Apple ID 登出,
登出後不需要登入,待打開程式,點取購買時再登入

點選買5金幣,會告訴你是在Sandbox(測試環境)
初次點選購買,要登入帳號,在這裡登入測之前的測試帳號


如果取消,將會進入transactionCallback的cancelled狀態

成功購買將會進入purchased狀態

在purchased狀態中,印出購買的productIdentifier-coin5

從判斷是否進入purchased 可得知是否成功付款,成功購買後,就可以在後面加上金幣增加動作囉

希望大家in-app 賺大錢囉:D

來自小島

沒有留言:

張貼留言