6.3 Responding to the Transaction

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/).

To recap, an unencrypted HTML form from a local server starts the transaction. This form specified an unencrypted notification URL for the receipt of the data (although an HTTPS connection can be used instead). Therefore, the HTTPS connection back to PayPal to validate this is pretty important; otherwise, the application might be getting a fake order that just happens to look like a PayPal request.


An example email, formatted as HTML is shown in Figure 6-6.

Figure 6-6. Email notification
figs/rww_0606.gif


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.

Example 6-4. Fax sender code
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.

Example 6-5. Generating the InterFAX bindings
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.

Figure 6-7. Received fax
figs/rww_0607.gif


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.

If you intend to work with faxes a lot?in particular, if you wish to design nicely formatted faxes?you will likely go mad debugging the results using a normal fax machine. You may wish to consider using a fax-to-email provider such as j2.com (http://www.j2.com). There is, of course, a certain pleasant irony in using InterFAX to generate faxes only to then use the j2.com to turn the fax back into an email.


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.

Example 6-6. PayPal tokens
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).

Example 6-7. Fax tokens
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.