我无法在我的版本2中测试inapp还原功能

时间:2013-06-03 06:42:47

标签: ios lua corona

我经过测试和测试在appStore上传我的应用程序它的inapp购买工作非常好,我也能够恢复应用程序。 但现在我想测试它的版本2,但现在能够购买,但恢复不起作用。

module(..., package.seeall)
require("store")
require("ui")
local inappfile = require("inappfile")
    local validProducts, invalidProducts = {}, {}
    local listOfProducts = 
    {
        -- These Product IDs must already be set up in your store
        -- We'll use this list to retrieve prices etc. for each item
        -- Note, this simple test only has room for about 4 items, please adjust accordingly

        -- The iTunes store will not validate bad Product IDs 
        "com.tinytapps.pandamath.fullpack",
    }
-------------------------------------------------   
    -- we store everything inside this group at the end
    local newActivity = display.newGroup()
    -----------------------------------------------------
---------------- IN APP CODE STARTS HERE ------------------

    checkIfPurchased = function()

        local filePath = system.pathForFile( "purchased.txt", system.DocumentsDirectory )
        local file = io.open( filePath, "r" )
        if file then
            _G["isPurchased"] = true
            --AdMediator.hide()
        else
            _G["isPurchased"] = false
            --local_configuration()
        end
    end

    callforpurchase = function()

        if _G["isPurchased"] == false then

            local cerateFile = function()   
                _G.noOfQuestionCompleted=0
                local filePath = system.pathForFile( "purchased.txt", system.DocumentsDirectory )
                local file = io.open( filePath, "r" )
                print(file)
                if file then
                    _G["isPurchased"] = true
                    io.close( file )
                else
                    file = io.open( filePath, "w" )
                    _G["isPurchased"] = true
                    io.close( file )

                end
            end

            function showTitle()
                if isSimulator then
                    local myAlert = native.showAlert(  "iTunes Store not available in Corona Simulator", 
                        "Offline testing: see console for output.", 
                        { "OK" } )
                end
            end

        -------------------------------------------------------------------------------
        --  Product IDs should match the In App Purchase products set up in iTunes Connect.
        --  We cannot get them from the iTunes store so here they are hard coded;
        --  your app could obtain them dynamically from your server.
        -------------------------------------------------------------------------------

        -------------------------------------------------------------------------------
        -- Process and display product information obtained from store.
        -- Constructs a button for each item
        -------------------------------------------------------------------------------

            function unpackValidProducts()

                local buttonSpacing = 5
                -- Utility to build a buy button

                --  Handler for buy button 
                function newBuyButton (index)
                    local buttonDefault = "add/buybtn.png"
                    local buttonOver = "add/buybtn.png"
                    local buyThis = function ( product )
                        print ("Purchasing " ..product)
                        -- Purchase the item
                        if store.canMakePurchases then
                            store.purchase( {product} )
                        else
                            native.showAlert("Store purchases are not available, please try again later", 
                                    { "OK" } )
                        end
                    end

                    function buyThis_closure ( index ) 
                        -- Closure wrapper for buyThis() to remember which button
                        return function ( event )
                            buyThis (validProducts[index].productIdentifier)         
                            return true
                        end
                    end

                    local hideDescription = function ( )
                        --descriptionArea.text = "Select a product..."
                    end

                    local describeThis = function ( description )
                        print ("About this product:  " ..description)
                    end

                    function describeThis_closure ( index )            
                        -- Closure wrapper for describeThis() to remember which button
                        return function ( event )
                            describeThis (validProducts[index].description)      
                            return true
                        end        
                    end

                        buyButton = ui.newButton{ 
                        default = buttonDefault, over = buttonOver,
                        onPress = describeThis_closure (index), onRelease = buyThis_closure (index),
                                            }
                        _G.buyButton=buyButton                  
                        return buyButton

                end

                -- Utility to build a restore button
                function newRestoreButton ()
                    local buttonDefault = "add/restorebtn.png"
                    local buttonOver = "add/restorebtn.png"
                    local restore = function ( product )
                        -- Ask the iTunes Store to initiate restore transaction sequence
                        print ("Restoring " )
                        store.restore()
                    end

                    local hideDescription = function ( )
                        --descriptionArea.text = "Select a product..."
                    end

                    local describeThis = function ()
                        -- Display info in description area
                        print ("Test restore feature")
                    end

                        --local label = "Test restore"
                        restoreButton = ui.newButton{ 
                        default = buttonDefault, over = buttonOver,
                        onPress = describeThis, onRelease = restore,
                                                }

                        _G.restoreButton = restoreButton                    
                        return restoreButton
                end


                print ("Loading product list")
                if not validProducts then
                    native.showAlert( "In App features not available", "initStore() failed", { "OK" } )        
                else
                    print ("Product list loaded")
                    print( "Country: " .. system.getPreference( "locale", "country" ) ) 
                    local buttonSpacing = 5
                    print( "Found " .. #validProducts .. " valid items ")
                    -- display the valid products in buttons 
                    for i=1, #validProducts do            
                        -- Debug:  print out product info 
                        print ("Item " .. i .. ": " .. validProducts[i].productIdentifier 
                                    .. " (" .. validProducts[i].price .. ")")
                        print (validProducts[i].title .. ",  ".. validProducts[i].description)
                    ---------------------------------------------------------------------------
                        -- create and position product button
                            local myButton = newBuyButton(i)
                            myButton.x = 570 --centerX - 200
                            myButton.y = 450 --centerY + 250
                            --myButton.xScale = 0.6
                            --myButton.yScale = 0.6
                            --newActivity:insert(buyButton)
                            myButton:toFront()

                        end 

                        local restoreButton = newRestoreButton()
                        restoreButton.x = 200 --centerX + 200
                        restoreButton.y = 450 -- centerY + 250  
                        --restoreButton.xScale = 0.6
                        --restoreButton.yScale = 0.6
                        --newActivity:insert(restoreButton)

                            restoreButton:toFront()

                    for i=1, #invalidProducts do
                        -- Debug:  display the product info 
                        native.showAlert( "Item " .. invalidProducts[i] .. " is invalid.",
                                    { "OK" } )
                        print("Item " .. invalidProducts[i] .. " is invalid.")
                    end

                end
            end

        -------------------------------------------------------------------------------
        -- Handler to receive product information 
        -- This callback is set up by store.loadProducts()
        -------------------------------------------------------------------------------

            function loadProductsCallback( event )
                -- Debug info for testing
                print("In loadProductsCallback()")
                print("event, event.name", event, event.name)
                print(event.products)
                print("#event.products", #event.products)
                io.flush()  -- remove for production

                -- save for later use
                validProducts = event.products
                invalidProducts = event.invalidProducts    
                unpackValidProducts ()

            end

        -------------------------------------------------------------------------------
        -- Handler for all store transactions
        -- This callback is set up by store.init()
        -------------------------------------------------------------------------------

            function transactionCallback( event )
                local infoString  
                print("transactionCallback: Received event ", event.name)
                if event.transaction.state == "purchased" then
                    cerateFile()
                    _G["isPurchased"] = true

                    --AdMediator.hide()
                    if buyButton ~= nil then    
                        buyButton=nil
                    end 
                    if restoreButton ~= nil then        
                        restoreButton=nil
                    end 
                    infoString = "Transaction successful!"
                    print (infoString)

                    inappfile.onClose()
                    print("transactionCallback() method called inappfile.onClose()")



                elseif  event.transaction.state == "restored" then
                    print ("Success! Product restored")
                    cerateFile()
                    _G["isPurchased"] = true

                    if buyButton ~= nil then    
                        buyButton=nil
                    end 
                    if restoreButton ~= nil then        
                        restoreButton=nil
                    end 
                    infoString = "Transaction successful!"
                    print (infoString)
                    inappfile.onClose()
                    print("transactionCallback() method called inappfile.onClose()")


                elseif event.transaction.state == "cancelled" then
                    infoString = "Transaction cancelled by user."

                elseif event.transaction.state == "failed" then        
                    infoString = "Transaction failed, type: ", 
                    event.transaction.errorType, event.transaction.errorString
                else
                    infoString = "Unknown event"
                end

                -- Tell the store we are done with the transaction.
                -- If you are providing downloadable content, do not call this until
                -- the download has completed.
                store.finishTransaction( event.transaction )
            end

        -------------------------------------------------------------------------------
        -- Setter upper 
        -------------------------------------------------------------------------------

            function setupMyStore (event)

                store.loadProducts( listOfProducts, loadProductsCallback )
                print ("After store.loadProducts, waiting for callback")

                if isSimulator then
                    -- No Store, so no callbacks, so exercise our GUI "manually"  
                    validProducts[1] = {}
                    validProducts[1].title = "Panda Math"
                    validProducts[1].description = "A wonderful product of Math for testing"
                    validProducts[1].price = 1.99
                    validProducts[1].productIdentifier = "com.tinytapps.pandamath.full"
                    unpackValidProducts()   
                end
            end

        -------------------------------------------------------------------------------
        -- Main
        -------------------------------------------------------------------------------

            -- Show title card
            showTitle ()

            -- Connect to store at startup
            store.init (transactionCallback )
            print ("After init")

            -- Hide title card, run store
            timer.performWithDelay (1000, setupMyStore)
        end
    end

0 个答案:

没有答案