Here's some more information about how to use these methods. Normally your app will have a screen that offers the option to upgrade. Before displaying this screen, you would call GetUpgrade and wait for the ChannelStoreEvent message. Then extract the information you need from the message (the price at least) and display the offer. If the user accepts the offer, call DoUpgrade to complete the purchase, and check the return code from DoUpgrade to determine whether the user completed the purchase or cancelled it.
Here is some skeleton code that does this. It would need to be fleshed out with text appropriate to your app, and possibly modified if your upgrade offer is not on a separate screen like this.
' ---------------------------------------------------------------------
function ShowPurchaseScreen() as Void
' First screen: wait for GetUpgrade from Channel Store
store = CreateObject("roChannelStore")
if store = invalid then return
screen1 = CreateObject("roParagraphScreen")
upgrade = ScreenGetUpgrade(screen1, store)
if upgrade = invalid then return
' Second screen: buy offer
screen2 = CreateObject("roParagraphScreen")
if not ScreenPurchase(screen2, screen1, store, upgrade) then return
' Third screen: thanks for buying
screen3 = CreateObject("roParagraphScreen")
ScreenThanks(screen3, screen2)
end function
' ---------------------------------------------------------------------
function ScreenGetUpgrade(screen as Object, store as Object) as Dynamic
msgport = CreateObject("roMessagePort")
screen.SetMessagePort(msgport)
screen.AddParagraph("Please wait a moment...")
screen.Show()
store.SetMessagePort(msgport)
store.GetUpgrade()
timer = CreateObject("roTimeSpan")
timer.Mark()
while true
msg = wait(1000, msgport)
if type(msg) = "roChannelStoreEvent" then
if msg.IsRequestSucceeded() then
items = msg.GetResponse()
if items <> invalid and items.count() > 0 then return items[0]
exit while
elseif msg.IsRequestFailed() then
print "channel store req failed: ";msg.GetStatusMessage()
exit while
end if
elseif type(msg) = "roParagraphScreenEvent" then
if msg.IsScreenClosed() then return invalid
end if
if timer.TotalMilliseconds() > 8000 then
print "Channel Store timeout"
exit while
end if
end while
return invalid
end function
' ---------------------------------------------------------------------
function ScreenPurchase(screen as Object, old_screen as Object, store as Object, upgrade as Object) as Boolean
msgport = CreateObject("roMessagePort")
screen.SetMessagePort(msgport)
screen.AddParagraph("Do you want to upgrade?")
screen.AddButton(1, "Buy for " + upgrade.cost)
screen.Show()
old_screen.Close()
while true
msg = wait(0, msgport)
if type(msg) = "roParagraphScreenEvent" then
if msg.IsScreenClosed() then
return false
elseif msg.IsButtonPressed()
button = msg.GetIndex()
if button = 1 then ' Buy
item = store.DoUpgrade()
if item <> invalid and item <> "" then
print "buy succeeded, item ";item
return true
end if
print "buy failed"
end if
end if
end if
end while
end function
' ---------------------------------------------------------------------
function ScreenThanks(screen as Object, old_screen as Object) as Void
msgport = CreateObject("roMessagePort")
screen.SetMessagePort(msgport)
screen.AddParagraph("Thank you for upgrading.")
screen.AddParagraph("Press OK to return to the Roku home screen.")
screen.AddButton(1, "OK")
screen.Show()
old_screen.Close()
while true
msg = wait(0, msgport)
if type(msg) = "roParagraphScreenEvent" then
if msg.IsScreenClosed() then
return
elseif msg.IsButtonPressed()
return
end if
end if
end while
end function
--Mark