Announcement Announcement Module
Collapse
No announcement yet.
paypal and spring mvc Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • paypal and spring mvc

    hey Folks,

    I got an issue and it driving me nuts. I am posting from a form to my Spring MVC 3 controller. Internally I am posting to paypal.
    GOAL: To display the page that shows the item and the price charged to the customer.

    What I keep getting is: "Please login to use the PayPal Sandbox features." WHEN I use the test url:
    https://www.sandbox.paypal.com/cgi-bin/webscr

    AND I get: "You have requested an outdated version of PayPal. This error often results from the use of bookmarks." when I use the official URL: https://www.paypal.com/cgi-bin/webscr



    Code:
    						String url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
    		//url = "https://www.paypal.com/cgi-bin/webscr";
    		//url = "https://developer.paypal.com";
    		//url = "https://www.paypal.com/cgi-bin/webscr";
    		//HttpParams httpParams = new BasicHttpParams(); 
    		//HttpClient client = new DefaultHttpClient(httpParams);
    		HttpPost httpPost = new HttpPost(url);
    
    		// MIMIC BROWSER
    		  //conn.setRequestProperty( "Host", "test.com");
    		//httpPost.setHeader( "User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
    		httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows; U;Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7");
    
    		  //conn.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.2; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0");
    		//httpPost.setHeader( "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    		 httpPost.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    
    		httpPost.setHeader( "Accept-Language", "en-us,en;q=0.5");
    		  //conn.setRequestProperty( "Keep-Alive", "115");
    		  //conn.setRequestProperty( "Connection", "keep-alive");
    		//httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
    		
    		
    		
    		
    		List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            //nvps.add(new BasicNameValuePair("method", "password"));
            nvps.add(new BasicNameValuePair("cmd", "_s-xclick"));
            
            //nvps.add(new BasicNameValuePair("cmd", "_xclick"));
            //nvps.add(new BasicNameValuePair("hosted_button_id", "31991"));
    		//client.getParams().setParameter("on0", "$20.00");
    		//<input type="hidden" name="business" value="[email protected]">
    		nvps.add(new BasicNameValuePair("business", "[email protected]"));
            //nvps.add(new BasicNameValuePair("business", "[email protected]"));
    		//<input type="hidden" name="currency_code" value="USD">
            nvps.add(new BasicNameValuePair("currency_code", "USD"));
    		//Amount: <br /><input name="amount" type="text" id="amount" size="45">
            nvps.add(new BasicNameValuePair("amount", "4"));
            nvps.add(new BasicNameValuePair("item_name", "PiT words"));
            nvps.add(new BasicNameValuePair("no_note", "1"));
            nvps.add(new BasicNameValuePair("return", "http://localhost:8080/pit-web-0.0.1-SNAPSHOT/welcome"));
    	
            //nvps.add(new BasicNameValuePair("IDToken2", "password"));
    
            try {
    			httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    		} catch (UnsupportedEncodingException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		HttpResponse response = null;
    		HttpClient client = new DefaultHttpClient();
    		try {
    			response =   client.execute(httpPost);
    			HttpEntity entity = response.getEntity();
    
    		       entity = response.getEntity();
    		        if (entity != null) {
    		            System.out.println(EntityUtils.toString(entity));
    		            entity.consumeContent();
    		        }
    		} catch (ClientProtocolException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    	}
    SO I am arriving at the paypal site its JUST that it keeps asking me to login OR giving me that "outdated version bookmark error". I did login through my browser so apparently a cookie is set in the browser, but this is posting through Java....

    I suspect it has something to do with the headers because if I remove some of the values I get an exception...anyone know whats going on???

    Code:
    		httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows; U;Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7");
    
    		  //conn.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.2; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0");
    		//httpPost.setHeader( "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    		 httpPost.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    
    		httpPost.setHeader( "Accept-Language", "en-us,en;q=0.5");

    Ultimately I just want the paypal page to show with the item and the price so the customer can buy....any help would be greatly appreciated.

  • #2
    re-work of params but now blank response

    When I try to connect to the official paypal url, change my "BusinessName" which is basically the email I use to login officially (not my test account) and change the cmd to "_xclick",

    I get a blank response:

    Code:
    String //url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
    		url = "https://www.paypal.com/cgi-bin/webscr";
    		HttpParams httpParams = new BasicHttpParams(); 
    		HttpPost httpPost = new HttpPost(url);
    		HttpClient client = new DefaultHttpClient(httpParams);
    		HttpClientParams.setCookiePolicy(httpParams, CookiePolicy.BROWSER_COMPATIBILITY);
    
    		// MIMIC BROWSER
    		httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3");
    		 httpPost.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    		 httpPost.setHeader("Accept-Language","en-gb,en;q=0.5");
    		httpPost.setHeader("content-type", "application/x-www-form-urlencoded");
    		
    		List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("cmd", "_xclick"));
    		//nvps.add(new BasicNameValuePair("cmd", "_s-xclick"));
    		//nvps.add(new BasicNameValuePair("business", "[email protected]"));
    		nvps.add(new BasicNameValuePair("business", "[email protected]"));
            nvps.add(new BasicNameValuePair("currency_code", "USD"));
            nvps.add(new BasicNameValuePair("amount", "4"));
            nvps.add(new BasicNameValuePair("item_name", "PiT words"));
            nvps.add(new BasicNameValuePair("no_note", "1"));
            nvps.add(new BasicNameValuePair("return", "http://localhost:8080/pit-web-0.0.1-SNAPSHOT/welcome"));
            nvps.add(new BasicNameValuePair("hosted_button_id", "31991"));
            try {
    			httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		}
    		HttpResponse response = null;
    		
    		try {
    			response =   client.execute(httpPost);
    			HttpEntity entity = response.getEntity();
    
    		       entity = response.getEntity();
    		        if (entity != null) {
    		            System.out.println(EntityUtils.toString(entity));
    		            entity.consumeContent();
    		        }
    		} catch (ClientProtocolException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    I tried all of this with a GET but I get a PayPal homepage.
    I am stuck!!!!
    I would just like to see the paypal page containing my item and the price.

    Is there not a way to bypass "logging in" and clicking on a button??
    Please advise. Thank you.

    Comment


    • #3
      paypal, spring mvc, internal post/get

      can anyone help me on this please?
      My issue is that I have dynamic input which means that the amount is determined after it is posted. It is not a fixed amount.
      I was on a similar site and I saw that they did the same thing I would like to do, it was some cgi program, probably using c or some other scripting language...

      I know it is possible ...it just that I guess I am not setting up my GET command or my POST command properly...BUT I am including only those variables that are required for the <form> under regular conditions...I am just trying to simulate the form post...(or get)

      Again, anyone out therE?? This one is just killing me...its been over a week....
      please advise
      thank you so much

      Comment


      • #4
        paypal java

        Hi ppl,

        I have a serious dilemma here. I am using Website Payment standard and I would like to integrate this with my java application. Basically the user submits a form, it is intercepted by my controller and some validation happens. At that point I would like to post/get to paypal. The payment page is supposed to be returned and this is the page that I would like to forward to the customer so that they can log into paypal or continue without logging in, again that is the payment page.

        I have been playing around with the HttpPost apache library. Something is not right. I have documented my problem here: http://forum.springsource.org/showthread.php?t=94075

        I came across a different way to submit paypal parameters here:
        http://code.google.com/p/divermate/s...pec=svn26&r=26
        OR this: http://paypaldev.org/yaf_postst16691...x-problem.aspx

        i tried to mimic the way the query string was supposed to be built:
        String str = "cmd=_s-xclick";
        str = str + "&" + "item_number" + "=" + "1";<br>
        str = str + "&" + "business" + "=" + "my@emailcom";<br>
        str = str + "&" + "currency_code" + "=" + "USD";<br>
        str = str + "&" + "amount" + "=" + "55";<br>
        str = str + "&" + "item_name" + "=" + "PiT Words";<br>
        str = str + "&" + "no_note" + "=" +"1";<br>
        str = str + "&" + "return" + "=" + "http://localhost:8080/ZZZ-web-0.0.1-SNAPSHOT/welcome";<br>
        str = str + "&" + "hosted_button_id" + "=" + "7D3BWZZZ87AME";<br>

        I even tried using :
        URLConnection such as:

        URL u = new URL("https://www.paypal.com/cgi-bin/webscr");
        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();

        following this example from paypal: https://www.paypal.com/us/cgi-bin/we...es-pop-outside
        BUT this was for command: cmd=_notify-validate
        NOT for cmd=_s-xclick but I assume it is set up the same way.
        I also tried using HTTPGet and HTTPPost...but nothing is working....
        I am lost..

        it just behaves differently with cmd=_s-xclick vs. cmd=_xclick ...OR it keeps saying there are is stuff missing OR it comes back with an empty response....or it says bookmarks are expired....this is crazy..not to mention the button that I genrated...am I supposed to include that....according to this I am:
        http://code.google.com/p/divermate/s...pec=svn26&r=26

        but when I do the exact same thing I get a blank response.

        can SOMEONE please post a working example of website payment standard that I can do a POST or GET to paypal from my java application. All I want to do is display the Paypal Payment Page to the user...please a working example..thank you so much...

        Comment


        • #5
          hi,

          For this you need Apache HttpClient (http://hc.apache.org/downloads.cgi) libs. This makes tasks lot easier.

          This is for HttpClient 4.1 library..

          public void yourMethod () {

          HttpClient httpclient = new DefaultHttpClient();
          HttpParams myParams = httpclient.getParams();
          HttpConnectionParams.setConnectionTimeout(myParams ,40000);
          HttpConnectionParams.setSoTimeout(myParams,40000);
          HttpPost httppost = new HttpPost("https://www.paypal.com/cgi-bin/webscr");

          try {
          // Add your data
          String url = "https://www.paypal.com/cgi-bin/webscr?"; // or sandbox url here
          url += "business=" + "[email protected]" + "&";
          url += "cmd=" + "_xclick" + "&";
          url += "amount=" + "20.00" + "&";
          url += "item_name=" + "item_name" + "&";
          url += "item_number=" + "item_number" + "&";
          url += "quantity=" + "1" + "&";
          url += "currency_code=" + "USD" + "&";
          url += "no_shipping=" + "1" + "&";
          url += "rm=" + "2";

          HttpGet httpGet = new HttpGet(url);
          HttpResponse response = httpclient.execute(httpGet);

          InputStream is = response.getEntity().getContent();
          BufferedReader reader = new BufferedReader(new InputStreamReader(is));
          StringBuilder str = new StringBuilder();
          String line = null;

          while((line = reader.readLine()) != null){
          str.append(line + "\n");
          }
          is.close();
          String responseText = str.toString();
          getContext().getResponse().getWriter().write(respo nseText); // Simply print the responseText string
          }
          catch (Exception e) {
          //TODO: handle exception
          }

          }

          The POST method didn't work..
          Hope this helps..

          Rukmal Dias

          Comment


          • #6
            PayPal Pay Now and Spring MVC

            Hi,
            I am a beginner tospring and web application.
            I have an application spring-mvc that will have the "Pay Now" button on a jsp.
            In the jsp, when click PayNow I use:
            doSubmit(document.forms['<c:out value="${mdl.modelName}"/>'],'payNow');

            in the controller I have:

            public ModelAndView payNow(HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse) {

            HttpClient httpclient = new DefaultHttpClient();
            HttpParams myParams = httpclient.getParams();
            HttpConnectionParams.setConnectionTimeout(myParams ,40000);
            HttpConnectionParams.setSoTimeout(myParams,40000);
            HttpPost httppost = new HttpPost("https://www.paypal.com/cgi-bin/webscr");
            try {
            String url = "https://www.paypal.com/cgi-bin/webscr?"; // or sandbox url here
            url += "business=" + "[email protected]" + "&";
            url += "cmd=" + "_xclick" + "&";
            url += "amount=" + "20.00" + "&";
            url += "item_name=" + "item_name" + "&";
            url += "item_number=" + "item_number" + "&";
            url += "quantity=" + "1" + "&";
            url += "currency_code=" + "USD" + "&";
            url += "no_shipping=" + "1" + "&";
            url += "rm=" + "2";

            HttpGet httpGet = new HttpGet(url);
            HttpResponse response = httpclient.execute(httpGet);

            InputStream is = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder str = new StringBuilder();
            String line = null;

            while((line = reader.readLine()) != null){
            str.append(line + "\n");
            }
            is.close();
            String responseText = str.toString();
            getContext().getResponse().getWriter().write(respo nseText); // Simply print the responseText string
            }
            catch (Exception e) {
            //TODO: handle exception
            }
            }

            I copied the code posted above by Rukmal Dias in my method, but my method have to return :
            return new ModelAndView(?????);

            Can anyone ask me how return the Paypal response from my ModelAndView payNow(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse)

            Thanks

            Comment


            • #7
              hi rita
              In here u need to redirect to pay-pal server/web page...

              try some thing like this,

              // Return type void / any type
              public void payNow() {

              boolean test = true;
              String url = "";

              url += (test)? "https://www.sandbox.paypal.com/cgi-bin/webscr?" : "https://www.paypal.com/cgi-bin/webscr?";
              url += "business=" + ownerEmail + "&";
              url += "cmd=" + "_xclick" + "&";
              url += "amount=" + "20.00" + "&";
              url += "item_name=" + "user_signup" + "&";
              url += "item_number=" + "item_number" + "&";
              url += "quantity=" + "1" + "&";
              url += "currency_code=" + "USD" + "&";
              url += "no_shipping=" + "1" + "&";
              url += "rm=" + "2";

              getContext().getResponse().sendRedirect(url);
              // Use this or any redirecting method in your framework.
              }

              Note: you need to log in before testing pay-pal test account. Otherwise it gives a 'Please login to use features' or some message.

              Rukmal Dias
              Last edited by rukmaldias; Jun 17th, 2011, 02:09 AM.

              Comment


              • #8
                IPN Listner

                Thanks rukmaldias,
                the payment on paypal works, now I have to test the return of the transaction from paypal.

                Paypal 's example for java/jsp about IPN listner is very poor, have you a template for spring MVC thath I cau use ?

                I think that is better write a class for listner, is it right ?

                Thanks
                Rita

                Comment

                Working...
                X