title: How do I use HTTP POST in JSP page to send a FORM to Paypal and read response?
body: <p>I'm using paypal's <code>sandbox</code> to test out the <code>Payment Data Transfer (PDT)</code> feature from Paypal. Please note, <strong>Paypal doesn't have a code example, in JSP, for PDT.</strong> I'm shocked, since they seem to have examples for everything else.</p>
  
  <p>According to Paypal, I can specify the URL that I want to use for processing the PDT information. I did just that in my sandbox profile.</p>
  
  <p>Now I'm stuck on a few things with the JSP code. I managed to get some example code, but it might not be complete, and to tell you the truth, I'm not exactly familiar with some of the code in it.</p>
  
  <p>The JSP code is below. If you look at <a href="https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&amp;content_ID=developer/howto_html_paymentdatatransfer" rel="nofollow">Paypal's Payment Data Transfer</a> page, it explains, without any code examples, the PDT process.</p>
  
  <p>It states that the <strong>transaction id</strong> will be appended to the <code>URL</code> specified in your Profile. Okay, so I can get that id via <code>request.getParameter("tx")</code>. That's the easy part.</p>
  
  <p><strong>1) -- But how do I POST the form (as shown below) back to Paypal? That part I don't understand. How do I code that?</strong></p>
  
  <p><strong>2)</strong> --Then the page states "In PayPal's reply to your post, the first line will be SUCCESS or FAIL. An example successful response looks like this (HTTP Header has been omitted):<br/>
  SUCCESS<br/> 
  first_name=Jane+Doe <br/>
  last_name=Smith <br/>
  payment_status=Completed <br/>
  payer_email=janedoesmith%40hotmail.com <br/>
  payment_gross=3.99 <br/>
  mc_currency=USD <br/>
  custom=For+the+purchase+of+the+rare+book+Green+Eggs+%26+Ham <br/></p>
  
  <p><strong>How is this information retrieved from within my JSP page?</strong></p>
  
  <p><br/></p>
  
  <h1>Html Form to Post to Paypal</h1>
  
  <pre><code>&lt;form method=post action="https://www.paypal.com/cgi-bin/webscr"&gt; 
  &lt;input type="hidden" name="cmd" value="_notify-synch"&gt; 
  &lt;input type="hidden" name="tx" value="TransactionID"&gt; 
  &lt;input type="hidden" name="at" value="YourIdentityToken"&gt; 
  &lt;input type="submit" value="PDT"&gt; 
  &lt;/form&gt;
  </code></pre>
  
  <h1>JSP code</h1>
  
  <pre><code>// Java JSP
  
  &lt;%@ page import="java.util.*" %&gt;
  &lt;%@ page import="java.net.*" %&gt;
  &lt;%@ page import="java.io.*" %&gt;
  
  &lt;%@ page import="javax.servlet.*"%&gt;
  &lt;%@ page import="javax.servlet.http.*"%&gt;
  &lt;%@ page import="javax.naming.*"%&gt;
  &lt;%@ page import="javax.sql.*"%&gt;
  &lt;%@ page import="java.sql.*"%&gt;
  
  &lt;%
  // read post from PayPal system and add 'cmd'
  Enumeration en = request.getParameterNames();
  String str = "cmd=_notify-validate";
  while(en.hasMoreElements()){
  String paramName = (String)en.nextElement();
  String paramValue = request.getParameter(paramName);
  str = str + "&amp;" + paramName + "=" + URLEncoder.encode(paramValue);
  }
  
  String transId = request.getParameter("tx");
  
  // 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.
  java.net.URL u = new java.net.URL("https://www.sandbox.paypal.com/cgi-bin/webscr");
  
  java.netHttpURLConnection uc = (java.net.HttpURLConnection)u.openConnection();
  
  uc.setRequestMethod("POST");
  
  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();
  
  // assign posted variables to local variables
  String itemName = request.getParameter("item_name");
  String itemNumber = request.getParameter("item_number");
  String paymentStatus = request.getParameter("payment_status");
  String paymentAmount = request.getParameter("mc_gross");
  String paymentCurrency = request.getParameter("mc_currency");
  String txnId = request.getParameter("txn_id");
  String receiverEmail = request.getParameter("receiver_email");
  String payerEmail = request.getParameter("payer_email");
  
              DataSource ds = null;
              Connection conn = null;
              Statement stmt = null;
  
              try {
              final Context ctx = new InitialContext();
              ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mydb");
              conn = ds.getConnection();
              stmt = conn.createStatement();
  
              //test_paypal
              int success = stmt.executeUpdate("insert into test_paypal values("paymentStatus="+request.getParameter("payment_status")+"paymentAmount="+request.getParameter("mc_gross")+
                                               "txnId"+request.getParameter("txn_id")+")");
               }//try
               catch(Exception e) {}
  
               finally {
                         stmt.close();
                         conn.close();
                       }
  
  
  
  check notification validation
  if(res.equals("VERIFIED")) {
  // check that paymentStatus=Completed
  // check that txnId has not been previously processed
  // check that receiverEmail is your Primary PayPal email
  // check that paymentAmount/paymentCurrency are correct
  // process payment
  }
  else if(res.equals("INVALID")) {
  // log for investigation
  }
  else {
  // error
  }
  %&gt;
  </code></pre>