Paypal& Java:不显示订单摘要和确认失败

时间:2012-11-05 10:35:05

标签: java jsp paypal paypal-sandbox

在我的paypal JSP应用程序中,当重定向到paypal帐户(https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-50634332TM915520D)时,它不会显示帐户摘要(如图所示)。最后在成功页面上也显示失败。 enter image description here

。 setcheckout.jsp是

<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>

和success.jsp是

        &LT;%

        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 个答案:

答案 0 :(得分:0)

将订单摘要更改https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-50634332TM915520D显示为https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=" + token+"&useraction=commit"