使用 Java 将 Paypal 集成到 Web 应用程序中

发布于 2024-12-13 10:35:15 字数 183 浏览 0 评论 0原文

我想将 paypal 集成到我的网络应用程序中。我正在使用 RESTEasy API 来处理宁静的请求。我想知道如何将 paypal 集成到我的应用程序中。我已经从他们的网站下载了 paypal java sdk,但现在我没有好的应用程序可以帮助我将 paypal 集成到我的应用程序中。任何人都可以建议任何网站,显示在网站中逐步集成贝宝或简单易懂吗?

I want to integrate paypal in my web application. I am using RESTEasy API for restful requests. I want to know that how to integrate paypal in my application. I have dowonloaded paypal java sdk from their web site but right now I have no good applicaion which will help me to integrate paypal in my app. Can anybody suggest any site which shows step by step inegration of paypal in website or simple to understand ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤舞 2024-12-20 10:35:15

Paypal 提供了一些示例< /a> 甚至是一个向导为您生成一些代码。

在前面的链接离线后,从这个小教程开始: https://developer.paypal.com/docs/checkout/integrate/#

Paypal offers a few examples and even a wizard that generates you some code.

After the previous links gone offline start with this small tutorial: https://developer.paypal.com/docs/checkout/integrate/#

秋叶绚丽 2024-12-20 10:35:15

我们必须按照以下步骤在 java web 应用程序中配置 paypal。这里我展示了 Paypal 到 Paypal 和信用卡到 Paypal 付款的配置。

1.创建一个沙盒账户。
2.创建Web应用程序。
3.将以下struts2操作与您的代码集成。如果您想与任何其他操作一起使用,则只需采用方法并直接使用即可。

public class PaypalAction extends ActionSupport implements SessionAware {


    private static final long serialVersionUID = 1L;
    private String resultString;
    private String token;

    @SuppressWarnings("rawtypes")
    private Map finalMap = new HashMap();

    public void paypalPay() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        Integer payPercent = 10;     
        Map result = new HashMap();
        String data1 = "";

        try {
            data1 = URLEncoder.encode("USER", "UTF-8") + "="+ URLEncoder.encode("Sandbox UserName","UTF-8");
            data1 += "&" + URLEncoder.encode("PWD", "UTF-8") + "="+ URLEncoder.encode("Sandbox Password", "UTF-8");
            data1 += "&" + URLEncoder.encode("SIGNATURE", "UTF-8") + "="+ URLEncoder.encode("Sandbox Signature","UTF-8");
            data1 += "&" + URLEncoder.encode("METHOD", "UTF-8") + "="+ URLEncoder.encode("SetExpressCheckout", "UTF-8");

            data1 += "&" + URLEncoder.encode("RETURNURL", "UTF-8") + "=" + URLEncoder.encode(request.getRequestURL().toString().replaceAll(request.getServletPath(), "") + "/successPage","UTF-8");
            data1 += "&" + URLEncoder.encode("CANCELURL", "UTF-8") + "="+ URLEncoder.encode(request.getRequestURL().toString().replaceAll(request.getServletPath(), "") + "/failurePage",   "UTF-8");

            data1 += "&" + URLEncoder.encode("VERSION", "UTF-8") + "="+ URLEncoder.encode("104.0", "UTF-8");            
            data1 += "&" + URLEncoder.encode("AMT", "UTF-8")+ "=" + URLEncoder.encode("10", "UTF-8");                       
            data1 += "&" + URLEncoder.encode("CURRENCYCODE", "UTF-8") + "=" + URLEncoder.encode("USD", "UTF-8");            
            data1 += "&" + URLEncoder.encode("L_NAME0", "UTF-8") + "=" + URLEncoder.encode("Sample Test", "UTF-8");
            data1 += "&" + URLEncoder.encode("L_AMT0", "UTF-8") + "=" + URLEncoder.encode("10", "UTF-8");                   
            data1 += "&" + URLEncoder.encode("CUSTOM", "UTF-8") + "="+ URLEncoder.encode("Thank You For business","UTF-8");         
            data1 += "&" + URLEncoder.encode("DESC", "UTF-8") + "=" + URLEncoder.encode("Product Details", "UTF-8");                    
            data1 += "&" + URLEncoder.encode("NOSHIPPING", "UTF-8") + "="+ URLEncoder.encode("1", "UTF-8");


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        result = post(data1);



        Iterator<?> iter = result.entrySet().iterator();

        while (iter.hasNext()) {
            Map.Entry mEntry = (Map.Entry) iter.next();

        }

        if(result!=null){

            token = (String) result.get("TOKEN");
            String url = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express"+ "-" + "checkout&token=" + (String) result.get("TOKEN");
            try {
                response.sendRedirect(url);
            } catch (IOException e) {

                e.printStackTrace();
            }

        }


    }

