daily pastebin goal
40%
SHARE
TWEET

Untitled

a guest Jul 10th, 2015 239 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package it.myproject.movieuniverse.server;
  2.  
  3.  
  4.  
  5. /**
  6.  * Created by IntelliJ IDEA.
  7.  * User: lhuynh
  8.  * Date: Dec 6, 2007
  9.  * Time: 5:06:52 PM
  10.  * To change this template use File | Settings | File Templates.
  11.  */
  12.  
  13. import java.net.URLDecoder.*;
  14. import java.util.*;
  15. import java.util.HashMap;
  16. import java.util.StringTokenizer.*;
  17. import java.io.*;
  18. import java.net.*;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21.  
  22. public class paypalfunctions  {
  23.  
  24.  
  25.     private String gv_APIUserName;
  26.     private String gv_APIPassword;
  27.     private String gv_APISignature;
  28.  
  29.     private String gv_APIEndpoint;
  30.     private String  gv_BNCode;
  31.  
  32.     private String  gv_Version;
  33.     private String  gv_nvpHeader;
  34.     private String  gv_ProxyServer;
  35.     private String gv_ProxyServerPort;
  36.     private int gv_Proxy;
  37.     private boolean gv_UseProxy;
  38.     private String PAYPAL_URL;
  39.  
  40.     public paypalfunctions() {//lhuynh - Actions to be Done on init of this class
  41.  
  42.     //BN Code is only applicable for partners
  43.     gv_BNCode           ="PP-ECWizard";
  44.         // Replace <API_USERNAME> with your API Username
  45.         // Replace <API_PASSWORD> with your API Password
  46.         // Replace <API_SIGNATURE> with your Signature
  47.     gv_APIUserName      ="movieuniverse_api1.gmail.com";
  48.     gv_APIPassword      ="E4T76Z68V4C38FS6";
  49.     gv_APISignature ="AFcWxV21C7fd0v3bYYYRCpSSRl31AWKgxYWxptrQw.jokhnsrfl4nC6m";
  50.  
  51.     boolean bSandbox = true;
  52.  
  53.     /*
  54.     Servers for NVP API
  55.     Sandbox: https://api-3t.sandbox.paypal.com/nvp
  56.     Live: https://api-3t.paypal.com/nvp
  57.     */
  58.  
  59.     /*
  60.     Redirect URLs for PayPal Login Screen
  61.     Sandbox: https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token=XXXX
  62.     Live: https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=XXXX
  63.     */
  64.  
  65.     if (bSandbox == true)
  66.     {
  67.         gv_APIEndpoint ="https://api-3t.sandbox.paypal.com/nvp";
  68.         PAYPAL_URL ="https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=";
  69.     }
  70.     else
  71.     {
  72.         gv_APIEndpoint ="https://api-3t.paypal.com/nvp";
  73.         PAYPAL_URL ="https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
  74.     }
  75.  
  76.     String HTTPREQUEST_PROXYSETTING_SERVER = "";
  77.     String HTTPREQUEST_PROXYSETTING_PORT = "";
  78.     boolean USE_PROXY = false;
  79.  
  80.     gv_Version  = "93";
  81.  
  82.     //WinObjHttp Request proxy settings.
  83.     gv_ProxyServer      = HTTPREQUEST_PROXYSETTING_SERVER;
  84.     gv_ProxyServerPort = HTTPREQUEST_PROXYSETTING_PORT;
  85.     gv_Proxy            = 2;    //'setting for proxy activation
  86.     gv_UseProxy         = USE_PROXY;
  87.  
  88.  
  89. }
  90.  
  91.  
  92.  
  93. /*********************************************************************************
  94.   * CallShortcutExpressCheckout: Function to perform the SetExpressCheckout API call
  95.   *
  96.   * Inputs:
  97.   *             paymentAmount:          Total value of the shopping cart
  98.   *             currencyCodeType:       Currency code value the PayPal API
  99.   *             paymentType:            paymentType has to be one of the following values: Sale or Order or Authorization
  100.   *             returnURL:                      the page where buyers return to after they are done with the payment review on PayPal
  101.   *             cancelURL:                      the page where buyers return to when they cancel the payment review on PayPal
  102.   *
  103.   * Output: Returns a HashMap object containing the response from the server.
  104. *********************************************************************************/
  105. public HashMap CallShortcutExpressCheckout( String paymentAmount, String returnURL, String cancelURL)
  106. {
  107.  
  108.         /*
  109.         '------------------------------------
  110.         ' The currencyCodeType and paymentType
  111.         ' are set to the selections made on the Integration Assistant
  112.         '------------------------------------
  113.         */
  114.  
  115.         String currencyCodeType = "EUR";
  116.         String paymentType = "Sale";
  117.  
  118.     /*
  119.     Construct the parameter string that describes the PayPal payment
  120.     the varialbes were set in the web form, and the resulting string
  121.     is stored in $nvpstr
  122.     */
  123.     String nvpstr = "&PAYMENTREQUEST_0_AMT=" + paymentAmount + "&PAYMENTREQUEST_0_PAYMENTACTION=" + paymentType + "&ReturnUrl=" + URLEncoder.encode( returnURL ) + "&CANCELURL=" + URLEncoder.encode( cancelURL ) + "&PAYMENTREQUEST_0_CURRENCYCODE=" + currencyCodeType;
  124.  
  125.     /*
  126.     Make the call to PayPal to get the Express Checkout token
  127.     If the API call succeded, then redirect the buyer to PayPal
  128.     to begin to authorize payment.  If an error occured, show the
  129.     resulting errors
  130.     */
  131.  
  132.     HashMap nvp = httpcall("SetExpressCheckout", nvpstr);
  133.     String strAck = nvp.get("ACK").toString();
  134.     if(strAck !=null && strAck.equalsIgnoreCase("Success"))
  135.     {
  136.         return nvp;
  137.     }
  138.  
  139.     return null;
  140. }
  141.  
  142. /*********************************************************************************
  143.   * CallMarkExpressCheckout: Function to perform the SetExpressCheckout API call
  144.   *
  145.   * Inputs:
  146.   *             paymentAmount:          Total value of the shopping cart
  147.   *             currencyCodeType:       Currency code value the PayPal API
  148.   *             paymentType:            paymentType has to be one of the following values: Sale or Order or Authorization
  149.   *             returnURL:                      the page where buyers return to after they are done with the payment review on PayPal
  150.   *             cancelURL:                      the page where buyers return to when they cancel the payment review on PayPal
  151.   *             shipToName:             the Ship to name entered on the merchant's site
  152.   *             shipToStreet:           the Ship to Street entered on the merchant's site
  153.   *             shipToCity:                     the Ship to City entered on the merchant's site
  154.   *             shipToState:            the Ship to State entered on the merchant's site
  155.   *             shipToCountryCode:      the Code for Ship to Country entered on the merchant's site
  156.   *             shipToZip:                      the Ship to ZipCode entered on the merchant's site
  157.   *             shipToStreet2:          the Ship to Street2 entered on the merchant's site
  158.   *             phoneNum:                       the phoneNum  entered on the merchant's site
  159.   *
  160.   * Output: Returns a HashMap object containing the response from the server.
  161. *********************************************************************************/
  162. public HashMap CallMarkExpressCheckout( String paymentAmount, String returnURL, String cancelURL, String shipToName, String shipToStreet, String shipToCity, String shipToState,
  163.                                         String shipToCountryCode, String shipToZip, String shipToStreet2, String phoneNum)
  164. {
  165.         /*
  166.         '------------------------------------
  167.         ' The currencyCodeType and paymentType
  168.         ' are set to the selections made on the Integration Assistant
  169.         '------------------------------------
  170.         */
  171.         String currencyCodeType = "EUR";
  172.         String paymentType = "SALE";
  173.  
  174.     /*
  175.     Construct the parameter string that describes the PayPal payment
  176.     the varialbes were set in the web form, and the resulting string
  177.     is stored in $nvpstr
  178.     */
  179.     String  nvpstr = "ADDROVERRIDE=1&PAYMENTREQUEST_0_AMT=" + paymentAmount + "&PAYMENTREQUEST_0_PAYMENTACTION=" + paymentType;
  180.     nvpstr=nvpstr.concat("&PAYMENTREQUEST_0_CURRENCYCODE=" + currencyCodeType + "&ReturnUrl=" + URLEncoder.encode( returnURL  ) + "&CANCELURL=" +      
  181.         URLEncoder.encode( cancelURL ));
  182.        
  183.     nvpstr=nvpstr.concat( "&PAYMENTREQUEST_0_SHIPTONAME=" + shipToName + "&PAYMENTREQUEST_0_SHIPTOSTREET=" + shipToStreet + "&PAYMENTREQUEST_0_SHIPTOSTREET2=" + shipToStreet2);
  184.     nvpstr=nvpstr.concat("&PAYMENTREQUEST_0_SHIPTOCITY=" + shipToCity + "&PAYMENTREQUEST_0_SHIPTOSTATE=" + shipToState + "&PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE=" + shipToCountryCode);
  185.     nvpstr=nvpstr.concat("&PAYMENTREQUEST_0_SHIPTOZIP=" + shipToZip + "&PAYMENTREQUEST_0_SHIPTOPHONENUM" + phoneNum);
  186.  
  187.     /*
  188.     Make the call to PayPal to set the Express Checkout token
  189.     If the API call succeded, then redirect the buyer to PayPal
  190.     to begin to authorize payment.  If an error occured, show the
  191.     resulting errors
  192.     */
  193.  
  194.     HashMap nvp = httpcall("SetExpressCheckout", nvpstr);
  195.     String strAck = nvp.get("ACK").toString();
  196.     if(strAck !=null && !(strAck.equalsIgnoreCase("Success") || strAck.equalsIgnoreCase("SuccessWithWarning")))
  197.     {
  198.         return nvp;
  199.     }
  200.  
  201.         return null;
  202. }
  203.  
  204.  
  205. /*********************************************************************************
  206.   * GetShippingDetails: Function to perform the GetExpressCheckoutDetails API call
  207.   *
  208.   * Inputs:  None
  209.   *
  210.   * Output: Returns a HashMap object containing the response from the server.
  211. *********************************************************************************/
  212. public HashMap GetShippingDetails( String token)
  213. {
  214.     /*
  215.     Build a second API request to PayPal, using the token as the
  216.     ID to get the details on the payment authorization
  217.     */
  218.  
  219.     String nvpstr= "&TOKEN=" + token;
  220.  
  221.    /*
  222.     Make the API call and store the results in an array.  If the
  223.     call was a success, show the authorization details, and provide
  224.     an action to complete the payment.  If failed, show the error
  225.     */
  226.  
  227.     HashMap nvp = httpcall("GetExpressCheckoutDetails", nvpstr);
  228.     String strAck = nvp.get("ACK").toString();
  229.     if(strAck !=null && !(strAck.equalsIgnoreCase("Success") || strAck.equalsIgnoreCase("SuccessWithWarning")))
  230.     {
  231.         return nvp;
  232.     }
  233.     return null;
  234. }
  235.  
  236. /*********************************************************************************
  237.   * GetShippingDetails: Function to perform the DoExpressCheckoutPayment API call
  238.   *
  239.   * Inputs:  None
  240.   *
  241.   * Output: Returns a HashMap object containing the response from the server.
  242. *********************************************************************************/
  243. public HashMap ConfirmPayment( String token, String payerID, String finalPaymentAmount, String serverName)
  244. {
  245.  
  246.         /*
  247.         '------------------------------------
  248.         ' The currencyCodeType and paymentType
  249.         ' are set to the selections made on the Integration Assistant
  250.         '------------------------------------
  251.         */
  252.         String currencyCodeType = "EUR";
  253.         String paymentType = "Sale";
  254.  
  255.   /*
  256.     '----------------------------------------------------------------------------
  257.     '----       Use the values stored in the session from the previous SetEC call
  258.     '----------------------------------------------------------------------------
  259.     */
  260.     String nvpstr  = "&TOKEN=" + token + "&PAYERID=" + payerID + "&PAYMENTREQUEST_0_PAYMENTACTION=" + paymentType + "&PAYMENTREQUEST_0_AMT=" + finalPaymentAmount;
  261.        
  262.     nvpstr = nvpstr + "&PAYMENTREQUEST_0_CURRENCYCODE=" + currencyCodeType + "&IPADDRESS=" + serverName;
  263.  
  264.  /*
  265.     Make the call to PayPal to finalize payment
  266.     If an error occured, show the resulting errors
  267.   */
  268.     HashMap nvp = httpcall("DoExpressCheckoutPayment", nvpstr);
  269.     String strAck = nvp.get("ACK").toString();
  270.     if(strAck !=null && !(strAck.equalsIgnoreCase("Success") || strAck.equalsIgnoreCase("SuccessWithWarning")))
  271.     {
  272.         return nvp;
  273.     }
  274.     return null;
  275.    
  276. }
  277.  
  278.  
  279. /*********************************************************************************
  280.   * httpcall: Function to perform the API call to PayPal using API signature
  281.   *     @ methodName is name of API  method.
  282.   *     @ nvpStr is nvp string.
  283.   * returns a NVP string containing the response from the server.
  284. *********************************************************************************/
  285. public HashMap httpcall( String methodName, String nvpStr)
  286. {
  287.  
  288.     String version = "2.3";
  289.     String agent = "Mozilla/4.0";
  290.     String respText = "";
  291.     HashMap nvp = null;   //lhuynh not used?
  292.  
  293. //    deformatNVP( nvpStr );
  294.     String encodedData = "METHOD=" + methodName + "&VERSION=" + gv_Version + "&PWD=" + gv_APIPassword + "&USER=" + gv_APIUserName + "&SIGNATURE=" + gv_APISignature + nvpStr + "&BUTTONSOURCE=" + gv_BNCode;
  295.  
  296.     try
  297.     {
  298.         URL postURL = new URL( gv_APIEndpoint );
  299.         HttpURLConnection conn = (HttpURLConnection)postURL.openConnection();
  300.  
  301.         // Set connection parameters. We need to perform input and output,
  302.         // so set both as true.
  303.         conn.setDoInput (true);
  304.         conn.setDoOutput (true);
  305.  
  306.         // Set the content type we are POSTing. We impersonate it as
  307.         // encoded form data
  308.         conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  309.         conn.setRequestProperty( "User-Agent", agent );
  310.  
  311.         //conn.setRequestProperty( "Content-Type", type );
  312.         conn.setRequestProperty( "Content-Length", String.valueOf( encodedData.length()) );
  313.         conn.setRequestMethod("POST");
  314.  
  315.         // get the output stream to POST to.
  316.         DataOutputStream output = new DataOutputStream( conn.getOutputStream());
  317.         output.writeBytes( encodedData );
  318.         output.flush();
  319.         output.close ();
  320.  
  321.         // Read input from the input stream.
  322.         DataInputStream  in = new DataInputStream (conn.getInputStream());
  323.         int rc = conn.getResponseCode();
  324.         if ( rc != -1)
  325.         {
  326.             BufferedReader is = new BufferedReader(new InputStreamReader( conn.getInputStream()));
  327.             String _line = null;
  328.             while(((_line = is.readLine()) !=null))
  329.             {
  330.                 respText = respText + _line;
  331.             }
  332.             nvp = deformatNVP( respText );
  333.         }
  334.         return nvp;
  335.     }
  336.     catch( IOException e )
  337.     {
  338.         // handle the error here
  339.         return null;
  340.     }
  341. }
  342.  
  343. /*********************************************************************************
  344.   * deformatNVP: Function to break the NVP string into a HashMap
  345.   *     pPayLoad is the NVP string.
  346.   * returns a HashMap object containing all the name value pairs of the string.
  347. *********************************************************************************/
  348. public HashMap deformatNVP( String pPayload )
  349. {
  350.     HashMap nvp = new HashMap();
  351.     StringTokenizer stTok = new StringTokenizer( pPayload, "&");
  352.     while (stTok.hasMoreTokens())
  353.     {
  354.         StringTokenizer stInternalTokenizer = new StringTokenizer( stTok.nextToken(), "=");
  355.         if (stInternalTokenizer.countTokens() == 2)
  356.         {
  357.             String key = URLDecoder.decode( stInternalTokenizer.nextToken());
  358.             String value = URLDecoder.decode( stInternalTokenizer.nextToken());
  359.             nvp.put( key.toUpperCase(), value );
  360.         }
  361.     }
  362.     return nvp;
  363. }
  364.  
  365. /*********************************************************************************
  366.   * RedirectURL: Function to redirect the user to the PayPal site
  367.   *     token is the parameter that was returned by PayPal
  368.   * returns a HashMap object containing all the name value pairs of the string.
  369. *********************************************************************************/
  370. public void RedirectURL( HttpServletResponse response, String token )
  371. {
  372.     String payPalURL = PAYPAL_URL + token;
  373.  
  374.     //response.sendRedirect( payPalURL );
  375.     response.setStatus(302);
  376.     response.setHeader( "Location", payPalURL );
  377.     response.setHeader( "Connection", "close" );
  378. }
  379.  
  380. //end class
  381. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top