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

Posted

技术标签:

【中文标题】Paypal 和 Java:不显示订单摘要和确认失败【英文标题】:Paypal & Java: Not Showing order summary and failure in acknowledgement 【发布时间】:2012-10-25 04:20:31 【问题描述】:

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

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

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

显示订单夏季更改https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&amp;token=EC-50634332TM915520Dhttps://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&amp;token=" + token+"&amp;useraction=commit"

【讨论】:

以上是关于Paypal 和 Java:不显示订单摘要和确认失败的主要内容,如果未能解决你的问题,请参考以下文章

PayPal Express C# 订单摘要

如何使用 ActiveMerchant 和 PayPal Express 跳过订单确认页面

我能否在不依赖客户从 PayPal 中单击“返回站点”的情况下从 PayPal Standard 获得订单确认?

PayPal REST API 订单摘要 自定义描述

Paypal预先批准的付款,不显示价格?

接收订单详情和付款确认(电子商务网站)[关闭]