Assuming the PayPal data is validated, a fax and an email are sent. If the data isn't validated, just an email is sent (helpful for immediate notification of a potential hacker). The email processing is handled using the standard JavaMail API (http://java.sun.com/products/javamail/).
|
An example email, formatted as HTML is shown in Figure 6-6.
It's easy to imagine adding additional logic to the PayPalReciept.toHTMLString( ) method for a more appealing and easier to understand design, but it contains all of the needed information.
The fax sending is encapsulated in a simple Java class, as shown in Example 6-4.
package com.cascadetg.ch06; import cc.interfax.www.*; import java.net.URL; import org.apache.axis.client.*; import javax.xml.namespace.QName; /** * http://www.interfax.net/en/dev.html * * C:\devenv\axis-1_1\lib>java -classpath * commons-logging.jar;log4j-1.2.8.jar;wsdl4j.jar;axis.jar; commons-discovery.jar;jax * rpc.jar;saaj.jar org.apache.axis.wsdl.WSDL2Java * http://ws.interfax.net/dfs.asmx?wsdl */ public class FaxSender { public static final String TEXT = "TXT"; public static final String HTML = "HTM"; String text; String textType = TEXT; long result; /** * This is the main method used to send a Fax. You'll want to set * the text to be sent and the text type before calling this. * * @return true if successful, false if not. */ public boolean sendFax( ) { try { // Create an instance of the Web Service Object InterFaxSoapStub ifs = new InterFaxSoapStub( new URL("http://ws.interfax.net/DFS.asmx"), new Service(new QName("SendCharFax"))); // Invoke the SendCharFax method result = ifs.sendCharFax( FaxSenderTokens.faxUsername, FaxSenderTokens.faxPassword, FaxSenderTokens.faxTestFaxNumber, text, textType); if (result > 0) { // Positive returned value indicates that the fax was // sent successfully. // The return value is the Transaction ID. System.out.println( "Fax submitted properly. Transaction number: " + result); return true; } else { // Negative returned value indicates sending failure. // See error code definitions System.out.println( "Error sending fax! Error code " + result); return false; } } catch (Exception e) { e.printStackTrace( ); return false; } } public String getText( ) { return text; } public void setText(String text) { this.text = text; } public String getTextType( ) { return textType; } public void setTextType(String textType) { this.textType = textType; } public String getResult( ) { return Long.toString(result); } public static void main(String[] args) { FaxSender test = new FaxSender( ); test.setTextType(TEXT); test.setText("This is a test."); System.out.println(test.sendFax( )); System.out.println("Done!"); } }
The code shown in Example 6-4 uses a standard Apache Axis SOAP service. To use this code, generate the bindings using the Apache Axis tools (as originally described in Chapter 3). The command that generates the InterFAX code is shown in Example 6-5.
C:\devenv\axis-1_1\lib>java -classpath commons-logging.jar; log4j-1.2.8.jar;wsdl4j.jar;axis.jar;commons-discovery.jar;jaxrpc.jar;saaj. jar org.apache.axis.wsdl.WSDL2Java
http://ws.interfax.net/dfs.asmx?wsdl
Arguably, it's harder to generate the HTML to send as the content of the fax than it actually is to make the call into the fax service and send the fax. As shown in Figure 6-7, you can see that the same HTML formatting used for the email is also used for the fax.
InterFAX does, of course, charge for faxes sent through their system. The nice thing is that you never need to worry about setting up or maintaining a fax system yourself. While it's possible to envision using the standard Java printing APIs to generate faxes that are sent via a local fax modem, this can rapidly become very complicated and expensive. It certainly takes more time to develop and maintain, plus you'll pay telephone charges.
|
The secure information (such as passwords and account names) has been broken into a separate class. The first, for PayPal, is shown in Example 6-6, and the second, for the InterFAX service, is shown in Example 6-7.
package com.cascadetg.ch06; public class PayPalTokens { // Put your PayPal registered email address here. You want to // make sure that payments are sent to the correct address. static final String paypalEmail = "test_account@cascadetg.com"; // Mail configuration static final String mailhost = "smtp.mail.myisp.com"; static final String mailhost_username = "mail_username"; static final String mailhost_password = "mail_password"; }
The code assumes that you must provide authentication to use the target SMTP server (typical for most ISPs today).
package com.cascadetg.ch06; public class FaxSenderTokens { public static String faxUsername = "fax_account"; public static String faxPassword = "fax_password"; public static String faxDevPartnerID = "12345678; public static String faxTestFaxNumber = "0014155551234"; }
Notice that the U.S. phone number is shown with a three-digit country code (001) prefixing the 415 (San Francisco area) phone number. Developers in non-U.S. countries are likely used to this method for specifying full phone numbers. If you're not, consult your local phone book for information on country codes and international dialing rules.
You've now used two different web services to provide for an automated sales system and managed to add both fax and email notification capabilities. It's easy to imagine adding even more capabilities as the support organization grows.