    public String successPage() {
        HttpServletRequest request = ServletActionContext.getRequest();

        String payerID = request.getParameter("PayerID");
        String token=request.getParameter("token");
        doPaypalPayment(payerID,token);
        return "success";
    }


    public String failurePage()
    {

        return "failurePage";
    }


    public void doPaypalPayment(String payerID, String token2) {
        Map result = new HashMap();
        String data = "";
        try {

            data1 = URLEncoder.encode("USER", "UTF-8") + "="+ URLEncoder.encode("Sandbox UserName","UTF-8");
            data1 += "&" + URLEncoder.encode("PWD", "UTF-8") + "="+ URLEncoder.encode("Sandbox Password", "UTF-8");
            data1 += "&" + URLEncoder.encode("SIGNATURE", "UTF-8") + "="+ URLEncoder.encode("Sandbox Signature","UTF-8");

            data += "&" + URLEncoder.encode("METHOD", "UTF-8") + "="+ URLEncoder.encode("DoExpressCheckoutPayment", "UTF-8");
            data += "&" + URLEncoder.encode("PAYERID", "UTF-8") + "="+ URLEncoder.encode(payerID, "UTF-8");
            data += "&" + URLEncoder.encode("PAYMENTACTION", "UTF-8") + "="+ URLEncoder.encode("Sale", "UTF-8");
            data += "&" + URLEncoder.encode("TOKEN", "UTF-8") + "="+ URLEncoder.encode(token2, "UTF-8");
            data += "&" + URLEncoder.encode("AMT", "UTF-8") + "="+ URLEncoder.encode("10", "UTF-8");
            data += "&" + URLEncoder.encode("VERSION", "UTF-8") + "="+ URLEncoder.encode("104.0", "UTF-8");
            data += "&" + URLEncoder.encode("CURRENCYCODE", "UTF-8") + "="+ URLEncoder.encode("USD", "UTF-8");

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        result = post(data);    
    }


     public void deformatNVP()
        {   
            try
            {
                String delims = "[&]";
                String equals = "[=]";
                String[] tokens = this.resultString.split(delims);

                for(int i = 0; i < tokens.length; i++)
                {
                    String[] newTokens = tokens[i].split(equals);
                    this.finalMap.put(URLDecoder.decode(newTokens[0], "UTF-8"), URLDecoder.decode(newTokens[1], "UTF-8"));
                }

            } catch (Exception e) {
                System.out.println(e.toString());
            }
            //return finalMap;
        }

        public Map post(String data)
        {
            try
            {
                //Connect to the url
                URL url = new URL("https://api-3t.sandbox.paypal.com/nvp");
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                //Post the data
                wr.write(data);
                wr.flush();

                // Get the response
                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                this.resultString = rd.readLine();
                deformatNVP();
                wr.close();
                rd.close();

            } catch (Exception e) {
                System.out.println(e.toString());
            }
            return finalMap;  
        }





        @Override
        public void setSession(Map<String, Object> arg0) {
            // TODO Auto-generated method stub

        }


         public void doPaypalPaymentWithCreditCard() {
                try 
                {
                    //Load the caller service

                    //Create a new map to hold the result
                    Map result = new HashMap();

                    // Construct data request string
                   String data1 = URLEncoder.encode("USER", "UTF-8") + "="+ URLEncoder.encode("Sandbox UserName","UTF-8");
                   data1 += "&" + URLEncoder.encode("PWD", "UTF-8") + "="+ URLEncoder.encode("Sandbox Password", "UTF-8");
                   data1 += "&" + URLEncoder.encode("SIGNATURE", "UTF-8") + "="+ URLEncoder.encode("Sandbox Signature","UTF-8");                    data += "&" + URLEncoder.encode("METHOD", "UTF-8") + "=" + URLEncoder.encode("DoDirectPayment", "UTF-8");
                    data += "&" + URLEncoder.encode("AMT", "UTF-8") + "=" + URLEncoder.encode("20.10", "UTF-8");
                    data += "&" + URLEncoder.encode("VERSION", "UTF-8") + "=" + URLEncoder.encode("80.0", "UTF-8");
                    data += "&" + URLEncoder.encode("IPADDRESS", "UTF-8") + "=" + URLEncoder.encode("192.168.1.0", "UTF-8");
                    data += "&" + URLEncoder.encode("PAYMENTACTION", "UTF-8") + "=" + URLEncoder.encode("Sale", "UTF-8");
                    data += "&" + URLEncoder.encode("CREDITCARDTYPE", "UTF-8") + "=" + URLEncoder.encode("Visa", "UTF-8");
                    data += "&" + URLEncoder.encode("ACCT", "UTF-8") + "=" + URLEncoder.encode("4532513511140817", "UTF-8");
                    data += "&" + URLEncoder.encode("EXPDATE", "UTF-8") + "=" + URLEncoder.encode("102014", "UTF-8");
                    data += "&" + URLEncoder.encode("CVV2", "UTF-8") + "=" + URLEncoder.encode("123", "UTF-8");
                    data += "&" + URLEncoder.encode("FIRSTNAME", "UTF-8") + "=" + URLEncoder.encode("Jason", "UTF-8");
                    data += "&" + URLEncoder.encode("LASTNAME", "UTF-8") + "=" + URLEncoder.encode("Michels", "UTF-8");
                    data += "&" + URLEncoder.encode("STREET", "UTF-8") + "=" + URLEncoder.encode("123", "UTF-8");
                    data += "&" + URLEncoder.encode("CITY", "UTF-8") + "=" + URLEncoder.encode("Papillion", "UTF-8");
                    data += "&" + URLEncoder.encode("STATE", "UTF-8") + "=" + URLEncoder.encode("NE", "UTF-8");
                    data += "&" + URLEncoder.encode("ZIP", "UTF-8") + "=" + URLEncoder.encode("68046", "UTF-8");
                    data += "&" + URLEncoder.encode("COUNTRYCODE", "UTF-8") + "=" + URLEncoder.encode("US", "UTF-8");


                    result = post(data);

                    //This will iterate through the entire response map
                    Iterator iter = result.entrySet().iterator();
                    while (iter.hasNext()) {
                            Map.Entry mEntry = (Map.Entry) iter.next();
                            System.out.println(mEntry.getKey() + " : " + mEntry.getValue());
                    }

                    //Now that you have a response check to see if it is a success
                    String ack = "" + result.get("ACK");
                    if("Success".equals(ack))
                    {
                        System.out.println("Congratulations your transaction was a success");
                    }
                    else
                    {
                        System.out.println("There was an error with your request.");
                    }
                } 
                catch (Exception e) 
                {
                    System.out.println(e.toString());
                }
            }//end of main function




}

这对我来说效果很好。希望它对那些想要通过 PayPal 配置付款的人有所帮助。

We have to follow the below step for configuring paypal in java web application.Here i have shown the configuration for Paypal to Paypal and CreditCard to Paypal payment.

1.create one sandbox account.
2.create web application.
3.Integrate the following struts2 action with your code.If you want to use with any other then just take methods and use directly.

public class PaypalAction extends ActionSupport implements SessionAware {


