XML-RPC

[XML-PRC index]
-> Apache & Java

Using the XML-RPC API With Apache and Java 

Below is an example of how to use the SECVPN XML-RPC API with the org.apache.xmlrpc packages to 
place a test transaction on the SECPay server.

In order to run the code below you will need:

This example can easily be modified to take other method names from the command line which represent a method to call on the SECVPN object.
For each new method call which you would like to implement - a new execute<MethodName>() method can be added which provides the parameters appropriate for the method you are trying to call.

To use the example below as is would require the following command:

Simple XMLRPC Client Example

package com.mycompany.xmlrpc;

import java.io.*;
import java.net.*;
import java.util.*;

import org.apache.xmlrpc;

/**
  * Simple class with main method to execute a method of the SECVPN object.
  */
 public class XMLRPCTestClient {

    // The host address for SECVPN
     public static final String SECVPN_HOST_ADDRESS = "https://www.secpay.com/secxmlrpc/make_call";


    /**
      * Test method to call the specified method of the SECVPN object.
      */
     public static void main( String[] args )
     {
         if ( args.length < 1 )
         {
             exit( "Usage: java com.mycompany.xmlrpc.XMLRPCTestClient [methodtocall]"; );
         }

        String methodToCall = args[0];

        try
         {
             // Use the xerces driver
             XmlRpc.setDriver( "org.apache.xerces.parsers.SAXParser" );

            // Specifiy the server
             XmlRpcClient client = new XmlRpcClient( SECVPN_HOST_ADDRESS );
             System.out.println("Connected to host...");

            String  xmlRpcResponse = null;
            if ( methodToCall.equals( "validateCardFull" ) )
             {
                 System.out.println( "Executing SECVPN.validateCardFull()..." );
                 xmlRpcResponse = executeValidateCardFull( client ); // See method below.
             }


            // Add checks for other method calls you wih to implement here...

            System.out.println( "Response from SECVPN server: " + xmlRpcResponse );
         }
         catch( ClassNotFoundException cnfe )
         {
              System.out.println( "ErrorUnable to find SAXParser" );
         }
         catch( XmlRpcException xe )
         {
              System.out.println( "XmlRpcException: " + xe.getMessage() );
         }
         catch( IOException ioe )
         {
              System.out.println( "IOException: " + ioe.getMessage() );
         }
     }

    /**
      * Method for populating and executing the validateCardFull method of the SECVPN object via XML RPC.
      * Other methods like this one can be added to handle other methods of the SECVPN object.
      *
      * @param client The XmlRpcClient constructed using SECVPN host address
      * @return The full parseable return string from the call.
      */
       private static String executeValidateCardFull( XmlRpcClient client )
     throws XmlRpcException, IOException
     {
         // Populate the Vector with test parameters in the order specified
         // by the SECVPN.validateCardFull() method
         Vector params = new Vector();
         params.addElement( "secpay" );                          // test merchant id
         params.addElement( "secpay" );                          // vpn password
         params.addElement( "xmltest" );                         // merchants transaction id
         params.addElement( "123.4.56.789" );                    // the ip of the original caller
         params.addElement( "John Doe" );                        // the customer name
         params.addElement( "4444333322221111" );                // credit card no
         params.addElement( "49.99" );                           // amount
         params.addElement( "0105" );                            // expiry date
         params.addElement( "" );                                // issue no (switch/solo only)
         params.addElement( "0102" );                            // start date
         params.addElement( "prod=funny_book,amount=18.50;prod=sad_book,amount=16.50x3" ); // order item string
         // shipping address
         params.addElement(
                "name=CONTACT,company=COMPANY,addr_1=ADDRESSLINE1," +                                                                                          "addr_2=ADDRESSLINE2,city=CITY,state=COUNTY,country=COUNTRY," +
                "post_code=POST_CODE,tel=TELEPHONE,email=EMAIL,url=URL"
         );
         // billing address
         params.addElement(
                "name=CONTACT,company=COMPANY,addr_1=ADDRESSLINE1," +
                "addr_2=ADDRESSLINE2,city=CITY,state=COUNTY,country=COUNTRY," +
                "post_code=POST_CODE,tel=TELEPHONE,email=EMAIL,url=URL"
         );
         params.addElement( "test_status=true,dups=false" );     // options string


         // Once the Vector of test data is fully populated we can pass it to
         // the XmlRpcClient we initialised with the SECVPN host address
         // and call its execute() method.
         return ( String )client.execute( "SECVPN.validateCardFull", params );
    }

}

Raw XMLRPC that was submitted in above example:

<?xml version="1.0"?>
<methodCall>
<methodName>SECVPN.validateCardFull</methodName>
<params>
<param>
<value><string>secpay</string></value>
</param>
<param>
<value><string>secpay</string></value>
</param>
<param>
<value><string>xmltest</string></value>
</param>
<param>
<value><string>123.4.56.789</string></value>
</param>
<param>
<value><string>John Doe</string></value>
</param>
<param>
<value><string>4444333322221111</string></value>
</param>
<param>
<value><string>49.99</string></value>
</param>
<param>
<value><string>01/05</string></value>
</param>
<param>
<value><string></string></value>
</param>
<param>
<value><string>01/02</string></value>
</param>
<param>
<value><string>prod=funny_book,item_amount=18.50;prod=sad_book,item_amount=16.50x3</string></value>
</param>
<param>
<value><string>name=CONTACT,company=COMPANY,addr_1=ADDRESSLINE1,post_code=POST_CODE,tel=TELEPHONE,email=EMAIL,url=URL</string></value>
</param>
<param>
<value><string>name=CONTACT,company=COMPANY,addr_1=ADDRESSLINE1,addr_2=ADDRESSLINE2,city=CITY,state=COUNTY,country=COUNTRY,post_code=POST_CODE,tel=TELEPHONE,email=EMAIL,url=URL</string></value>
</param>
<param>
<value><string>test_status=true</string></value>
</param>
</params>
</methodCall>

Response received from above example in raw XMLRPC mark-up:

<?xml version="1.0" encoding="ISO-8859-1"?>
<methodResponse>
<params>
<param>
<value>?valid=true&trans_id=xmltest&code=A&auth_code=9999&amount=49.99&test_status=true</value>
</param>
</params>
</methodResponse>

After parsing to remove tags:

valid=true&trans_id=xmltest&code=A&auth_code=9999&amount=49.99&test_status=true 

Further Information

Details of the XML-RPC protocol can be found at www.xmlrpc.org.

(c)Copyright 2002 SECPay Ltd., All Rights Reserved.