Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * SendMoneyClient
- *
- * A class which facilitates the interaction with Payza's
- * SendMoney API. SendMoneyClient class allows user to create
- * the data to be sent to the API in the correct format and
- * retrieve the response.
- *
- *
- * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY
- * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
- * LIMITED TO THE IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE.
- *
- * @author Javanian
- * @copyright 2013
- */
- package payza;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- class SendMoneyClient
- {
- //The API's response varialbles
- private String response;
- public String getResponse()
- {
- return response;
- }
- public void setResponse(String value)
- {
- response = value;
- }
- //The server address of the SendMoney API
- private String server;
- public String getServer()
- {
- return server;
- }
- public void setServer(String value)
- {
- server = value;
- }
- //The exact URL of the SendMoney API
- private String url;
- public String getUrl()
- {
- return url;
- }
- public void setUrl(String value)
- {
- url = value;
- }
- //Your payza name which is your username
- private String myUserName;
- public String getMyUserName()
- {
- return myUserName;
- }
- public void setMyUserName(String value)
- {
- myUserName = value;
- }
- //Your API password that is generated from your payza account
- private String apiPassword;
- public String getApiPassword()
- {
- return myUserName;
- }
- public void setApiPassword(String value)
- {
- apiPassword = value;
- }
- //The data that will be sent to the sendMoney API
- private String dataToSend;
- public String getDataToSend()
- {
- return dataToSend;
- }
- public void setDataToSend(String value)
- {
- dataToSend = value;
- }
- //constructor
- public SendMoneyClient(String strUsername, String strPassword)
- {
- myUserName = strUsername;
- apiPassword = strPassword;
- dataToSend = null;
- }
- /*
- * SendMoneyClient::buildPostVariables()
- *
- * Builds an URL encoded post string which contains the variables to be
- * sent to the API in the correct format.
- *
- * @param string $strAmountPaid Payment amount.
- * @param string $strCurrency 3 letter ISO-4217 currency code.
- * @param string $strReceiverEmail Recipient's email address.
- * @param string $strSenderEmail Your secondary email (optional).
- * @param string $strPurchaseType A valid purchase type code.
- * @param string $strNote Note that you would like to send to the recipient.
- * @param string $strTestMode Test mode status.
- *
- * @return string The URL encoded post string
- */
- public String BuildPostVariables(String strAmountPaid, String strCurrency, String strReceiverEmail, String strSenderEmail,
- String strPurchaseType, String strNote, String strTestMode)
- {
- StringBuilder sbDataToSend = new StringBuilder();
- sbDataToSend.append("USER=").append(java.net.URLEncoder.encode(myUserName));
- sbDataToSend.append("&PASSWORD=").append(java.net.URLEncoder.encode(apiPassword));
- sbDataToSend.append("&AMOUNT=").append(java.net.URLEncoder.encode((String)strAmountPaid));
- sbDataToSend.append("&CURRENCY=").append(java.net.URLEncoder.encode((String)strCurrency));
- sbDataToSend.append("&RECEIVEREMAIL=").append(java.net.URLEncoder.encode((String)strReceiverEmail));
- sbDataToSend.append("&SENDEREMAIL=").append(java.net.URLEncoder.encode((String)strSenderEmail));
- sbDataToSend.append("&PURCHASETYPE=").append(java.net.URLEncoder.encode((String)strPurchaseType));
- sbDataToSend.append("&NOTE=").append(java.net.URLEncoder.encode((String)strNote));
- sbDataToSend.append("&TESTMODE=").append(java.net.URLEncoder.encode((String)strTestMode));
- dataToSend = sbDataToSend.toString();
- setDataToSend(sbDataToSend.toString());
- return dataToSend;
- }
- public String PostData()
- {
- String response = null;
- URL url;
- HttpURLConnection urlConn;
- DataOutputStream wr = null;
- try
- {
- // send the post
- url = new URL(getServer().concat(getUrl()));
- urlConn=(HttpURLConnection)url.openConnection();
- urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- urlConn.setDoInput(true);
- urlConn.setDoOutput(true);
- urlConn.setUseCaches(false);
- urlConn.setRequestMethod("POST");
- urlConn.setRequestProperty("Content-Length", Integer.toString(dataToSend.length()));
- urlConn.connect();
- wr = new DataOutputStream(urlConn.getOutputStream());
- wr.writeBytes(dataToSend);
- wr.close();
- // read the response
- BufferedReader sr = new BufferedReader( new InputStreamReader(urlConn.getInputStream()));
- String result = "";
- String strTemp = "";
- boolean flag = true;
- while (flag)
- {
- strTemp = sr.readLine();
- if (strTemp != null)
- {
- result += strTemp;
- }
- else
- {
- flag = false;
- }
- }
- response = result;
- // decode the response string
- response = java.net.URLDecoder.decode(response);
- sr.close();
- }
- catch(MalformedURLException ex)
- {
- System.err.println("Error: " + ex.getMessage());
- }
- catch (Exception ex)
- {
- System.err.println("Error: " + ex.getMessage());
- }
- finally
- {
- if (wr != null)
- { try
- {
- wr.close();
- }
- catch (Exception ex)
- {
- System.err.println("Error: " + ex.getMessage());
- }
- }
- return response;
- }
- }
- }
- class SendMoneyClientTest
- {
- public static void main(String[] args)
- {
- /*
- * test SendMoney API
- */
- objSendMoneyClient.setServer("https://api.payza.com");
- objSendMoneyClient.setUrl("/svc/api.svc/sendmoney");
- String strPostString = objSendMoneyClient.BuildPostVariables("1.11", "USD", "[email protected]", "", "1", "this is a test", "1");
- objSendMoneyClient.setResponse(objSendMoneyClient.PostData());
- System.out.println("Result: " + objSendMoneyClient.getResponse());
- }
- }
Add Comment
Please, Sign In to add comment