import org.apache.xerces.parsers.*;
import java.io.* ;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class Findfailure extends DefaultHandler {
static String respon="<pp.response><pp.authresponse merchant=\"1001\" ordernumber=\"245\" transactionid=\"CC_FU2E7D4A9B3F\" authcode=\"OK7315\" avs=\"YYY\" cvv2result=\"X\" authstatus=\"AUTH\" /></pp.response> ";
/*static String respon="<pp.response><pp.authresponse merchant=\"1001\"
ordernumber=\"6\" failure=\"true\">General failure: com.purepayments.biz.InvalidCCNumberException:
Invalid Credit Card Number (Check digit is invalid): 4012000033330025</pp.authresponse></pp.response>";
*/
//static String remote_DTD="<!DOCTYPE pp.response PUBLIC \"-//IMALL//DTD PUREPAYMENTS 1.0//EN\" \"http://www.purepayments.com/dtd/purepayments.dtd\">";
// static String respon="<pp.response><pp.authresponse merchant=\"1001\" ordernumber=\"14\" authstatus=\"DCL\" transactionid=\"CC_FU2AC951D180\" failure=\"true\">Card declined</pp.authresponse><pp.adjustresponse merchant=\"1001\" ordernumber=\"14\" transactionid=\"CC_FU2AC951D17F\"/></pp.response>";
//Needed for testing with main()
//static String local_DTD="<?xml version=\"1.0\" standalone=\"no\" ?><!DOCTYPE pp.response SYSTEM \"purepayments.dtd\">";
InputSource xmlurl=null;
String element_data=" ";
boolean found_failure=false;
boolean found_cvv=false;
boolean found_avs=false;
String xmldoc=null;
String [ ] results=new String[3];
public Findfailure(String xmldoc) {
this.xmldoc =xmldoc.substring(xmldoc.indexOf("<pp.response>"));
}
public static void main(String[] args){
System.out.println( new Findfailure(respon).find());
}
public String[] find() {
try {
SAXParser saxParser = new SAXParser();
saxParser.setContentHandler(this);
// xmlurl=new InputSource(new URL(xmldoc).openStream());
// System.out.println(xmldoc);
xmlurl=new InputSource(new ByteArrayInputStream(xmldoc.getBytes ()));
saxParser.parse(xmlurl);
} catch (SAXException ex) {
// if (found_cvv)return "Incorrect CVV code from back of CreditCard";
// if (found_avs)return "Credit Card does not match Address";
if (found_failure) {
if (element_data.indexOf("InvalidCCNumberException")>0) {
results[0]="Invalid Credit Card Number";
return results;
}
if (element_data.indexOf("pp.address requires fullname")>0) {
results[0]="Please be sure the name matches that on the Credit Card";
return results;
}
if (element_data.indexOf("Cannot authorize, expire date")>0) {
results[0]="This Credit Card Has Expired";
return results;
}
int i=element_data.indexOf("Exception:");
if (i<0) {
results[0]=element_data;
return results ;
}
results[0]=element_data.substring(i+10,element_data.length());
return results ;
} else {
results[0]="OK";
return results ;
}
} catch (Exception e) {
e.printStackTrace ();
// Catch any other exceptions that may occur //
results[0]="Error";
return results ;
}
results[0]="OK";
return results ;
}
/*
public void processingInstruction(String target, String data)
throws SAXException {
int href=data.indexOf ("href=");
int end_href=data.indexOf ("\"",href+6);
xsldoc=data.substring (href+6,end_href);
throw new SAXException(xsldoc); }
*/
public void characters(char[]ch,int start,int length)throws SAXException {
element_data=new String(ch,start,length );
throw new SAXException("founderrortext") ; //foundit is not used. Just an OOP artifact.
}
public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException{
if (localName.equals ("pp.authresponse" )) {
int jauth= atts.getLength () ;
for (int i=0;i<jauth;i++) {
if (atts.getLocalName (i).equals("failure")&&atts.getValue (i).equals ("true")){
found_failure=true;
break;
}
if (atts.getLocalName (i).equals("avs")) {
results[1]=atts.getValue (i);
found_avs=true;
}
if (atts.getLocalName (i).equals("cvv2result")) {
results[2]=atts.getValue (i);
found_cvv=true;
throw new SAXException("found_stuff") ;
}
}
}
}
public void fatalError(SAXParseException exception)
throws SAXException {
System.out.println("**Parsing Fatal Error**\n" +
" Line: " +
exception.getLineNumber() + "\n" +
" URI: " +
exception.getSystemId() + "\n" +
" Message: " +
exception.getMessage());
throw new SAXException("Fatal Error encountered");
}
}