package test.cmd;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
/**
*
*/
public class sale {
private static final String APIXML_TESTURL = "https://apiint.paymentsite.com/UniversalAPI/postXML";
//private static final String APIXML_PRODURL = "https://api.paymentsite.com/UniversalAPI/postXML";
/*
*
*/
private String Post(String xml, String url) throws IOException {
boolean bHTTPS = false;
String sResponse = "";
if (url.startsWith("https:"))
bHTTPS = true;
HttpURLConnection httpConn = null;
HttpsURLConnection httpsConn = null;
DataOutputStream dos = null;
InputStream istr = null;
BufferedReader reader = null;
try {
URL ul = new URL(url);
if (bHTTPS) {
httpsConn = (HttpsURLConnection) ul.openConnection();
httpsConn.setDoOutput(true);
httpsConn.setDoInput(true);
httpsConn.setUseCaches(false);
httpsConn.setRequestMethod("POST");
httpsConn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
httpsConn.setRequestProperty("Content-Length", Integer.toString(xml.getBytes("UTF-8").length));
dos = new DataOutputStream(httpsConn.getOutputStream());
} else {
httpConn = (HttpURLConnection) ul.openConnection();
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
httpConn.setRequestProperty("Content-Length", Integer.toString(xml.getBytes("UTF-8").length));
dos = new DataOutputStream(httpConn.getOutputStream());
}
dos.write(xml.getBytes("UTF-8"), 0, xml.getBytes("UTF-8").length);
dos.flush();
if (bHTTPS)
istr = httpsConn.getInputStream();
else
istr = httpConn.getInputStream();
reader = new BufferedReader(new InputStreamReader(istr, "UTF-8"));
String line;
StringBuffer sb = new StringBuffer();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
sResponse = sb.toString();
} catch (Exception e) {
throw e;
} finally {
if (istr != null) istr.close();
if (reader != null) reader.close();
if (dos != null) dos.close();
if (httpsConn != null) httpsConn.disconnect();
else if (httpConn != null) httpConn.disconnect();
}
return sResponse;
}
public String runTxn(){
String sResponse = "";
try{
/************ please build your request XML here ***********/
//Build API XML
String sample_xml = buildRequestXML();
sResponse = Post(sample_xml, APIXML_TESTURL);
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}
return sResponse;
}
public static void main(String[] args) {
try{
String sResponse = runTxn();
System.out.println("response content:\n" + sResponse);
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}