PayPal Hacks. 100 Industrial-Strength Tips and Tools Free Open Book

PayPal Hacks. 100 Industrial-Strength Tips and Tools

Previous Section  < Day Day Up >  Next Section

Hack 92 Handle Transaction Errors within the API Wrapper

figs/expert.gif figs/hack92.gif

Write one function to handle all transaction errors and simplify your API code.

If you were to take a close look at the objects created by your web reference in Visual Studio .NET (double-click the PayPal API web reference in your project and navigate to the web reference classes), you'd notice that the ResponseType classes ( RefundTransactionResponseType, TransactionSearchResponseType, and GetTransactionDetailsResponseType) extend the same AbstractResponseType class. This unified error-handling approach provides you the same response object, regardless of which transactional class was called. This means you can write one error-checking routine that displays the correct message if an error occurs in any of these API transactions.

These errors are not application exceptions that you should handle as you normally would. Rather, they are PayPal processing errors that deal with invalid attempts to perform a transaction (such as refunding a payment that's already been refunded).


Just add this code to any of your transaction API calls:

string CheckErrors(AbstractResponseType abstractResponse) {

  bool errorsExist = false;

  string errorList="";

  // First, check the Obvious.  Make sure Ack is not Success

  if (!abstractResponse.Ack.Equals(AckCodeType.Success)) {

    errorsExist = true;

  }

  // Check to make sure there is nothing in the Errors Collection

  if (abstractResponse.Errors.Length > 0) {

    errorsExist = true;

    // Do something with the errors

    foreach(ErrorType error in abstractResponse.Errors) {

      errorList+=("ERROR: "

        + error.LongMessage

        + " ("

        + error.ErrorCode

        + ")"

        );

    }

  }

  return errorList;

}

This method lets you (or your users) know if anything gets in the way of a successful transaction, even if the code otherwise completes successfully. That way, if something does go wrong, you can pass on information that is needed to enable your user to rectify the problem.

8.8.1 Using the Error Handler

To use the error handler, you must add code in two places. First, add a routine to your PayPalTestApp project that checks for errors and handles them appropriately. Second, add the following code to your API wrapper:

try{

        RefundTransactionResponseType response = service.RefundTransaction(request);

        string ErrorCheck=CheckErrors(response);

        if (ErrorCheck!="") {

                sReturn=("PayPal Says: The transaction was not successful: 

                        " + ErrorCheck);

        }

        else {

                sReturn=("PayPal Says: Response: " + response.Ack.ToString( ));

        }

}catch(Exception x){

        sReturn="SSL Failure, the transaction did not go through. Error: "+x.Message;

}

For instance, try updating the code from [Hack #91] with this error handler and running the code again. Since you've already run a refund against this transaction [Hack #91], an error is returned, letting the user know that the type of transaction cannot be refunded, as shown in Figure 8-8.

Figure 8-8. An error message generated by the generic error handler
figs/pph_0808.gif


Naturally, you'll want to supplement this error handler with your own messages and additional error traps, but this should help you build more fault-tolerant API applications.

Rob Conery and Dave Nielsen

    Previous Section  < Day Day Up >  Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y]


         Main Menu
    PayPal Hacks
    Table of Contents
    Copyright
    Credits
    Preface
    Chapter 1. Account Management
    Chapter 2. Making Payments
    Chapter 3. Selling with PayPal
    Chapter 4. Payment Buttons
    Chapter 5. Storefronts and Shopping Carts
    Chapter 6. Managing Subscriptions
    Chapter 7. IPN and PDT
    Chapter 8. The PayPal Web Services API
    Introduction: Hacks #87-100
    8.2 Create a Developer Account
    Hack 87 Set up the Sandbox
    Hack 88 Make Your First API Call
    Hack 89 Create a Wrapper Class for Your API Calls
    Hack 90 Use the PayPal API Wrapper Class
    Hack 91 Refund Payments with the API
    Hack 92 Handle Transaction Errors within the API Wrapper
    Hack 93 Retrieve Transaction Details with the API
    Hack 94 Search for PayPal Transactions
    Hack 95 Hack the API Wrapper
    Hack 96 Issue Payments en Masse with the Mass Pay API
    Hack 97 Pay Affiliates and Suppliers on a Schedule
    Hack 98 Search eBay for Listings that Accept PayPal
    Hack 99 Test IPN and PDT in the Sandbox
    Hack 100 Go Live
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele