Hack 93 Retrieve Transaction Details with the API

figs/expert.gif figs/hack93.gif

Given only a transaction ID, use the GetTransactionDetail API call with the API wrapper DLL to retrieve the details of the transaction.

The GetTransactionDetail API call is a more detailed in terms of the data it returns than the RefundTransaction call [Hack #91] . The initiating call is made in the same fashion, but the response object holds many types that you need to access to get the transaction details. These types are designed to hold information pertaining to the myriad of PayPal transaction types, so if you use PayPal only to process sales from your Shopping Cart (as opposed to eBay auctions or digital subscriptions), you might not need all the information it returns.

But since retrieving information is so important (not to mention loads of fun), this example puts the call through its paces and retrieves all the available transaction details. The response object has a few Type objects that are of interest, because they hold the details of the entire transaction:


Information about the payment, including gross payment amount, fee amount, date of payment, and so on.


Information about the person or entity who sent the payment.


If you sold items, their details are captured in the PaymentItemInfoType.


Returns information about the auction (if the payment came from an auction).


Subscription information, including interval, start date, and so on.

The PayPal API uses its BasicAmountType object to store monetary values (e.g., dollar amounts), such as any property of a Type object with the word amount in it. If there is no amount, the property will be null, which can trip up your routines. To return safe values from these fields, the following code makes use of the GetAmountValue() function [Hack #89] to return a string value.

8.9.1 The Code

Here's the GetTransactionDetail() method that retrieves the transaction details for a given PayPal transaction ID:

public string GetTransactionDetail(string transactionID, string delimiter){

string sOut="";

//Create the request type, which holds information about the transaction you //want more 

information about

GetTransactionDetailsRequestType detailRequest=new GetTransactionDetailsRequestType( );


//Set the request type of the request object

GetTransactionDetailsReq request=new GetTransactionDetailsReq( );


//send the request to PayPal



//make sure there is a response


//use a StringBuilder as this return uses a lot of resources if you just

//just append a regular string value

1.  StringBuilder sb=new StringBuilder( );

sb.Append("************ Payment Information "+ **************"+delimiter);

//access each response type, gathering the information

//payment info

PaymentInfoType payment=response.PaymentTransactionDetails.PaymentInfo;

sb.Append("ReceiptID: "+payment.ReceiptID+delimiter);

sb.Append("TransactionID: "+payment.TransactionID+delimiter);

sb.Append("PaymentDate: "+payment.PaymentDate+delimiter);      

sb.Append("GrossAmount: "+GetAmountValue(payment.GrossAmount)+delimiter);

sb.Append("SettleAmount: " + 


sb.Append("FeeAmount: "+GetAmountValue(payment.FeeAmount)+delimiter);

sb.Append("TaxAmount: "+GetAmountValue(payment.TaxAmount)+delimiter);

sb.Append("PaymentStatus: "+payment.PaymentStatus+delimiter);

sb.Append("PaymentType: "+payment.PaymentType+delimiter);

sb.Append("TransactionType: "+payment.TransactionType+delimiter);

2.  //item info

PaymentItemInfoType item=response.PaymentTransactionDetails.PaymentItemInfo;

int i=1;

sb.Append("************** Item Information ******************"+delimiter);

sb.Append("Custom: "+item.Custom+delimiter);

sb.Append("InvoiceID: "+item.InvoiceID+delimiter);

sb.Append("Memo: "+item.Memo+delimiter);

sb.Append("SalesTax: "+item.SalesTax+delimiter);

//The items are returned in an array of PaymentItemType

//loop through the items array, accessing item information

foreach(PaymentItemType itm in item.PaymentItem){


sb.Append("Item "+i.ToString( )+":"+delimiter);

sb.Append("Name: "+itm.Name+delimiter);

sb.Append("Number: "+itm.Number+delimiter);

sb.Append("Options: "+itm.Options+delimiter);

sb.Append("Quantity: "+itm.Quantity+delimiter);

sb.Append("SalesTax: "+itm.SalesTax+delimiter);




//if you are dealing in auctions, the information about

//the auction will be in the AuctionInfoType

sb.Append("************ Auction Information *************"+delimiter);

AuctionInfoType auction=new AuctionInfoType( );

sb.Append("BuyerID: "+auction.BuyerID+delimiter);

sb.Append("ClosingDate: "+auction.ClosingDate+delimiter);

sb.Append("ClosingDateSpecified: "+auction.ClosingDateSpecified+delimiter);

sb.Append("multiItem: "+auction.multiItem+delimiter);

//Same with Subscriptions

sb.Append("********** Subscription Information ***********"+delimiter);

SubscriptionInfoType sub=new SubscriptionInfoType( );

sb.Append("EffectiveDate: "+sub.EffectiveDate+delimiter);

sb.Append("EffectiveDateSpecified: "+sub.EffectiveDateSpecified+delimiter);

sb.Append("Password: "+sub.Password+delimiter);

sb.Append("reattempt: "+sub.reattempt+delimiter);

sb.Append("Recurrences: "+sub.Recurrences+delimiter);

sb.Append("recurring: "+sub.recurring+delimiter);

sb.Append("RetryTime: "+sub.RetryTime+delimiter);

sb.Append("RetryTimeSpecified: "+sub.RetryTimeSpecified+delimiter);

sb.Append("SubscriptionDate: "+sub.SubscriptionDate+delimiter);

sb.Append("SubscriptionDateSpecified: "+sub.SubscriptionDateSpecified+delimiter);

sb.Append("SubscriptionID: "+sub.SubscriptionID+delimiter);

sb.Append("Terms: "+sub.Terms+delimiter);

sb.Append("Username: "+sub.Username+delimiter);

sReturn=sb.ToString( );


return sReturn;

PayPal does not know the type of the transaction for which you are requesting details, so the web service returns every possible bit of information it can. In this example, all this information is appended to a single string so that it can be displayed easily. Since the string can be long, you'll need a StringBuilder object (line 1). A more practical approach might be to add tables to a DataSet object (if you are using .NET) or perhaps to create your own class to handle this information.

If you are developing a typical commerce site, in which items are sold using PayPal as the payment processor, the section beginning on line 2 will interest you the most. Each item sold is handed back to you in the PaymentTransactionDetails.PaymentItemInfo.PaymentItem array. Each item in the transaction is represented by a PaymentItemType that has pertinent information, such as item number (a.k.a. SKU), price, quantity, and so on.

8.9.2 Running the Hack

To use the API wrapper class to look up details of a transaction, you need to add the Auction and Subscription code to the GetTransactionDetail() method in your API wrapper class and run your PayPalTestApp application. See [Hack #90] for further details.

--Rob Conery and Dave Nielsen