【问题标题】:Paypal Integration on Java web applicatonJava Web 应用程序上的 Paypal 集成
【发布时间】:2014-08-02 21:53:14
【问题描述】:

目前,我成功拨打了第一个电话 https://api-3t.sandbox.paypal.com/nvp 这让我回到了TOKENACK,这当然是成功的......现在的问题是如何从这些变量中捕获值。我想不出办法。有人可以帮我在servlet 中获取这些值,以便我可以比较和操作在线支付的其他步骤!

提前致谢!

这是我发送给https://api-3t.sandbox.paypal.com/nvp的表格

<input type="text" name="USER" value="rspunk07-facilitator_api1.hotmail.com" /><br/>
<input type="text" name="PWD" value="1402570771" /><br/>
<input type="text" name="SIGNATURE" value="AOgUKQLphsryE4s2aIWzkssJyVf3ALKhcP5TZ2W4CnDz.bAbL.WoCv9q" /><br/>
<input type="text" name="METHOD" value="SetExpressCheckout" /><br/>
<input type="text" name="VERSION" value="98" /><br/>
<input type="text" name="PAYMENTREQUEST_0_AMT" value="10" /><br/>
<input type="text" name="PAYMENTREQUEST_0_CURRENCYCODE" value="USD" /><br/>
<input type="text" name="PAYMENTREQUEST_0_PAYMENTACTION" value="SALE" /><br/>
<input type="text" name="cancelUrl" value="http://localhost:8084/E-Drivers_Licensing_System/onlinepayment.jsp" /><br/>
<input type="text" name="returnUrl" value="http://localhost:8084/E-Drivers_Licensing_System/success.jsp" /><br/>

<input type="submit" name="h" value="Register"/>&nbsp;&nbsp;
<input type="reset" name="cancel" value="Cancel"/><br/>

对此,我在同一网址https://api-3t.sandbox.paypal.com/nvp 中得到TOKEN=EC%2d08371303KU3173044&amp;TIMESTAMP=2014%2d06%2d17T10%3a32%3a09Z&amp;CORRELATIONID=26d5a8792d431&amp;ACK=Success&amp;VERSION=98&amp;BUILD=11457922

现在,我遇到的问题是,从我的 servlet 中捕获 url 中提供的详细信息

public class OnlinePaymentController extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {




    } finally {            
        out.close();
    }
}

我该怎么做?

【问题讨论】:

    标签: java jsp servlets paypal


    【解决方案1】:
    // This method is called by the servlet container to process a GET request.
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGetOrPost(req, resp);
    }
    
    // This method is called by the servlet container to process a POST request.
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGetOrPost(req, resp);
    }
    
    // This method handles both GET and POST requests.
    private void doGetOrPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // Get the value of a request parameter; the name is case-sensitive
        String name = "param";
        String value = req.getParameter(name);
        if (value == null) {
            // The request parameter 'param' was not present in the query string
            // e.g. http://hostname.com?a=b
        } else if ("".equals(value)) {
            // The request parameter 'param' was present in the query string but has no value
            // e.g. http://hostname.com?param=&a=b
        }
    
        // The following generates a page showing all the request parameters
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/plain");
    
        // Get the values of all request parameters
        Enumeration enum = req.getParameterNames();
        for (; enum.hasMoreElements(); ) {
            // Get the name of the request parameter
            name = (String)enum.nextElement();
            out.println(name);
    
            // Get the value of the request parameter
            value = req.getParameter(name);
    
            // If the request parameter can appear more than once in the query string, get all values
            String[] values = req.getParameterValues(name);
    
            for (int i=0; i<values.length; i++) {
                out.println("    "+values[i]);
            }
        }
        out.close();
    }
    

    【讨论】:

      【解决方案2】:

      我认为问题在于,您真的不想将用户重定向到 Paypal,因为 Paypal API 响应并非旨在直接传回给用户。

      相反,您的 Servlet 需要充当客户端,因此流程如下:

      • 用户填写表单并提交
      • 表单被提交到您的 Servlet,而不是直接提交给 API
      • 您的 Servlet 使用 Java HTTP 客户端(例如 http://hc.apache.org/httpcomponents-client-ga/)向 API 发出请求
      • 您的 Servlet 解析并解码来自 paypal API 的响应
      • 您的 Servlet 转发到代表付款流程下一步的 jsp。

      大概您想要的只是令牌,然后您可以将其传递回客户端以写入隐藏字段并在下一步中提交。

      我对 PayPal API 不是很熟悉,但我建议您进一步探索他们的文档,以确保这是访问他们服务的最佳方式。

      【讨论】:

        猜你喜欢
        • 2011-07-25
        • 1970-01-01
        • 2011-12-20
        • 2014-11-07
        • 2016-09-27
        • 1970-01-01
        • 2014-09-22
        • 2013-09-09
        • 2016-04-30
        相关资源
        最近更新 更多