2011-11-03 3 views
4

내 웹 응용 프로그램에 페이팔을 통합하고 싶습니다. 편안한 요청에 RESTEasy API를 사용하고 있습니다. 내 응용 프로그램에서 페이팔을 통합하는 방법을 알고 싶습니다. 나는 그들의 웹 사이트에서 페이팔 자바 SDK를 가지고 dowonloaded하지만 지금은 내 애플 리케이션에서 페이팔을 통합하는 데 도움이 될 것입니다 좋은 applicaion 있습니다. 아무도 웹 사이트 또는 간단하게 이해할 수있는 페이팔의 단계 inegration을 보여주는 모든 사이트를 제안 할 수 있습니까?자바를 사용하여 웹 응용 프로그램에 페이팔 통합

답변

3

몇 가지 코드를 생성하는 examples과 심지어 wizard을 제공합니다.

+1

마법사 링크가 업데이트되었습니다. https://devtools-paypal.com/integrationwizard/ – maxivis

3

우리는 자바 웹 응용 프로그램에서 페이팔을 구성하려면 아래 단계를 따라야합니다. 여기에는 Paypal에 대한 Paypal 및 CreditCard에 대한 구성이 표시되어 있습니다.

1. 하나의 샌드 박스 계정을 만듭니다.
2. 웹 응용 프로그램을 만듭니다.
3. 코드를 사용하여 다음 struts2 작업을 통합하십시오. 다른 것과 함께 사용하려면 메서드를 사용하여 직접 사용하십시오. 이 me.Hope을 위해 잘 작동

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 




} 

는 페이팔을 통해 지불을 구성하려는 사람들에게 도움이 될 것입니다.

+0

전체 코드를 게시 할 수 있습니까? 나는 모든 수업을 통해 나처럼 페이팔에 익숙하지 않은 사람들이 그것에 대한 혜택을 얻을 수있게했다. – LifeSaver