    private static final long serialVersionUID = 1L;
    private String resultString;
    private String token;

    @SuppressWarnings("rawtypes")
    private Map finalMap = new HashMap();

    public void paypalPay() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        Integer payPercent = 10;     
        Map result = new HashMap();
        String data1 = "";

        try {
            data1 = URLEncoder.encode("USER", "UTF-8") + "="+ URLEncoder.encode("Sandbox UserName","UTF-8");
            data1 += "&" + URLEncoder.encode("PWD", "UTF-8") + "="+ URLEncoder.encode("Sandbox Password", "UTF-8");
            data1 += "&" + URLEncoder.encode("SIGNATURE", "UTF-8") + "="+ URLEncoder.encode("Sandbox Signature","UTF-8");
            data1 += "&" + URLEncoder.encode("METHOD", "UTF-8") + "="+ URLEncoder.encode("SetExpressCheckout", "UTF-8");

            data1 += "&" + URLEncoder.encode("RETURNURL", "UTF-8") + "=" + URLEncoder.encode(request.getRequestURL().toString().replaceAll(request.getServletPath(), "") + "/successPage","UTF-8");
            data1 += "&" + URLEncoder.encode("CANCELURL", "UTF-8") + "="+ URLEncoder.encode(request.getRequestURL().toString().replaceAll(request.getServletPath(), "") + "/failurePage",   "UTF-8");

            data1 += "&" + URLEncoder.encode("VERSION", "UTF-8") + "="+ URLEncoder.encode("104.0", "UTF-8");            
            data1 += "&" + URLEncoder.encode("AMT", "UTF-8")+ "=" + URLEncoder.encode("10", "UTF-8");                       
            data1 += "&" + URLEncoder.encode("CURRENCYCODE", "UTF-8") + "=" + URLEncoder.encode("USD", "UTF-8");            
            data1 += "&" + URLEncoder.encode("L_NAME0", "UTF-8") + "=" + URLEncoder.encode("Sample Test", "UTF-8");
            data1 += "&" + URLEncoder.encode("L_AMT0", "UTF-8") + "=" + URLEncoder.encode("10", "UTF-8");                   
            data1 += "&" + URLEncoder.encode("CUSTOM", "UTF-8") + "="+ URLEncoder.encode("Thank You For business","UTF-8");         
            data1 += "&" + URLEncoder.encode("DESC", "UTF-8") + "=" + URLEncoder.encode("Product Details", "UTF-8");                    
            data1 += "&" + URLEncoder.encode("NOSHIPPING", "UTF-8") + "="+ URLEncoder.encode("1", "UTF-8");


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        result = post(data1);



        Iterator<?> iter = result.entrySet().iterator();

        while (iter.hasNext()) {
            Map.Entry mEntry = (Map.Entry) iter.next();

        }

        if(result!=null){

            token = (String) result.get("TOKEN");
            String url = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express"+ "-" + "checkout&token=" + (String) result.get("TOKEN");
            try {
                response.sendRedirect(url);
            } catch (IOException e) {

                e.printStackTrace();
            }

        }


    }

