Corona中文站

强大、易学的跨平台(iOS/Android)开发框架。QQ群1:74390406(满) 群2:221929599

导航

在Corona中使用应用内收费(IAP)

点击下载源代码

第一步,在iTunes中创建应用程序,及添加产品价目表。

这个步骤相信大家摸索一下就会知道大概怎么做,直接上截图,给大家看一下每个项目大概怎么填的。

网上找的,仅供参考:

    Reference Name(参考名称): 产品的通用名称。比如,我使用的是 “Pro Upgrade”。此名称是不允许进行编辑的,它不会显示于App Store中。
    Product ID(产品ID): 你产品的唯一id。通常格式是 com.company.appname.product,但它可以说任何形式。它并不要求以程序的App ID作为前缀。
    Type(类型): 有三种选择
        Non-consumable(非消耗品): 仅需付费一次 (例如你希望将出现从免费版升级为专业版)
        Consumable(消耗品): 每次下载都需要付费
        Subscription(预订): 循环反复
    Price Tier(价格等级): 产品价格。参见不同等级的价格列表。
    Cleared for Sale(等待销售): 一定要选取此项,否则的话,测试时会发生非法产品ID的错误。
    Language to Add(增加的语言): 选一项。下列两项将出现:
        Displayed Name(显示名称): 用户看到的产品名称。比如我选择 “Upgrade to Pro”。
        Description(描述): 对产品进行描述。此处输入的文本将与Displayed Name 及 Price 一起在你代码中提取 SKProduct时出现。
    Screenshot(截屏): 展示你产品的截屏。尽管屏幕上会显示“提交截屏会触发产品审核过程”之类的文字(个人拙见,这是非常糟糕的设计),你还是可以安全地提交截屏而不会使产品进入审核过程。存储后,选择“Submit with app binary” (随程序二进制码一起提交)选项。是产品与程序二进制绑定在一起,所以在你最后正式提交100%完成的程序二进制码时,产品也会随之提交。

第二步,在程序中添加应用内收费

Corona提供了加载APP收费项目列表的API(store.loadProducts()),可以获取APP有哪些收费项目,好处是项目名称可以随时在itunes Connect中修改,缺点是网络不好时,获取列表时间比较长,对促进消费不太好,所以建议价目表写死在客户端,而且界面可以做的更好看。代码如下:

module(..., package.seeall)

local store = require("store")
local ui = require("ui")

local msgText

function clean ( event )
--print("screenAdd cleaned"))
end

local function showAI()
print("start")
native.setActivityIndicator(true)
local listener = function() native.setActivityIndicator(false) end
timer.performWithDelay(10000, listener)
end

local function button1Release(event)
showAI()
store.purchase{ "ohyeah.games.xqkp.product001" }
end

local function button2Release(event)
showAI()
store.purchase{ "ohyeah.games.xqkp.product003" }
end

local function button3Release(event)
showAI()
store.restore()
end

local listOfProducts = 
{
"ohyeah.games.xqkp.product001",
"ohyeah.games.xqkp.product003",
}

function transactionCallback( event )
local transaction = event.transaction
if transaction.state == "purchased" then
print("Transaction succuessful!")
print("productIdentifier", transaction.productIdentifier)
print("receipt", transaction.receipt)
print("transactionIdentifier", transaction.identifier)
print("date", transaction.date)
elseif  transaction.state == "restored" then
print("Transaction restored (from previous session)")
print("productIdentifier", transaction.productIdentifier)
print("receipt", transaction.receipt)
print("transactionIdentifier", transaction.identifier)
print("date", transaction.date)
print("originalReceipt", transaction.originalReceipt)
print("originalTransactionIdentifier", transaction.originalIdentifier)
print("originalDate", transaction.originalDate)
elseif transaction.state == "cancelled" then
print("User cancelled transaction")
elseif transaction.state == "failed" then
print("Transaction failed, type:", transaction.errorType, transaction.errorString)
else
print("unknown event")
end

store.finishTransaction( transaction )
native.setActivityIndicator(false)
end

function new()
store.init (transactionCallback )

localGroup = display.newGroup()
msgText = display.newText("", 50, 360, nil, 12)
print = function(txt) msgText.text = txt end

--Init
local button1 = ui.newButton{
default = "buttonGray.png",
over = "buttonGrayOver.png",
onRelease = button1Release,
text = "100钻 $0.99",
emboss = true
}
button1.x = screenW / 2
button1.y = 50
localGroup:insert(button1)

local button2 = ui.newButton{
default = "buttonGray.png",
over = "buttonGrayOver.png",
onRelease = button2Release,
text = "300钻 $2.99",
emboss = true
}
button2.x = screenW / 2
button2.y = 100
localGroup:insert(button2)

local button3 = ui.newButton{
default = "buttonGray.png",
over = "buttonGrayOver.png",
onRelease = button3Release,
text = "restore",
emboss = true
}
button3.x = screenW / 2
button3.y = 150
localGroup:insert(button3)

return localGroup
end

store.canMakePurchases(),主要是检测设备上是否禁用了IAP,呵呵,主要是防止小孩乱点乱买的,在store.purchase()之前可以进行判断。

store.restore(),如果你的程序提供的是非消耗性的或是订阅类的商品,就必须提供restore的功能,使用户可以在其他设备上重新存储购买信息。

<< 每周之星【2011.10.23】 - Car Seat Helper我们为什么要采用CoronaSDK【from csdn】 >>

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最近发表

Powered By Z-Blog 1.8 Walle Build 100427 Copyright 2011-2015 BuildApp.Net. All Rights Reserved.