SOAP request using Lua

Lua is a short and simple language exposing the power of C in clean syntax. In recent project I had to make lua talk to SOAP Server on a payment gateway. I was able to do this successfully. The lua code is based on LuaSoap. You can down the full sample code from here. It required three libraries to be built on the *nix system before the actual soap request is made. Happy tweaking with Lua.

http://onlinepaymentprocessing.com/downloads/lua/agms_lua_example_1_0_0.zip
README.md

## Dependencies

LuaSOAP version >= 3.0.0 (https://github.com/tomasguisasola/luasoap)
LuaExpat version >= 1.3.0 (http://matthewwild.co.uk/projects/luaexpat)
LuaSocketgit version >= 3.0-rc1 (https://github.com/diegonehab/luasocket)
LuaSec version >= 0.4.1 (https://github.com/brunoos/luasec)

The libraries are included in the lib folder. Please note the luasec-0.5.0 do not work on Centos environment.


## Package Dependencies:
Ubuntu:lua-dev, libexpat-dev, openssl-devel
CentOS: lua-devel, expat-devel, openssl-dev


## Compile Instructions

The libexpat can be built using the `make`. Upload the libexpat folder on your developement server and issue `make` command.

The luasocket can be built and installed using make. Upload the luasocket library on your development machine and issue `make {PLATEFORM}` and then `make install`. Please note the path of CDIR - path to *.so files and LDIR - path to lua files. These need to be updated in the lua script

The luasec can be built using the make. Upload the libsec-0.4 folder on your developement server and issue `make` command



## Path Lua and C Libraries

The correct path of these libraries (Lua & C) needs to be set in the run.lua for them to work. 

agms.lua

#!/usr/bin/lua
-- Agms Module to interact with the Agms Gateway

-- @author: Maanas Royy
-- @copyright: Avant-Garde Marketing Solutions, Inc.

agms = {}

 -- Request Object
 agms.Request = {}
 agms.Request.TransactionType = ""
 agms.Request.GatewayUserName = ""
 agms.Request.GatewayPassword = ""
 agms.Request.PaymentType = ""
 agms.Request.Amount = ""
 agms.Request.Tax = ""
 agms.Request.Shipping = ""
 agms.Request.OrderDescription = ""
 agms.Request.OrderID = ""
 agms.Request.PONumber = ""
 agms.Request.CCNumber = ""
 agms.Request.CCExpDate = ""
 agms.Request.CVV = ""
 agms.Request.CheckName = ""
 agms.Request.CheckABA = ""
 agms.Request.CheckAccount = ""
 agms.Request.AccountHolderType = ""
 agms.Request.AccountType = ""
 agms.Request.SecCode = ""
 agms.Request.FirstName = ""
 agms.Request.LastName = ""
 agms.Request.Company = ""
 agms.Request.Address1 = ""
 agms.Request.Address2 = ""
 agms.Request.City = ""
 agms.Request.State = ""
 agms.Request.Zip = ""
 agms.Request.Country = ""
 agms.Request.Phone = ""
 agms.Request.Fax = ""
 agms.Request.EMail = ""
 agms.Request.Website = ""
 agms.Request.ShippingFirstName = ""
 agms.Request.ShippingLastName = ""
 agms.Request.ShippingCompany = ""
 agms.Request.ShippingAddress1 = ""
 agms.Request.ShippingAddress2 = ""
 agms.Request.ShippingCity = ""
 agms.Request.ShippingState = ""
 agms.Request.ShippingZip = ""
 agms.Request.ShippingCountry = ""
 agms.Request.ShippingEmail = ""
 agms.Request.ShippingPhone = ""
 agms.Request.ShippingFax = ""
 agms.Request.ProcessorID = ""
 agms.Request.TransactionID = ""
 agms.Request.Tracking_Number = ""
 agms.Request.Shipping_Carrier = ""
 agms.Request.IPAddress = ""
 agms.Request.Track1 = ""
 agms.Request.Track2 = ""
 agms.Request.Track3 = ""
 agms.Request.Track_Type = ""
 agms.Request.Custom_Field_1 = ""
 agms.Request.Custom_Field_2 = ""
 agms.Request.Custom_Field_3 = ""
 agms.Request.Custom_Field_4 = ""
 agms.Request.Custom_Field_5 = ""
 agms.Request.Custom_Field_6 = ""
 agms.Request.Custom_Field_7 = ""
 agms.Request.Custom_Field_8 = ""
 agms.Request.Custom_Field_9 = ""
 agms.Request.Custom_Field_10 = ""
 agms.Request.SAFE_Action = ""
 agms.Request.SAFE_ID = ""
 agms.Request.ReceiptType = ""
 agms.Request.MICR = ""
 agms.Request.MICRSymbolSet = ""
 agms.Request.CheckFrontTIFF = ""
 agms.Request.CheckBackTIFF = ""
 agms.Request.CheckNumber = ""
 agms.Request.Terminal_ID = ""
 agms.Request.CCNumber2 = ""
 agms.Request.Clerk_ID = ""
 agms.Request.Billing_Code = ""
 agms.Request.InvoiceID = ""
 agms.Request.BatchID = ""
 agms.Request.DLNumber = ""
 agms.Request.DLState = ""
 agms.Request.IdentityVerification = ""
 agms.Request.CourtesyCardID = ""
 agms.Request.MagData = ""

 -- Response Object
 agms.Response = {}
 agms.Response.STATUS_CODE = ""
 agms.Response.STATUS_MSG = ""
 agms.Response.TRANS_ID = ""
 agms.Response.AUTH_CODE = ""
 agms.Response.AVS_CODE = ""
 agms.Response.AVS_MSG = ""
 agms.Response.CVV2_CODE = ""
 agms.Response.CVV2_MSG = ""
 agms.Response.ORDERID = ""
 agms.Response.SAFE_ID = ""
 agms.Response.FULLRESPONSE = ""
 agms.Response.POSTSTRING = ""
 agms.Response.BALANCE = ""
 agms.Response.GIFTRESPONSE = ""
 agms.Response.MERCHANT_ID = ""
 agms.Response.CUSTOMER_MESSAGE = ""


 -- AgmsAPI method
 agms.AgmsAPI = {}
 agms.AgmsAPI.url = "https://gateway.agms.com/roxapi/agms.asmx"

 function agms.AgmsAPI.ProcessTransaction (request)

 -- Fabricate the SOAP Request Body
 local request_body = {tag = "objparameters"}
 for key, val in pairs(request) do
 if val ~= "" then
 table.insert(request_body, { tag = key, val})
 end
 end
 
 -- Soap Call, Important part add the namespace
 local ns, meth, ent = agms.soap_client.call(
 {url = agms.AgmsAPI.url,
 namespace = "https://gateway.agms.com/roxapi/",
 soapaction = "https://gateway.agms.com/roxapi/ProcessTransaction",
 method = "ProcessTransaction",
 entries = {request_body}
 })
 
 return ns, meth, ent
 
 end


return agms






run.lua

#!/usr/bin/lua
-- Lua code to interact with the Agms Gateway

-- @author: Maanas Royy
-- @copyright: Avant-Garde Marketing Solutions, Inc.

-- Define package path to load libraries
package.path = './lib/luasoap-3.0/src/?.lua;' .. package.path -- luasoap
package.path = './lib/luaexpat-1.3.0/src/?.lua;' .. package.path -- luaexpat
package.path = '/usr/local/share/lua/5.1/?.lua;' .. package.path -- luasocket
package.path = './lib/luasec-0.4.1/src/?.lua;' .. package.path -- luasec

-- Define package path to load C libraries
package.cpath = './lib/luaexpat-1.3.0/src/?.so;' .. package.cpath -- luaexpat
package.cpath = '/usr/local/lib/lua/5.1/?.so;' .. package.cpath -- luasocket
package.cpath = './lib/luasec-0.4.1/src/?.so;' .. package.cpath -- luasec

-- Import agms 
local agms = require "agms"
-- Import helper function to print lua table
local print_r = require "helper"

-- Import LuaSoap
agms.soap_client = require "client"
-- Import SSL, Version 0.4 require you to call https before we can actually load ssl.https.
-- It exports module as ssl.https
require "https"
local https = require "ssl.https"

-- Assign the https protocol in SOAP client
agms.soap_client.https = https

-- Prepare Request
agms.Request.GatewayUserName = "##########";
agms.Request.GatewayPassword = "##########";
agms.Request.TransactionType = "sale";
agms.Request.PaymentType = "creditcard";
agms.Request.Amount = "1.00";
agms.Request.CCNumber = "4111111111111111";
agms.Request.CCExpDate = "0120";
agms.Request.CVV = "123";

-- Debug request
-- print_r(agms.Request)

local ns, meth, ent = agms.AgmsAPI.ProcessTransaction(agms.Request);
print("namespace = ", ns, "element name = ", meth)
for i, elem in ipairs (ent[1]) do
 print (elem['tag'], '=', elem[1])
end

Advertisements