    public String successPage() {
        HttpServletRequest request = ServletActionContext.getRequest();

        String payerID = request.getParameter("PayerID");
        String token=request.getParameter("token");
        doPaypalPayment(payerID,token);
        return "success";
    }


    public String failurePage()
    {

        return "failurePage";
    }


    public void doPaypalPayment(String payerID, String token2) {
        Map result = new HashMap();
        String data = "";
        try {

            data1 = URLEncoder.encode("USER", "UTF-8") + "="+ URLEncoder.encode("Sandbox UserName","UTF-8");
            data1 += "&" + URLEncoder.encode("PWD", "UTF-8") + "="+ URLEncoder.encode("Sandbox Password", "UTF-8");
            data1 += "&" + URLEncoder.encode("SIGNATURE", "UTF-8") + "="+ URLEncoder.encode("Sandbox Signature","UTF-8");

            data += "&" + URLEncoder.encode("METHOD", "UTF-8") + "="+ URLEncoder.encode("DoExpressCheckoutPayment", "UTF-8");
            data += "&" + URLEncoder.encode("PAYERID", "UTF-8") + "="+ URLEncoder.encode(payerID, "UTF-8");
            data += "&" + URLEncoder.encode("PAYMENTACTION", "UTF-8") + "="+ URLEncoder.encode("Sale", "UTF-8");
            data += "&" + URLEncoder.encode("TOKEN", "UTF-8") + "="+ URLEncoder.encode(token2, "UTF-8");
            data += "&" + URLEncoder.encode("AMT", "UTF-8") + "="+ URLEncoder.encode("10", "UTF-8");
            data += "&" + URLEncoder.encode("VERSION", "UTF-8") + "="+ URLEncoder.encode("104.0", "UTF-8");
            data += "&" + URLEncoder.encode("CURRENCYCODE", "UTF-8") + "="+ URLEncoder.encode("USD", "UTF-8");

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        result = post(data);    
    }


     public void deformatNVP()
        {   
            try
            {
                String delims = "[&]";
                String equals = "[=]";
                String[] tokens = this.resultString.split(delims);

                for(int i = 0; i < tokens.length; i++)
                {
                    String[] newTokens = tokens[i].split(equals);
                    this.finalMap.put(URLDecoder.decode(newTokens[0], "UTF-8"), URLDecoder.decode(newTokens[1], "UTF-8"));
                }

            } catch (Exception e) {
                System.out.println(e.toString());
            }
            //return finalMap;
        }

        public Map post(String data)
        {
            try
            {
                //Connect to the url
                URL url = new URL("https://api-3t.sandbox.paypal.com/nvp");
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                //Post the data
                wr.write(data);
                wr.flush();

                // Get the response
                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                this.resultString = rd.readLine();
                deformatNVP();
                wr.close();
                rd.close();

            } catch (Exception e) {
                System.out.println(e.toString());
            }
            return finalMap;  
        }





        @Override
        public void setSession(Map<String, Object> arg0) {
            // TODO Auto-generated method stub

        }


         public void doPaypalPaymentWithCreditCard() {
                try 
                {
                    //Load the caller service

                    //Create a new map to hold the result
                    Map result = new HashMap();

                    // Construct data request string
                   String data1 = URLEncoder.encode("USER", "UTF-8") + "="+ URLEncoder.encode("Sandbox UserName","UTF-8");
                   data1 += "&" + URLEncoder.encode("PWD", "UTF-8") + "="+ URLEncoder.encode("Sandbox Password", "UTF-8");
                   data1 += "&" + URLEncoder.encode("SIGNATURE", "UTF-8") + "="+ URLEncoder.encode("Sandbox Signature","UTF-8");                    data += "&" + URLEncoder.encode("METHOD", "UTF-8") + "=" + URLEncoder.encode("DoDirectPayment", "UTF-8");
                    data += "&" + URLEncoder.encode("AMT", "UTF-8") + "=" + URLEncoder.encode("20.10", "UTF-8");
                    data += "&" + URLEncoder.encode("VERSION", "UTF-8") + "=" + URLEncoder.encode("80.0", "UTF-8");
                    data += "&" + URLEncoder.encode("IPADDRESS", "UTF-8") + "=" + URLEncoder.encode("192.168.1.0", "UTF-8");
                    data += "&" + URLEncoder.encode("PAYMENTACTION", "UTF-8") + "=" + URLEncoder.encode("Sale", "UTF-8");
                    data += "&" + URLEncoder.encode("CREDITCARDTYPE", "UTF-8") + "=" + URLEncoder.encode("Visa", "UTF-8");
                    data += "&" + URLEncoder.encode("ACCT", "UTF-8") + "=" + URLEncoder.encode("4532513511140817", "UTF-8");
                    data += "&" + URLEncoder.encode("EXPDATE", "UTF-8") + "=" + URLEncoder.encode("102014", "UTF-8");
                    data += "&" + URLEncoder.encode("CVV2", "UTF-8") + "=" + URLEncoder.encode("123", "UTF-8");
                    data += "&" + URLEncoder.encode("FIRSTNAME", "UTF-8") + "=" + URLEncoder.encode("Jason", "UTF-8");
                    data += "&" + URLEncoder.encode("LASTNAME", "UTF-8") + "=" + URLEncoder.encode("Michels", "UTF-8");
                    data += "&" + URLEncoder.encode("STREET", "UTF-8") + "=" + URLEncoder.encode("123", "UTF-8");
                    data += "&" + URLEncoder.encode("CITY", "UTF-8") + "=" + URLEncoder.encode("Papillion", "UTF-8");
                    data += "&" + URLEncoder.encode("STATE", "UTF-8") + "=" + URLEncoder.encode("NE", "UTF-8");
                    data += "&" + URLEncoder.encode("ZIP", "UTF-8") + "=" + URLEncoder.encode("68046", "UTF-8");
                    data += "&" + URLEncoder.encode("COUNTRYCODE", "UTF-8") + "=" + URLEncoder.encode("US", "UTF-8");


                    result = post(data);

                    //This will iterate through the entire response map
                    Iterator iter = result.entrySet().iterator();
                    while (iter.hasNext()) {
                            Map.Entry mEntry = (Map.Entry) iter.next();
                            System.out.println(mEntry.getKey() + " : " + mEntry.getValue());
                    }

                    //Now that you have a response check to see if it is a success
                    String ack = "" + result.get("ACK");
                    if("Success".equals(ack))
                    {
                        System.out.println("Congratulations your transaction was a success");
                    }
                    else
                    {
                        System.out.println("There was an error with your request.");
                    }
                } 
                catch (Exception e) 
                {
                    System.out.println(e.toString());
                }
            }//end of main function




}

This is working fine for me.Hope it will help to those who want to configure payment through paypal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文