Hack 92 Handle Transaction Errors within the API Wrapper

 < Day Day Up > 

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

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

 < Day Day Up > 

PayPal Hacks
PayPal Hacks
ISBN: 0596007515
EAN: 2147483647
Year: 2004
Pages: 169

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net