I wanted to use the Paypal API from a C# project recently. Since they propose a SOAP interface for their API, I naïvely thought I could just consume it with the great “web reference” tool from visual studio.
However, I faced many disappointments: first, the WSDL contains errors, and if you want to use it in VS, you’ll have to manually download the wsdl (and every included definition files), correct the errors, and add a web reference from your locally modified WSDL.
This was not a good sign: clearly the people from paypal didn’t bother to check their WSDL, and on paypal forums, they suggest the complaining developpers to use their former web API (NVP).
I kept trying, and eventually could make VS create my web reference. But then faced another issue: the interface doesn’t match the informations you have. To access the API, paypal gives you an ID, a password, and a Signature string. There were several fields, but nothing was close to this, and there was absolutely no documentation.
On the paypal website, they suggest to use a .net 2.0 class library you have to compile, but I feel there is something wrong about this: First, what’s the point to publish a WSDL to expose a webservice if you need a special wrapper to use it correctly. Then, if we have to use a wrapper, then the old style API is more accurate, since it’ll avoid the SOAP overhead. And the binary they ask us to compile is fairly complex, so I really didn’t feel like using it.
Since I didn’t want to use the wrapper they suggested, and couldn’t consume their service directly, I decided to create my own class library to access the service.
It’s small, simple, and accurate (it uses the NVP).
So far, I implemented 2 method: TransactionSearch and GetBalance. The first will list all the transactions between 2 dates, and the second returns your current account balance. Both method are very straightforward:
PaypalMgr mgr = new PaypalMgr();
mgr.SetCredentials("paypal_api1.yyy.com", "MYAPIPASSWORD", "MyApiSignature");
double balance = 0;
// Prints the account balance
if (mgr.GetBalance(ref balance)) Console.Out.WriteLine("Current account balance: " + balance.ToString());
else Console.Error.WriteLine("An error occured: "+mgr.LastError);
// Prints all the transactions of the last you received during a month (until now)
List<PaypalTransaction> list = mgr.TransactionSearch(DateTime.Now.AddMonths(-1), DateTime.Now);
if (list == null) Console.Error.WriteLine("An error occured: " + mgr.LastError);
Console.Out.WriteLine("Last month transactions\n---------------------------------------");
foreach (PaypalTransaction pt in list) Console.Out.WriteLine(pt.ToString());
As you can see, it only takes a couple of lines to retrieve your balance or a transaction list. In order to use the PaypalMgr class, you only need to add TWCS.dll as a reference in your project. If you need to support more methods, don’t hesitate to contact me.
Download TWCS.dll: here
ps: for those who remember TeapotWare, twcs is the rebirth of the project, as a .net class library.