package org.openedit.cart.paypal; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.Enumeration; import java.util.HashMap; import org.openedit.WebPageRequest; import org.openedit.store.Product; import org.openedit.store.Store; public class PaypalUtil { protected Store fieldStore; public Store getStore() { return fieldStore; } public void setStore(Store store) { this.fieldStore = store; } public String handleIPN(WebPageRequest inReq) throws Exception{ String mode = inReq.findValue("usePaypalSandbox"); String url = "https://www.paypal.com/cgi-bin/webscr"; if(Boolean.parseBoolean(mode)){ url = "https://www.sandbox.paypal.com/cgi-bin/webscr"; } Enumeration en = inReq.getRequest().getParameterNames(); String str = "cmd=_notify-validate"; HashMap resultInfo = new HashMap(); while(en.hasMoreElements()){ String paramName = (String)en.nextElement(); String paramValue = inReq.getRequest().getParameter(paramName); resultInfo.put(paramName, paramValue); str = str + "&" + paramName + "=" + URLEncoder.encode(paramValue); } // post back to PayPal system to validate // NOTE: change http: to https: in the following URL to verify using SSL (for increased security). // using HTTPS requires either Java 1.4 or greater, or Java Secure Socket Extension (JSSE) // and configured for older versions. //URL u = new URL("https://www.paypal.com/cgi-bin/webscr"); URL u = new URL(url); URLConnection uc = u.openConnection(); uc.setDoOutput(true); uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); PrintWriter pw = new PrintWriter(uc.getOutputStream()); pw.println(str); pw.close(); BufferedReader in = new BufferedReader( new InputStreamReader(uc.getInputStream())); String res = in.readLine(); in.close(); //check notification validation if(res.equals("VERIFIED")) { inReq.putPageValue("resultMap", resultInfo); //getStore().getOrderArchive(). } else if(res.equals("INVALID")) { // log for investigation } else { // error } return res; } public String handlePDT(WebPageRequest inReq) throws Exception { String mode = inReq.findValue("usePaypalSandbox"); String url = "https://www.paypal.com/cgi-bin/webscr"; if(Boolean.parseBoolean(mode)){ url = "https://www.sandbox.paypal.com/cgi-bin/webscr"; } String token = inReq.findValue("pdtToken"); Enumeration en = inReq.getRequest().getParameterNames(); String str = "cmd=_notify-synch"; while(en.hasMoreElements()){ String paramName = (String)en.nextElement(); String paramValue = inReq.getRequest().getParameter(paramName); str = str + "&" + paramName + "=" + URLEncoder.encode(paramValue); } str = str + "&at=" + URLEncoder.encode(token); URL u = new URL(url); URLConnection uc = u.openConnection(); uc.setDoOutput(true); uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); PrintWriter pw = new PrintWriter(uc.getOutputStream()); pw.println(str); pw.close(); BufferedReader in = new BufferedReader( new InputStreamReader(uc.getInputStream())); String res = in.readLine(); HashMap resultInfo = new HashMap(); String line = null; int rowNum = 0; Product product = null; while( (line = in.readLine() ) != null) { String[] param = line.split("="); if(param.length == 2){ String key = URLDecoder.decode(param[0]); String val = URLDecoder.decode(param[1]); resultInfo.put(key, val); } } inReq.putPageValue("resultMap", resultInfo); // StringWriter out = new StringWriter(); // InputStream in = uc.getInputStream(); // byte[] input = new byte[1024]; // int size = 0; // while ((size = in.read(input)) != -1) { // out.append(new String(input, 0, size, "UTF-8")); // } // String responseString = out.toString(); return res; } }