Advance

Lua script guide

We provide 7 build-in libraries used in Lua environment for Lua script development

  • string (opens new window)
  • math (opens new window)
  • base (opens new window)
  • VsysService (TO BE UPDATED)
  • VsysHttp
    • get(string url, string pubKey):Send GET request to url, and pubKey, optional, is the key to decrypt content in request response.
    • post(string url, string pubKey, table body):send post request and encrypt body content by pubKey
      • body object will be converted into json. The post request body is the cypher of json encrypted by pubKey
      • requset header will contains signature of body content
    • postwosign(string url, lua.LTable body):Send normal post request without body encryption and signature.
  • VsysRefund
    • refundVsys(int amount, string recipient, string orderID):stop order whose id is orderId , and refund amount vsys coin to wallet address recipient
    • refundToken(int amount, string recipient, string orderID):stop order whose id is orderId , and refund amount token to wallet address recipient .
  • SystemUtils
    • preventDefault:When enable this setting, the initial status of UserService created by this function will be ServicePending.See Service Dutation for details.

Banned Syntax in Lua Script

  • while
  • coroutine
  • _G
  • collectgarbage
  • dofile
  • getfenv
  • getmetatable
  • loadfile
  • load
  • loadstring
  • rawequal
  • rawget
  • rawset
  • setfenv
  • setmetatable
  • io
  • os
  • debug
  • newproxy
  • package
  • pcall
  • printregs
  • xpcall
  • module

Lua script functions implementation

Merchants are suggested to provide start, stop and refund functions in Lua scripts which will be executed for starting, stopping or refunding services. If corresponding scripts are empty in ServiceApi of Service Type, system will execute default logic in corresponding stage.

Example start:lua script invoked by system after users pay for orders

-- Import VsysHttp and SystemUtils library
local http = require("VsysHttp")
local util = require("SystemUtils")
-- Declare start function. Function name must be one of 'start', 'stop' or 'refund'.
-- inputTable: parameter table
--[[
    id: string,Order ID
    duration: int, Service duration of the order
    createdAt: int, Order create time
    options: inputTable, service option of order
--]]
function start(inputTable)
-- Enable preventDefault model in the example
    util.preventDefault()
    a = {}  
    a["id"]= inputTable["id"]
    a["createdAt"] = inputTable["createdAt"]
    a["duration"] = inputTable["duration"]
    -- send post
    resp = http.postwosign("https://example-url", a)
    return resp
end

stop example:invoked in procedure of order redund

-- import VsysHttp library
local http = require("VsysHttp")
-- Declare stop function. Function name must be one of 'start', 'stop' or 'refund'.
-- inputTable: prameter table
--[[
        id: string, order ID
--]]
function stop(inputTable)
    a = {}
    a["id"] = inputTable["id"]
    resp = http.postwosign("https://example-url"..a["id"], a)
    return resp
end

refund example:invoked when user apply for redund

-- import VsysHttp library
local http = require("VsysHttp")
-- Declare refund function. Function name must be one of 'start', 'stop' or 'refund'.
-- inputTable: prameter table
--[[
        id: string, order ID
--]]
function refund(inputTable)
    a = {}
    a["id"] = inputTable["id"]
    resp = http.postwosign("https://example-url"..a["id"], a)
    return resp
end
Last Updated: 6/29/2021, 6:18:56 AM