Introduction
This tutorial shows how to use the playground web service interface to charge a subscriber from a java client. The tutorial is based on the send SMS Parlay X web service, and illustrates how to create a java client invoking a web service, based on WSDL files. Please note that the Charging interfaces in the playground LAB are implemented towards a billing simulator, and not a live billing system. In order to charge a subscriber, the phone number of the subscriber must first be added as a billable account in the billing simulator (this is handled in the playground partnering process). Furthermore, you should be aware that there are different ways of utilizing the charge subscriber interfaces. The one implemented in this tutorial is based on the charge / refund billing pattern (which does not require server side session management) and the Amount charging web services. As a prerequisite we assume that you are familiar with the following technologies:
- HTML
- XML, SOAP and Web Services
- Eclipse - as an environment for Java and web service development
- Java, inclusive basic HTTP/URL handling
- SMS messaging
Follow the steps below to create a client which invokes the Amount Charging web service.
Place the cursor over this icon to expand screenshots from Eclipse.
- Download and install the Eclipse IDE (Web Tools Platform).
- Create a new project by selecting File | New | Java Project from the menu bar. This initiates the "New Java Project" wizard.
- Give the project a name, and choose your preferences on Java version and project organization. In this tutorial, we named the project "Charge Subscriber". The default values are used for all other selections.
- Push "Finish" to continue and close the wizard. The project is created, and the next steps describe how to start adding the required content to your project.
- Next, download and store locally the file, playground.jar
- Add the library to your project. To do this, use the right mouse button on your project, and select "Properties", then click "Java Build Path" and "Add External JAR's" button, to select the JAR files. Click the OK button, when you are done.
- In addition to playground.jar you should add the following libraries to your project:
- activation.jar
- mail.jar
- axis.jar - the axis binary download also includes the following required libraries:
- commons-discovery-0.2.jar
- commons-logging-1.0.4.jar
- wsdl4j-1.5.1.jar
- jaxrpc.jar
- saaj.jar
- Add a new Java class to the project. Right click your project, and select "New -> Class" from the menu, to start the "New Java Class" wizard. Name the Java class, in this example "MyAmountChargingDemo" and check for automatically generating the main method. Click "Finish" and the java class is generated.
- Finally, cut & paste from the source code below into your java class file
import java.math.BigDecimal;
import java.net.URL;
import com.telenor.playground.toolbox.session.SessionManagerService;
import com.telenor.playground.toolbox.session.SessionManagerServiceLocator;
import com.telenor.playground.toolbox.session.SessionManager_PortType;
import com.telenor.playground.toolbox.amount_charging.AmountChargingService;
import com.telenor.playground.toolbox.amount_charging.AmountChargingServiceLocator;
import com.telenor.playground.toolbox.amount_charging.AmountCharging_PortType;
import com.telenor.playground.toolbox.amount_charging.ChargingInformation;
import com.telenor.playground.toolbox.amount_charging.PolicyException;
import com.telenor.playground.toolbox.amount_charging.ServiceException;
public class MyAmountChargingDemo {
MyAmountChargingDemo(String uName, String pWord){
try {
/**
* Get the session manager
*/
SessionManagerService sessionManagerService =
new SessionManagerServiceLocator();
URL sessionManagerAddress =
new URL(sessionManagerService.getSessionManagerAddress());
SessionManager_PortType sessionManager =
sessionManagerService.getSessionManager(sessionManagerAddress);
/**
* Call setSecurity to create a security
* soap header. uName and pWord is provided
* by playground in introduction to lab email
*/
sessionManager.setSecurity(uName, pWord);
/**
* Get a session ID from WLNG
*/
String sessionId = sessionManager.getSession();
System.out.println("sessionId: " + sessionId);
/**
* Get the Payment service
*/
AmountChargingService amountChargingService =
new AmountChargingServiceLocator();
URL amountChargingAddress =
new URL(amountChargingService.getAmountChargingAddress());
AmountCharging_PortType ac_port =
amountChargingService.getAmountCharging(amountChargingAddress);
/**
* Create a security soap header. (Set the security)
*/
ac_port.setSecurity(sessionId, uName, pWord);
/**
* Create a URI from the phone number of the subscriber
* to be charged. In the playgroundLAB this phone number
* must be registered in the billing simulator before
* it can be charged
*/
org.apache.axis.types.URI address =
new org.apache.axis.types.URI("tel:[CCDDDDDDDD]"); // C=Country code / D=Digit
/**
* Create the actual charging object. You must add the
* amount to be charged as a BigDecimal. Currency, Code
* and description are all of type String.
*/
ChargingInformation ci = new ChargingInformation();
BigDecimal amount = new BigDecimal(5.0);
ci.setAmount(amount);
ci.setCurrency("NOK");
ci.setCode("XXX");
ci.setDescription("WEB API");
/**
* Charge the amount.
*/
ac_port.chargeAmount(address, ci, "Take");
System.out.println(address.toString() + " charg: " + amount);
/**
* Refund the amount.
*/
ac_port.refundAmount(address, ci, "Return");
System.out.println(address.toString() + " refund: " + amount);
}
/**
* Catch policy exceptions and print information about the exception
*/
catch (PolicyException e){
System.out.println("Got an PolicyException.");
System.out.println("e.getText(): " + e.getText());
System.out.println("e.getClass(): " + e.getClass());
e.printStackTrace();
return;
}
/**
* Catch service exceptions and print information about the exception
*/
catch (ServiceException e){
System.out.println("Got an ServiceException.");
System.out.println("e.getMessage(): " + e.getMessage());
System.out.println("e.getLocalizedMessage(): " + e.getLocalizedMessage());
System.out.println("e.getClass(): " + e.getClass());
e.printStackTrace();
return;
}
/**
* Catch any other exception and print information about it.
*/
catch (Exception e) {
System.out.println("Got an Exception.");
System.out.println("e.getMessage(): " + e.getMessage());
System.out.println("e.getLocalizedMessage(): " + e.getLocalizedMessage());
System.out.println("e.getClass(): " + e.getClass());
e.printStackTrace();
return;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/**
* Use your main method to initiate an instance of the class
* and perform the logic placed in the constructor.
*
* Constructor arguments (username and password) are received
* in your welcome to the lab email
* (username = ApplicationInstanceGroupId /
* password = ApplicationInstanceGroupPassword)
*
*/
String username = [username];
String password = [password];
new MyAmountChargingDemo(username, password);
}
}
|


