Dear all,
I've to write a Java EE application that have to become an interface to a payment platform. The protocol require to send a POST string containing some information (including an error and response page) to an external host, get the response and redirect the user to another host using the previous response. When the user ends the transaction the host redirect the user to my error or response page.
For the moment my user access to my application using glassfish REALM authentication. After that I write the following code:
public void paymentPrepare() throws UnsupportedEncodingException, IOException {
String postUrl = "https://test.payment.test/init/http";
HttpPost postRequest = new HttpPost(postUrl);
String id = "99999999";
String password = "99999999";
String action = "4";
String amt = "1.00";
String currencycode = "978";
String langid = "ENG";
String responseurl = "http://myhost:8080/EMBOWorkshop/secure/response.xhtml";
String errorurl = "http://myhost:8080/EMBOWorkshop/secure/error.xhtml";
trackid = "TRCK0001";
udf1 = "Descrizione";
StringEntity postParamsEntity = new StringEntity("id=" + id
+ "&password=" + password
+ "&action=" + action
+ "&amt=" + amt
+ "¤cycode=" + currencycode
+ "&langid=" + langid
+ "&responseurl=" + responseurl
+ "&errorurl" + errorurl
+ "&trackid" + trackid
+ "&udf1" + udf1);
postParamsEntity.setContentType("application/x-www-form-urlencoded");
postRequest.setEntity(postParamsEntity);
DefaultHttpClient httpClient = new DefaultHttpClient();
// Execute the HTTP POST
System.out.println("Executing HTTP Post...\n");
HttpResponse response = httpClient.execute(postRequest);
// Check the HTTP status of the post.
if (response.getStatusLine().getStatusCode() != 200 && response.getStatusLine().getStatusCode() != 201) {
throw new RuntimeException("Failed: HTTP error code: "
+ response.getStatusLine().getStatusCode());
}
// Create a reader to read in the HTTP post results.
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
// Read in all of the post results into a String.
String output = "";
Boolean keepGoing = true;
while (keepGoing) {
String currentLine = br.readLine();
if (currentLine ==
null) {
keepGoing = false;
} else {
output += currentLine;
}
}
System.out.println("Raw string result: \n" + output);
}
The previous code works fine. I receive the response. But now I don't know how to continue. How can redirect the user to another site, and when the payment is finished, receive the response? Another question is, the user authenticate himself to access to the application. When the user will redirect to an external site, the glassfish session permits to get the response from the other host without require a new authentication?
For the moment I don't know how to redirect the user, but I tried to write this method to obtain the last response:
public void getResponse(HttpServletRequest request, HttpServletResponse response) {
paymentId = request.getParameter("paymentid");
result = request.getParameter("result");
auth = request.getParameter("auth");
ref = request.getParameter("ref");
traind = request.getParameter("tranid");
trackid = request.getParameter("trackid");
udf1 = request.getParameter("udf1");
responsecode = request.getParameter("responsecode");
}
Is it correct?
Thanks