Home arrow Web Services arrow Page 5 - Automated Billing and Faxing for the Web
WEB SERVICES

Automated Billing and Faxing for the Web


If you do a lot of payment processing through PayPal, you can save some serious time by automating the process. This article will show you how. It is excerpted from chapter six of the book Real World Web Services, written by Will Iverson (O'Reilly; ISBN: 059600642X). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 6
August 23, 2007
TABLE OF CONTENTS:
  1. · Automated Billing and Faxing for the Web
  2. · Starting the Transaction
  3. · Getting a Transaction Notification
  4. · Getting a Transaction Notification continued
  5. · Responding to the Transaction

print this article
SEARCH DEVARTICLES

Automated Billing and Faxing for the Web - Responding to the Transaction
(Page 5 of 5 )

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

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, you need to 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.

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 be working with faxes very much—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.


Figure 6-7.  Received fax

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 need to 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.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
WEB SERVICES ARTICLES

- Dealing with Loose Coupling in a Service-Ori...
- Loose Coupling in a Service-Oriented Archite...
- Safety, Idempotence, and the Resource-Orient...
- The Resource-Oriented Architecture in Action
- Features of the Resource-Oriented Architectu...
- The Resource-Oriented Architecture
- Getting Started with Flex
- Automated Billing and Faxing for the Web
- An Introduction to Web Services
- The Foundations of Web Services: From Novice...
- Web Services Reengineering: Finishing Touches
- Fault Handling with Web Services
- Flow and Web Services
- Process Lifecycles and Web Services
- Business Processes and Web Services

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials