Prototype Java ME payment API using HTTP
Article Metadata
Payment APIs allows Java ME applications to request the device to charge for some content, like a new game level, an information, or anything else. You can pay per application, per use (f.e. per day) or per functionality. No Nokia devices (at time of writing) support the Java ME Payment API (JSR 229). This article shows an example of a payment API made using HTTP.
This API isn't fixed for the technology behind the payment. The payment can be made by SMS, Credit Card, or phone bill. The operator of the user can configure what payment options the user will have available to pay.
MIDlet prototype example
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
public class SslTest extends MIDlet implements CommandListener {
private final String merchant="4181607";
private final String amount="19095";
private final String currency="208";
private final String orderid ="991002b";
private final String accepturl ="https://payment.architrade.com/cgissl/relay.cgi/http://www.java4mobile/dibs/godkendt.jsp";
private final String declineurl ="https://payment.architrade.com/cgissl/relay.cgi/http://www.java4mobile/dibs/afvist.jsp";
private final String test="foo";
private String cardno;
private String expmon;
private String expyear;
private String cvc;
private String url = "https://payment.architrade.com/cgi-ssl/auth.cgi";
private String post;
private String urlTotal;
private Command exitCommand = new Command("Exit", Command.EXIT, 2);
private Command getCommand = new Command("Pay", Command.SCREEN, 1);
private Form form;
private TextField txtCardno = new TextField("Card no:", null , 16, TextField.NUMERIC);
private TextField txtExpmon = new TextField("Expmon", null, 2, TextField.NUMERIC);
private TextField txtExpyear = new TextField("Expyear", null , 2, TextField.NUMERIC);
private TextField txtCvc = new TextField("Cvc", null, 3, TextField.NUMERIC);
private Display display;
public SslTest() { }
public void startApp() {
if (display == null)
display = Display.getDisplay(this);
form = new Form("Payment");
form.append(txtCardno);
form.append(txtExpmon);
form.append(txtExpyear);
form.append(txtCvc);
form.addCommand(exitCommand);
form.addCommand(getCommand);
form.setCommandListener(this);
display.setCurrent(form);
}
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
notifyDestroyed();
}
else if (c == getCommand) {
cardno=txtCardno.getString();
expmon=txtExpmon.getString();
expyear=txtExpyear.getString();
cvc=txtCvc.getString();
post = "?merchant="+merchant+"&amount="+amount+"¤cy="+currency+"&orderid="+orderid+ "&accepturl="+accepturl+"&declineurl="+declineurl+"&cardno="+cardno+"&expmon="+expmon+ "&expyear="+expyear+"&cvc="+cvc+"&test="+test;
StringBuffer b = new StringBuffer();
HttpsConnection con = null;
InputStream is = null;
OutputStream os = null;
urlTotal = url+post;
try {
int len = 0;
int ch = 0;
con = (HttpsConnection)Connector.open(urlTotal);
con.setRequestMethod(HttpsConnection.POST);
/*
byte[] data = post.getBytes();
con.setRequestProperty("Content-Length",
Integer.toString(data.length));
os = con.openOutputStream();
os.write( data );
os.close();
*/
System.out.println(con.getResponseCode());
is = con.openInputStream();
len = (int) con.getLength();
if (len != -1) {
for(int i=0; i<len; i++) {
if((ch = is.read()) != -1) {
b.append((char) ch);
}
}
}
else {
while((ch = is.read()) != -1) {
len = is.available();
b.append((char) ch);
}
}
System.out.println("Response: " +b);
Alert a = new Alert("Trans results:", b.toString(), null, null);
a.setTimeout(Alert.FOREVER);
display.setCurrent(a);
}
catch (Exception e) {
e.printStackTrace();
String s = e.toString();
If(s != null) {
Alert aa = new Alert("Error in connection:", s, null, null);
aa.setTimeout(Alert.FOREVER);
display.setCurrent(aa);
}
}
finally {
if (is != null) {
try {
is.close();
}
catch (Exception ce) { }
}
if (c != null) {
try {
con.close();
}
catch (Exception ce) { }
}
}
}
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) { }
}
More info: Analysis of J2ME for developing Mobile Payment Systems -> www.microjava.com/articles/techtalk/mpayment
Oficial Site -> http://jcp.org/en/jsr/detail?id=229


18 Sep
2009
This article implements a complete Payment API using simple and easy to understand code. Must try this code.If developers want their application to process payment they can try this code. Its a bug free code and runs without any exceptions.
08 Jan
2010
In this article's example code where is the use of payment API jsr-229. this article is just about payment using http get nothing else.