Get more 100 matched results using Paypal TransactionSearch
ctg | 07 Julio, 2008 14:11
One of the main problems using TransactionSearch on Paypal API is only returns last 100 records. In this case, TransactionSearch returns a SuccessWithWarning ack to indicate this problem.
To fix it, I made a code to get the status of one day transactions doing a "hack" to avoid 100 records problem.
// Sets user, password and signature
UserIdPasswordType user = new UserIdPasswordType();
user.Username = this.user;
user.Password = this.password;
user.Signature = this.signature;
// Define TransactionSearchResponseType. Inside loop we add params to adjust search
TransactionSearchResponseType transactionSearchResponseType = null;
// Paypal only can get 100 results at time.
do
{
// Prepare the interface with user
using (PayPalAPISoapBinding paypalInterfaceSearch = new PayPalAPISoapBinding())
{
paypalInterfaceSearch.RequesterCredentials = new CustomSecurityHeaderType();
paypalInterfaceSearch.Url = "https://api-3t.paypal.com/2.0/";
paypalInterfaceSearch.RequesterCredentials.Credentials = user;
// Prepare TransactionSearch request
TransactionSearchRequestType transactionSearchRequestType = new TransactionSearchRequestType();
transactionSearchRequestType.Version = "3.2";
transactionSearchRequestType.TransactionClass = PaymentTransactionClassCodeType.All;
transactionSearchRequestType.TransactionClassSpecified = true;
// Adjust the dates according is in loop or not
if (null != transactionSearchResponseType)
{
// StartDate always is 0:00:00 hours
transactionSearchRequestType.StartDate = new DateTime(
transactionSearchResponseType.PaymentTransactions[99].Timestamp.Year,
transactionSearchResponseType.PaymentTransactions[99].Timestamp.Month,
transactionSearchResponseType.PaymentTransactions[99].Timestamp.Day,
0, 0, 0);
// EndDate is last PaymentTransactions less 1 second. This way we obtain left results
transactionSearchRequestType.EndDate = transactionSearchResponseType.PaymentTransactions[99].Timestamp;
transactionSearchRequestType.EndDate.AddSeconds(-1);
}
else
{
transactionSearchRequestType.StartDate = startDate;
transactionSearchRequestType.EndDate = endDate;
if (startDate == endDate)
{
transactionSearchRequestType.EndDate = transactionSearchRequestType.EndDate.AddDays(1);
}
}
transactionSearchRequestType.EndDateSpecified = true;
TransactionSearchReq transactionSearchRequest = new TransactionSearchReq();
transactionSearchRequest.TransactionSearchRequest = transactionSearchRequestType;
// Get results and Ack
transactionSearchResponseType = paypalInterfaceSearch.TransactionSearch(transactionSearchRequest);
// Check Ack, if OK, get results
if (AckCodeType.Success == transactionSearchResponseType.Ack || AckCodeType.SuccessWithWarning == transactionSearchResponseType.Ack)
{
if (null != transactionSearchResponseType.PaymentTransactions)
{
foreach (PaymentTransactionSearchResultType paymentTransactionSearchResultType in transactionSearchResponseType.PaymentTransactions)
{
// PaymentTransaction only include PayPal TransactionID. To get JShop order number it's necessary get more details using TransactionID
// Its necessary prepare again the interface if not fails.
using(PayPalAPISoapBinding paypalInterfaceDetails = new PayPalAPISoapBinding())
{
paypalInterfaceDetails.RequesterCredentials = new CustomSecurityHeaderType();
paypalInterfaceDetails.Url = "https://api-3t.paypal.com/2.0/";
paypalInterfaceDetails.RequesterCredentials.Credentials = user;
// Prepare TransactionDetails
GetTransactionDetailsRequestType transactionDetailsRequesType = new GetTransactionDetailsRequestType();
transactionDetailsRequesType.Version = "3.2";
transactionDetailsRequesType.TransactionID = paymentTransactionSearchResultType.TransactionID; // Set TransactionID to search
GetTransactionDetailsReq transactionDetailsRequest = new GetTransactionDetailsReq();
transactionDetailsRequest.GetTransactionDetailsRequest = transactionDetailsRequesType;
// Get results
GetTransactionDetailsResponseType transactionDetailsResponseType = paypalInterfaceDetails.GetTransactionDetails(transactionDetailsRequest);
if (AckCodeType.Success == transactionDetailsResponseType.Ack)
{
if (null != transactionDetailsResponseType)
{
// Check paymentTransactionSearchResultType to see the transaction status
}
}
else
{
// Log message here. TransactionDetailsResponseType not successful searching TransactionID
}
}
}
}
else
{
// Log message here. TransactionSearchResponseType not sucessful
}
}
}
} while (transactionSearchResponseType.Ack == AckCodeType.SuccessWithWarning);
Publicado en C#, Paypal . Comentario: (0). Retroenlaces:(0). Enlace