1

I am trying to use the PayPal button manager API. Everytime I run my file, regardless of call type, I get the following error output:

run:
Request sent to PayPal: USER=xxxxxxxxxxxxxxx&PWD=xxxxxxxxx&SIGNATURE=xxxxxxxxxxxx&METHOD=BMButtonSearch&VERSION=59.0&STARTDATE=2012-10-11T00:00:00Z&ENDDATE=2012-10-11T00:00:00Zjava.net.UnknownHostException: sandbox.paypal.com/cgi-bin/webscr
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:866)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1258)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1211)
    at java.net.InetAddress.getAllByName(InetAddress.java:1127)
    at java.net.InetAddress.getAllByName(InetAddress.java:1063)
    at java.net.InetAddress.getByName(InetAddress.java:1013)
    at BM_ButtonSearch.main(BM_ButtonSearch.java:48)
BUILD SUCCESSFUL (total time: 0 seconds)

My class code is as follows:

import java.net.*;
import javax.net.ssl.*;
import java.io.*;
import java.security.MessageDigest;
import java.util.Date; 

public class BM_ButtonSearch { 

   public static void main(String[] args) {
      try {
         //String hostname = "paypal.com/cgi-bin/webscr"; //Set as host if live
         String hostname = "sandbox.paypal.com/cgi-bin/webscr"; //Set as host if testing
         // Set variables for PayPal transaction
         String user = "xxxxxxxxxxxxxxxxxxxx"; //Set PayPal API username
         String password = "xxxxxxxxxxx"; //API password, not acct password
         String signature = "xxxxxxxxxxxxxxxxxxx"; //Set unique user signature
         String method = "BMButtonSearch"; //Set Button Manager button type
         String version = "59.0"; //Set Button Manager Version as ##.#
         String startdate = "2012-10-11T00:00:00Z"; //Set start date using YYYY-MM-DDTHH:MM:SSZ
         String enddate = "2012-10-11T00:00:00Z"; //Set end date using YYYY-MM-DDTHH:MM:SSZ

         String requestID = "";
         MessageDigest md5 = MessageDigest.getInstance("MD5");
         byte[] md5summe = md5.digest(new Date().toString().getBytes()); 

         for (int it = 0; it < md5summe.length; it++) {
            String temp = Integer.toHexString(md5summe[it] & 0xFF);
            /*
             * toHexString has the side effect of making stuff like 0x0F
             * only one char F(when it should be '0F') so I check the length
             * of string
             */
            if (temp.length() < 2) {
               temp = "0" + temp;
            }
            requestID += temp.toUpperCase();
         } 

         // Build NVP String (currency string added, declare in variables)
         String paramNVP = "USER=" + user + "&PWD=" + password + "&SIGNATURE="
               + signature + "&METHOD=" + method + "&VERSION=" + version
               + "&STARTDATE=" + startdate + "&ENDDATE=" + enddate; 

         System.out.print("Request sent to PayPal: " + paramNVP); 

         // Create the SSL Socket
         int port = 443;
         InetAddress addr = InetAddress.getByName(hostname);
         SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
         SSLSocket sslsock = (SSLSocket) factory.createSocket(addr, port); 

         // Open Socket Output Stream
         String path = "/";
         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sslsock.getOutputStream(), "UTF8"));

         // Send data as a POST
         bw.write("POST " + path + " HTTP/1.0\r\n"); 

         // Write header info to the socket
         bw.write("Connection: close\r\n");
         bw.write("Content-Type: text/namevalue\r\n");
         bw.write("Content-Length: " + paramNVP.length() + "\r\n");
         bw.write("Host: " + hostname + "\r\n");
         bw.write("X-VPS-REQUEST-ID: " + requestID + "\r\n");
         bw.write("X-VPS-CLIENT-TIMEOUT: 240\r\n");
         // Sets the Payflow client IP variable
         // bw.write("PP_REMOTE_ADDR: 192.168.0.123\r\n");
         bw.write("\r\n"); 

