package examples; import java.io.*; import java.util.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; import com.worldpay.io.*; import com.worldpay.select.*; import com.worldpay.servlet.*; import com.worldpay.select.merchant.*; /** * This servlet listens for the transaction outcome from Select. * If a dump file has been specified in select.properties * (as authResult.dump) then a record of the transaction is written * to this file with all the parameter values. * If a result file has been specified in select.properties * (as authResult.file) then this is returned to WorldPay as the merchant * response. The file name is first looked for with the transaction status * appended, allowing you to have a different file for successful and * cancelled transactions (<filename>Y and <filename>C). If * these do not exist then the straight file name is looked for. * If there is no result file specified then this servlet returns a * standard response which displays the values of all the transaction * result parameters. * */ public class AuthResponseServlet extends SelectServlet implements SelectDefs { protected void doRequest(SelectServletRequest req, SelectServletResponse res) throws ServletException, IOException { // set up the output stream for the page res.setContentType("text/html"); ServletOutputStream out = res.getOutputStream(); // First, dump all the information that's available into a file, // if a file name has been specified in select.properties. // Output is appended to the end of the file. try { if (Select.getProperty("authResult.dump")==null) throw new SelectException("no authResult.dump specified in "+ "select.properties"); String dumpFileName = Select.getPropertyPath("authResult.dump"); if (!((dumpFileName==null)||dumpFileName.equals(""))) { PrintWriter dumpFile = new PrintWriter(new FileWriter(dumpFileName, true)); dumpFile.println("AuthResponse called for installation "+ req.getInstallationID()+":"); Enumeration names = req.getParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); dumpFile.println(name+"="+req.getParameter(name)); } dumpFile.println("================"); dumpFile.close(); } } catch (Exception e) { // ignore - probably just failure to open output file // log it in the error log though e.printStackTrace(); } // Look for a result file to send, as specified in select.properties try { if (Select.getProperty("authResult.file")==null) throw new SelectException("no authResult.file specified in "+ "select.properties"); String baseFileName = Select.getPropertyPath("authResult.file"); if (baseFileName==null) throw new SelectException("no authResult.file specified in "+ "select.properties"); String transStatus = req.getParameter(SEL_transStatus); if (transStatus==null) transStatus=""; // first try basename+transStatus (Y or C) FileInputStream file = null; try { file = new FileInputStream(baseFileName+transStatus); } catch (FileNotFoundException f1) { try { // default to bare base name file = new FileInputStream(baseFileName); } catch (FileNotFoundException f2) { throw new SelectException("no result file found - "+ "sending standard content instead"); } } // we've got a file - send it out StreamBridge bridge = new StreamBridge(file, out); bridge.pipe(); } catch (Exception e) { // no file available: give standard content out.println("
Thank you for your order. Your payment "+ "was successful"); break; case 'C': out.println("
Your payment has been cancelled"); break; default: out.println("
Unknown status returned from WorldPay"); break; } out.println("
Parameters were:
");
Enumeration names = req.getParameterNames();
while (names.hasMoreElements()) {
name = (String) names.nextElement();
out.println(name+"="+req.getParameter(name)+"
");
}
out.println("