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, andpubKey
, optional, is the key to decrypt content in request response.post(string url, string pubKey, table body)
:sendpost
request and encrypt body content bypubKey
body
object will be converted into json. Thepost
request body is the cypher of json encrypted bypubKey
- requset
header
will contains signature of body content
postwosign(string url, lua.LTable body)
:Send normalpost
request without body encryption and signature.
- VsysRefund
refundVsys(int amount, string recipient, string orderID)
:stop order whose id isorderId
, and refundamount
vsys coin to wallet addressrecipient
。refundToken(int amount, string recipient, string orderID)
:stop order whose id isorderId
, and refundamount
token to wallet addressrecipient
.
- SystemUtils
preventDefault
:When enable this setting, the initial status ofUserService
created by this function will beServicePending
.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