_InAppPurchase

Google和Apple内购功能 全部展开

只读属性展开

  • + canPayment : bool

    Apple手机是否开启内购

    示例

    print(_iap.canPayment)

单例展开

  • + _iap : _InAppPurchase

静态属性展开

  • + ArgumentsInvalid : uint

    失败原因,google平台上表示参数错误,apple平台请忽略此属性

  • + Cancelled : uint

    失败原因,用户取消购买。

  • + ClientInvalid : uint

    失败原因,客户端不可用,无法使用内购功能。

  • + FinishFailed : uint

    结束失败。

  • + FinishSuccess : uint

    结束成功。

  • + InitFailed: uint

    初始化失败。

  • + InitSuccess : uint

    初始化成功。

  • + NotAllowed : uint

    失败原因,无法购买或结束购买,google平台上同一个商品在结束购买前不允许再次购买;没有购买的商品不允许结束购买。apple平台上表示设备不允许内购

  • + NotAvailable : uint

    失败原因,apple平台上表示商品不可用,google平台请忽略此属性

  • + PayFailed : uint

    购买失败。

  • + PaymentInvalid : uint

    失败原因,商品不存在。

  • + PaySuccess : uint

    购买成功。

  • + RemoteException : uint

    失败原因,网络错误

  • + UNKNOWN : uint

    失败原因,未知。

  • + VerificationFailed : uint

    失败原因,google平台上表示签名验证失败,apple平台请忽略此属性

公共方法展开

  • + function finishFail(transaction: string)

    购买失败之后的结束购买,只有Apple苹果使用此方法

    • 参数
    • transaction : 回调透传给客户端的标识,直接传入此方法

    示例

    _iap:finishFail(transaction)

  • + function finishSuc(transaction: string)

    购买成功之后的结束购买

    • 参数
    • transaction : 回调透传给客户端的标识,直接传入此方法

    示例

    _iap:finishSuc(transaction)

  • + function init([publickey : string], callback)

    初始化

    • 参数
    • publickey : google后台获取的应用key,可选参数,Apple不需要传此参数
    • callback : 回调,用来返回初始化、购买、结束购买的结果

    示例

    _iap:init(publickey,callback)

    _iap:init(callback)

  • + function purchase(productid : string, count : number)

    发起购买

    • 参数
    • productid : 后台配置的商品id
    • count : 购买数量,google平台可忽略此参数

    示例

    _iap:purchase(productid,count)