         // Write the NVP String to the Socket
         bw.write(paramNVP);
         // Flush the stream
         bw.flush(); 

         // Read the Socket response
         BufferedReader br = new BufferedReader(new InputStreamReader(
               sslsock.getInputStream()));
         String line;
         while ((line = br.readLine()) != null) {
            System.out.println(line);
         }

         // Close the writer, reader, and the socket
         bw.close();
         br.close();
         sslsock.close();
      } 

      catch (Exception e) {
         e.printStackTrace(System.out);
      }
   } // end main()
}// end class SocketTest 

Any help would be appreciated. I see that most people suggest trying to access the URL via a browser, which in my case works, just not when using my java class.

Thanks in advance! :D

Fairplay89
  • 147
  • 3
  • 15

1 Answers1

2

sandbox.paypal.com/cgi-bin/webscr is not a hostname. Your hostname should be just sandbox.paypal.com. Also, your path should be /cgi-bin/webscr, not /.

See also:

mellamokb
  • 56,094
  • 12
  • 110
  • 136
  • Changing my declared hostname fixed my error, but now my output is a bunch of Set-Cookie errors. I can't post the output due to the length unfortunately. =/ – Fairplay89 Jul 23 '12 at 17:45
  • Not sure what you mean. Are there cookies that are required by the sandbox that you are not passing? Or is it an error generated by Java? Can you redact any confidential information and post a [**pastie**](http://pastie.org/) for example? Or post just part of the error message? – mellamokb Jul 23 '12 at 17:47
  • Sandbox should require no cookies. They aren't required for any other call and it's not documented in the API user guide. However my output from Java can be found at http://pastie.org/4307499 – Fairplay89 Jul 23 '12 at 17:48
  • Those aren't *errors*. That's the reply from the sandbox server, using HTTP Header `Set-Cookie` to request the client (in this case your Java code), to set the specified cookies for future requests. This is very common with a stateless protocol like HTTP to tie together the first request with future requests to be related. Normally this is all hidden away by your browser or other high-level HTTP browsing technology. – mellamokb Jul 23 '12 at 17:52
  • Why aren't you using a more advanced Java Web Client class like [HttpUnit's `WebClient`](http://stackoverflow.com/questions/1137812/equivallent-of-nets-webclient-and-httpwebrequest-in-java) which will automatically take care of these low-level HTTP details? You shouldn't have to muck around with HTTP headers with a language like Java. – mellamokb Jul 23 '12 at 17:53
  • I overlooked where you suggested I change my path variable. Now my output is showing http://pastie.org/private/f55fbrnidts0tgkbg6bdhw is this still appearing to be ok? I was expecting a slightly different result I guess. I thought it was going to list previously created buttons. – Fairplay89 Jul 23 '12 at 17:55
  • You're connecting to the web frontend, `/cgi-bin/webscr`. You need to read through some of the documentation on PayPal's API's. For instance, I dug around a little and found this list of API endpoints from PayPal: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/howto_api_endpoints. They are completely separate urls. You are getting an HTML response expected to be parsed and viewed by a browser, not an API. This is typical of any web-based system that provides both a browser-ready front-end and an API endpoint - they are usually not the same url. – mellamokb Jul 23 '12 at 17:57
  • ButtonSearch doesn't return the results I expected, but the other calls such as GetInventory do so I will have to play with my ButtonSearch class a bit. Thanks a lot for the info though. – Fairplay89 Jul 23 '12 at 18:03
  • Also you might take a look at the [Sample Code](https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/library_code) provided by PayPal for working with Java. Also, I found this ready-made library for working with PayPal with Java that may save you a lot of development and debugging time: http://www.paypalobjects.com/en_US/ebook/PP_APIReference/Appx-SDKjava.html – mellamokb Jul 23 '12 at 18:03
  • Your hostname should be `api-3t.sandbox.paypal.com`. *Not* `sandbox.paypal.com/cgi-bin/webscr`. `sandbox.paypal.com/cgi-bin/webscr` is for web requests, not API calls. – Robert Jul 24 '12 at 09:16