Guest User

PAYZA sendMoney API in JAVA

a guest
Feb 2nd, 2013
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.64 KB | None | 0 0
  1. /**
  2.  *
  3.  * SendMoneyClient
  4.  *
  5.  * A class which facilitates the interaction with Payza's
  6.  * SendMoney API. SendMoneyClient class allows user to create
  7.  * the data to be sent to the API in the correct format and
  8.  * retrieve the response.
  9.  *
  10.  *
  11.  * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY
  12.  * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  13.  * LIMITED TO THE IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE.
  14.  *
  15.  * @author Javanian
  16.  * @copyright 2013
  17.  */
  18. package payza;
  19.  
  20. import java.io.BufferedReader;
  21. import java.io.DataOutputStream;
  22. import java.io.InputStreamReader;
  23. import java.net.HttpURLConnection;
  24. import java.net.MalformedURLException;
  25. import java.net.URL;
  26.  
  27. class SendMoneyClient
  28. {
  29.         //The API's response varialbles
  30.     private String response;
  31.  
  32.     public String getResponse()
  33.     {
  34.         return response;
  35.     }
  36.     public void setResponse(String value)
  37.     {  
  38.         response = value;
  39.     }
  40.         //The server address of the SendMoney API
  41.     private String server;
  42.  
  43.     public String getServer()
  44.     {
  45.         return server;
  46.     }
  47.     public void setServer(String value)
  48.     {
  49.         server = value;
  50.     }
  51.         //The exact URL of the SendMoney API
  52.     private String url;
  53.    
  54.     public String getUrl()
  55.     {
  56.         return url;
  57.     }
  58.     public void setUrl(String value)
  59.     {
  60.         url = value;
  61.     }
  62.         //Your payza name which is your username
  63.     private String myUserName;
  64.    
  65.     public String getMyUserName()
  66.     {
  67.         return myUserName;
  68.     }
  69.     public void setMyUserName(String value)
  70.     {
  71.         myUserName = value;
  72.     }
  73.         //Your API password that is generated from your payza account
  74.     private String apiPassword;
  75.    
  76.     public String getApiPassword()
  77.     {
  78.         return myUserName;
  79.     }
  80.     public void setApiPassword(String value)
  81.     {
  82.         apiPassword = value;
  83.     }
  84.         //The data that will be sent to the sendMoney API
  85.         private String dataToSend;
  86.         public String getDataToSend()
  87.     {
  88.         return dataToSend;
  89.     }
  90.     public void setDataToSend(String value)
  91.     {
  92.                 dataToSend = value;
  93.     }
  94.         //constructor
  95.         public SendMoneyClient(String strUsername, String strPassword)
  96.         {
  97.              myUserName = strUsername;
  98.             apiPassword = strPassword;
  99.             dataToSend =    null;          
  100.         }
  101.        
  102.         /*
  103.          * SendMoneyClient::buildPostVariables()
  104.          *
  105.          * Builds an URL encoded post string which contains the variables to be
  106.          * sent to the API in the correct format.
  107.          *
  108.          * @param string $strAmountPaid Payment amount.
  109.          * @param string $strCurrency 3 letter ISO-4217 currency code.
  110.          * @param string $strReceiverEmail  Recipient's email address.
  111.          * @param string $strSenderEmail Your secondary email (optional).
  112.          * @param string $strPurchaseType A valid purchase type code.
  113.          * @param string $strNote Note that you would like to send to the recipient.
  114.          * @param string $strTestMode Test mode status.
  115.          *
  116.          * @return string The URL encoded post string
  117.          */
  118.          public String BuildPostVariables(String strAmountPaid, String strCurrency, String strReceiverEmail, String strSenderEmail,
  119.             String strPurchaseType, String strNote, String strTestMode)
  120.          {
  121.              StringBuilder sbDataToSend = new StringBuilder();
  122.                          
  123.             sbDataToSend.append("USER=").append(java.net.URLEncoder.encode(myUserName));
  124.             sbDataToSend.append("&PASSWORD=").append(java.net.URLEncoder.encode(apiPassword));
  125.             sbDataToSend.append("&AMOUNT=").append(java.net.URLEncoder.encode((String)strAmountPaid));
  126.             sbDataToSend.append("&CURRENCY=").append(java.net.URLEncoder.encode((String)strCurrency));
  127.             sbDataToSend.append("&RECEIVEREMAIL=").append(java.net.URLEncoder.encode((String)strReceiverEmail));
  128.             sbDataToSend.append("&SENDEREMAIL=").append(java.net.URLEncoder.encode((String)strSenderEmail));
  129.             sbDataToSend.append("&PURCHASETYPE=").append(java.net.URLEncoder.encode((String)strPurchaseType));
  130.             sbDataToSend.append("&NOTE=").append(java.net.URLEncoder.encode((String)strNote));
  131.             sbDataToSend.append("&TESTMODE=").append(java.net.URLEncoder.encode((String)strTestMode));
  132.             dataToSend = sbDataToSend.toString();
  133.             setDataToSend(sbDataToSend.toString());
  134.             return dataToSend;
  135.          }
  136.          public String PostData()
  137.          {
  138.                 String response = null;
  139.                 URL url;
  140.                 HttpURLConnection urlConn;
  141.                 DataOutputStream wr = null;
  142.                 try
  143.                 {
  144.                     // send the post
  145.                     url = new URL(getServer().concat(getUrl()));
  146.                     urlConn=(HttpURLConnection)url.openConnection();
  147.                     urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  148.                     urlConn.setDoInput(true);
  149.                     urlConn.setDoOutput(true);
  150.                     urlConn.setUseCaches(false);
  151.                     urlConn.setRequestMethod("POST");
  152.                     urlConn.setRequestProperty("Content-Length", Integer.toString(dataToSend.length()));
  153.                     urlConn.connect();
  154.  
  155.                     wr = new DataOutputStream(urlConn.getOutputStream());
  156.                     wr.writeBytes(dataToSend);
  157.                     wr.close();
  158.  
  159.  
  160.                     // read the response                
  161.                     BufferedReader sr = new BufferedReader( new InputStreamReader(urlConn.getInputStream()));
  162.  
  163.                     String result = "";
  164.                     String strTemp = "";
  165.                     boolean flag = true;
  166.                     while (flag)
  167.                     {
  168.                         strTemp = sr.readLine();
  169.                         if (strTemp != null)
  170.                         {
  171.                             result += strTemp;
  172.                         }
  173.                         else
  174.                         {
  175.                             flag = false;
  176.                         }
  177.                     }
  178.  
  179.                     response = result;
  180.                     // decode the response string
  181.                     response = java.net.URLDecoder.decode(response);
  182.                     sr.close();
  183.             }
  184.             catch(MalformedURLException ex)
  185.             {
  186.                 System.err.println("Error: " + ex.getMessage());
  187.             }
  188.             catch (Exception ex)
  189.             {
  190.                 System.err.println("Error: " + ex.getMessage());
  191.             }
  192.             finally
  193.             {
  194.                 if (wr != null)
  195.                 {   try
  196.                     {
  197.                         wr.close();
  198.                     }
  199.                     catch (Exception ex)
  200.                     {
  201.                         System.err.println("Error: " + ex.getMessage());
  202.                     }
  203.                 }
  204.  
  205.             return response;
  206.         }
  207.  
  208.     }
  209. }
  210.     class SendMoneyClientTest
  211.     {
  212.         public static void main(String[] args)
  213.         {
  214.             /*
  215.              * test SendMoney API
  216.              */
  217.             SendMoneyClient objSendMoneyClient = new SendMoneyClient("[email protected]", "XXXXXXXX");
  218.             objSendMoneyClient.setServer("https://api.payza.com");
  219.             objSendMoneyClient.setUrl("/svc/api.svc/sendmoney");
  220.             String strPostString = objSendMoneyClient.BuildPostVariables("1.11", "USD", "[email protected]", "", "1", "this is a test", "1");
  221.             objSendMoneyClient.setResponse(objSendMoneyClient.PostData());
  222.             System.out.println("Result: " + objSendMoneyClient.getResponse());
  223.         }
  224.     }
Add Comment
Please, Sign In to add comment