3
votes

In my paypal JSP application , when redirect to paypal account(https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-50634332TM915520D) it doesn't show account summary(As in the image). At last at success page it shows failure also. enter image description here

. setcheckout.jsp is

<body>
        <%
            String url = "https://api-3t.sandbox.paypal.com/nvp";
            String charset = "UTF-8";
            String user = "cinewo_13895465_biz_api1.gmail.com";
            String pwd = "1233310405";
            String signature = "AbLJtIu5Xk-EeZNM1Qxyhl8A3UcjAXCXJk7gW24OlxsLXL3ORPJX5no3";

            //Get the amount here  

            String amount = "20";

            String amt = request.getParameter("amount");
            System.out.println("amt : "+amt);
            if ( amt != null) {
                amount = amt;                
                // Setting amount to  session 
                session.setAttribute("amount", amount);
            }

            // Please give your ip address here not localhost 
            //  if dont no ip addrress just take google on your sys and search what is my ip it shows give it here

            String returnurl = "http://192.168.0.230:8084/Payment/sucess.jsp";
            String cancelurl = "http://192.168.0.230:8084/Payment/canceled.jsp";


            String query = "USER=" + user + "&PWD=" + pwd + "&SIGNATURE=" + signature + "&PAYMENTREQUEST_0_PAYMENTACTION=Sale&"
                    + "PAYMENTREQUEST_0_AMT=" + amount + "&RETURNURL=" + returnurl + "&CANCELURL=" + cancelurl + "&METHOD=SetExpressCheckout"
                    + "&VERSION=84.0";


            URLConnection connection = new URL(url).openConnection();
            connection.setDoOutput(true); // Triggers POST.
            connection.setRequestProperty("Accept-Charset", charset);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
            OutputStream output = null;
            try {
                output = connection.getOutputStream();
                output.write(query.getBytes(charset));
            } finally {
                if (output != null) {
                    try {
                        output.close();
                    } catch (IOException logOrIgnore) {
                        System.out.println("logOrIgnore : " + logOrIgnore);
                    }
                }
            }

            InputStream resp = connection.getInputStream();

            // StringBufferInputStream buf = new StringBufferInputStream(s);
            InputStreamReader reader = new InputStreamReader(resp);

            // Read from the input stream.
            int charRead;
            String outp = "";
            char tmp;
            while ((charRead = reader.read()) >= 0) {
                System.out.print((char) charRead);
                tmp = (char) charRead;
                outp += Character.toString(tmp);
                // out.print((char)charRead);
            }

            out.println("outp : " + outp);

            // Close the InputStreamReader and the
            // StringBufferInputStream objects.
            resp.close();
            reader.close();


            String decoded = URLDecoder.decode(outp, charset);
            //String n= URLEncoder.encode(outp, charset);

            String[] params = decoded.split("&");
            String[] eachpair;
            HashMap<String, String> hm = new HashMap<String, String>();
            for (int i = 0; i < params.length; i++) {
                eachpair = params[i].split("=");
                hm.put(eachpair[0], eachpair[1]);
            }

            String ack = "", token = "", version = "", tms = "", bld = "", corelid = "";

            out.println("ACK : " + hm.get("ACK"));

            ack = hm.get("ACK");

            if (ack.equals("Success")) {
                token = hm.get("TOKEN");
                version = hm.get("VERSION");
                tms = hm.get("TIMESTAMP");
                bld = hm.get("BUILD");
                corelid = hm.get("CORRELATIONID");
                String logurl = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=" + token;
                response.sendRedirect(logurl);
            }

            System.out.println("resp :" + resp);

            // final String PAYPAL_URL = "https://api-3t.sandbox.paypal.com/nvp";

        %>
        <h1>Hello World!</h1>
    </body>

and success.jsp is

<%

        String user = "cinewo_13895465_biz_api1.gmail.com";
        String pwd = "1325310405";
        String signature = "AbLJtIu5Xk-EeZNM1Qxyhl8A3UcjAXCXJk7gW24OlxsLXL3ORPJX5no3";
        String payerID = request.getParameter("PayerID");
        String token = request.getParameter("token");
        String amount = "25";

        //Reading amount from session

        String amt = session.getAttribute("amount")+"";
        if(amt!=""){
            amount=amt;
        }

        String version = request.getParameter("VERSION");
        String build = request.getParameter("BUILD");
        String charset = "UTF-8";

        token = URLEncoder.encode(token, charset);
        payerID = URLEncoder.encode(payerID, charset);

        String query = "USER=" + user + "&PWD=" + pwd + "&SIGNATURE=" + signature + "&PAYMENTACTION=Sale&"
                + "PAYERID=" + payerID + "&TOKEN=" + token + "&AMT=" + amount + "&METHOD=DoExpressCheckoutPayment"
                + "&VERSION=84.0";

        String url = "https://api-3t.sandbox.paypal.com/nvp";

        URLConnection connection = new URL(url).openConnection();
        connection.setDoOutput(true); // Triggers POST.
        connection.setRequestProperty("Accept-Charset", charset);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
        OutputStream output = null;

        try {
            output = connection.getOutputStream();
            output.write(query.getBytes(charset));
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException logOrIgnore) {
                    System.out.println("logOrIgnore : " + logOrIgnore);
                }
            }
        }

        InputStream resp = connection.getInputStream();

        InputStreamReader reader = new InputStreamReader(resp);

        // Read from the input stream.
        int charRead;
        String outp = "";
        char tmp;
        while ((charRead = reader.read()) >= 0) {
            System.out.print((char) charRead);
            tmp = (char) charRead;
            outp += Character.toString(tmp);
            // out.print((char)charRead);
        }
        String decoded = URLDecoder.decode(outp, charset);

        //out.println("decoded : " + decoded);

        String[] params = decoded.split("&");
        String[] eachpair;
        HashMap<String, String> hm = new HashMap<String, String>();
        for (int i = 0; i < params.length; i++) {
            eachpair = params[i].split("=");
            hm.put(eachpair[0], eachpair[1]);
        }

        String ack = "", corelid = "", tms = "", bld = "";

        out.println("ACK : " + hm.get("ACK"));

        ack = hm.get("ACK");
        if (ack.equals("Success")) {

            token = hm.get("TOKEN");
            version = hm.get("VERSION");
            tms = hm.get("TIMESTAMP");
            bld = hm.get("BUILD");
            corelid = hm.get("CORRELATIONID");

            out.println("token : " + token);
            out.println("version : " + version);
            out.println("tms : " + tms);
            out.println("bld : " + bld);
            out.println("corelid : " + corelid);

        } else {
            out.println("Sorry try again later");
        }

        // Close the InputStreamReader and the
        // StringBufferInputStream objects.
        resp.close();
        reader.close();
    %>


    <h1>Hello Sucess</h1>
</body>
1

1 Answers

0
votes

To show order summery change https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-50634332TM915520D to https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=" + token+"&useraction=commit"