代码示例

    --Google示例
    --result,回调事件的类型
    --transaction,用于成功购买后结束此次交易的参数
    --transactionid,谷歌返回的订单号
    --productid,购买产品的ID
    --count,购买产品的数量
    --token,谷歌返回的json数据,用于服务器向谷歌校验订单
    --errorcode,失败的类型
        -- _InAppPurchase.UNKNOWN,未知错误
        -- _InAppPurchase.ClientInvalid,手机支付插件不可用
        -- _InAppPurchase.Cancelled,用户取消
        -- _InAppPurchase.PaymentInvalid,productid不存在
        -- _InAppPurchase.NotAllowed,无法购买或结束购买,同一个产品在结束购买前不允许再次购买;没有购买的商品不允许结束购买
        -- _InAppPurchase.NotAvailable,谷歌支付请忽略
        -- _InAppPurchase.ArgumentsInvalid,参数不可用
        -- _InAppPurchase.RemoteException,网络错误
        -- _InAppPurchase.VerificationFailed,签名验证失败
    --ignore,如果不是upay支付,请忽略此参数
    --signature,发送给服务器,判断此次购买是否被篡改
    callback = function(result,transaction,transactionid,productid,count,token,errorcode,ignore,signature)
        print('result:'..result,'transaction:'..transaction,'transactionid:'..transactionid,'productid:'..productid,'count:'..count,'token:'..token,'errorcode:'..errorcode,'ignore:'..ignore,'signature:'..signature)

        if result == _InAppPurchase.InitSuccess then
            --初始化成功
        elseif result == _InAppPurchase.InitFailed then
            --初始化失败,可以根据errorcode给用户提示
        elseif result == _InAppPurchase.PaySuccess then
            --购买成功
            --发送token,signature给服务器,让服务器验证订单状态
            --游戏逻辑

            --结束此次购买
            print('结束此次购买')
            _iap:finishSuc(transaction)
            end
        elseif result == _InAppPurchase.PayFailed then
            --购买失败
            --根据errorcode提示给用户
        elseif result == _InAppPurchase.FinishSuccess then
            --结束购买成功
            --游戏逻辑
        elseif result == _InAppPurchase.FinishFailed then
            --结束购买失败
            --根据errorcode提示给用户
        end
    end

    --productid,在谷歌后台获取
    productid = 'gas'
    count = 1
    userdata = 'thisisasamplecode'
    --publickey,在谷歌后台获取
    publickey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvQUJPbui/udU2lklZQbcUKq8C+autMngaGLl64kQOhbl3uN/C6re003BFIaqTbiivGzmOVYdRXnTaUoobdhJaqGin6scZtbHr9QfYuOwGloKsQTJvQr4z15AGsIkgvwR945aIknBZNQ36PVUYSZkgztPhO/N4VdF1F9kSmtC49UYDuxcUWPWGxZQ8Zy6mPAT7y0AsnR4EWqYWVSIn5G+nvPJ94ryjxvrPbbAopvwE+xgdsAGWSrY5uOXyX2nPyW7itsPwIfPJix1Z9psXLAPM+KZEaN8QtKkVQgwoMSelz9RqP9hsMx9S9YNjla34xKWqiHhKPa/XPEFSHvlks4ShQIDAQAB'
    _app:onTouchEnd(function(x,y)
        if x < _rd.w/2 then
            _iap:init(publickey,callback)
        else
            _iap:purchase(productid,count,userdata)
        end
    end)
    --Apple示例
    --result,回调事件的类型
    --transaction,用于购买失败后结束此次交易的参数
    --transactionid,用于购买成功后结束此次交易的参数
    --productid,购买产品的ID
    --count,购买产品的数量
    --token,苹果返回的支付验证码,用于服务器向苹果校验订单
    --errorcode,失败的类型
        -- _InAppPurchase.UNKNOWN,未知错误
        -- _InAppPurchase.ClientInvalid,手机内购不可用
        -- _InAppPurchase.Cancelled,用户取消
        -- _InAppPurchase.PaymentInvalid,productid不存在
        -- _InAppPurchase.NotAvailable,商品不可用
        -- _InAppPurchase.ArgumentsInvalid,苹果支付请忽略
        -- _InAppPurchase.RemoteException,网络错误
        -- _InAppPurchase.VerificationFailed,苹果支付请忽略
    --ignore,如果不是upay支付,请忽略此参数
    --userdata,苹果支付请忽略
    callback = function(result,transaction,transactionid,productid,count,token,errorcode,ignore,userdata)
        print('result:'..result,'transaction:'..transaction,'transactionid:'..transactionid,'productid:'..productid,'count:'..count,'token:'..token,'errorcode:'..errorcode,'ignore:'..ignore,'userdata:'..userdata)

        if result == _InAppPurchase.PaySuccess then
            --购买成功
            --发送token给苹果服务器,验证订单状态,需游戏自行向苹果服务器发送验证,详情见服务器说明文档.docx
            --游戏逻辑
            --结束此次购买
            _iap:finishSuc(transactionid)
        elseif result == _InAppPurchase.PayFailed then
            --购买失败
            --根据errorcode提示给用户
            --结束此次购买
            _iap:finishFail(transaction)
        end
    end

    --productid,在苹果后台获取
    productid = 'gas'
    count = 1
    _app:onTouchEnd(function(x,y)
        if x < _rd.w/2 then
            _iap:init(callback)
        else
            --设备是否开启内购功能
            if _iap.canPayment then
                _iap:purchase(productid,count)
            end
        end
    end)
问题反馈(登录才可以发表哦!)