I'm integrating OpenPay payment gateway to one of our application. When doing testing, It needs something called "deviceDataId" which can be generate using OpenPay JavaScript library (https://github.com/open-pay/openpay-js). This value should be generated in the client's browser.
var deviceDataId = OpenPay.deviceData.setup("formId");
But I need to run this within my Java application for testing my implementation. So I have run this Js file from my Java Application using following code.
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.eval(new BufferedReader(new FileReader(JsTest.class.getClassLoader().getResource("openpay-data.v1.js").getFile())));
Invocable inv = (Invocable) engine;
inv.invokeFunction("OpenPay.deviceData.setup");
When I run this code, It will gave me following error.
Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "window" is not defined. (<Unknown source>#698) in <Unknown source> at line number 698
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:232)
at com.chathura.JsTest.main(JsTest.java:13)
I have search here for similar errors and found that the error comes because there is no "document" object when running it from the server side.
Executing javascript in java - Opening a URL and getting links
So rather than edit and re write this JS library to remove all client side dependencies, is there any way to simulate that "document" object when it runs on a server? May be a code block that I can include on top of that JS file?