SDN Featured Content

Do you Think Hibernate is better technology that EJB

Java/J2EE blogs

Welcome to my java /J2EE blogs.


Friday, December 18, 2009

Payware API payment example from java language

Payware API is one of the easy way to make payment online using no API setup.
It has made a simple java example to post the valid XML as an request to the Payware API and in return we get the response back.

We must have the demo account setup with the payware before we try this example else it give the error with invalid login details.

It's very good API that support the multiple plateform(visual basic,java .net) to make the payment online.



package com.payware.classes;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.payware.util.Utilities;
import com.payware.util.PaywareConstants;
public class PaywarePayment{
/**
* logger instance
*/
protected Log log = LogFactory.getLog(getClass());

public String processPayment(
) throws Exception {
log.info("PaywarePayment is called");
StringBuffer responseData=new StringBuffer();
try {

String requestXml =
""+
"100010001"+
"APIUser"+
"APIPassword"+
"123456789"+
"PAYMENT"+
"SALE"+
"CREDIT"+
"4005550000000019"+
"3"+
"4005550000000019=07121011000012345678"+
"12"+
"07"+
"1.00"+
"12345678901234567"+
"12345678901234567"+
"0.16"+
"123456"+
"
";
String httpsURL = "https://APIDemo.IPCharge.net/IPCHAPI/RH.aspx";
URL myurl = new URL(httpsURL);

HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "text/xml; charset=\"utf-8\"");
con.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream(),
"UTF8");
out.write(requestXml);
out.close();

InputStream ins = con.getInputStream();
InputStreamReader isr=new InputStreamReader(ins);
BufferedReader in =new BufferedReader(isr);

String inputLine="";
responseData.append("\n");
while ((inputLine = in.readLine()) != null){
if(!inputLine.trim().equalsIgnoreCase(".") && !inputLine.trim().equalsIgnoreCase("")){
System.out.println("Input Line:::"+inputLine);
String key=inputLine.substring(0,inputLine.indexOf(" "));;
String value=inputLine.substring(key.length(),inputLine.length());
responseData.append("<"+key+">"+value+""+"\n");
}
}
responseData.append("
");

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

}

return responseData.toString();
}

public static void main(String args[]){
PaywarePayment ppgtw=new PaywarePayment();
String response="";
try
{
response = ppgtw.processPayment();
System.out.println(response);
String currentDate = Utilities.now("yyyyMMdd");
String file_name = "PFReport" + currentDate + ".xml";
FileWriter WriteResult = new FileWriter("c:" +
"\\reports\\" +
file_name);
PrintWriter ResultOutput = new
PrintWriter(WriteResult, true);
ResultOutput.println(response);



} catch (Exception e)
{
System.out.println("Error in the main class:"+e.getMessage());
e.printStackTrace();
}

}
}

No comments: