If you conduct a lot of business through PayPal, this article can help you use reporting with Ruby-on-Rails to save you time and make your accounts more orgainzed. It is excerpted from chapter seven of the book Practical Reporting with Ruby on Rails, written by David Berube (Apress; ISBN: 1590599330).
PayPal is an e-commerce service that facilitates the electronic transfer of monies via the Internet. Often, PayPal is used by merchants as an easy way to accept credit cards without a merchant account. Additionally, PayPal accounts can be linked to special debit cards, which allows purchases to be made at brick-and-mortar stores using the money in a PayPal account. You can learn more about PayPal at http://www.paypal.com/.
Individuals who do a great deal of business online frequently use PayPal as a convenient way to spend and receive money. In those cases, reporting on data from PayPal can provide important information about financial transactions, as demonstrated in the example in this chapter.
Gathering Data from PayPal
You can take a couple approaches to gathering PayPal data for analysis. One is to use PayPal's web API to download transaction information. Unfortunately, results from searches via the PayPal web API are limited to 100 transactions, which makes it implausible to analyze any significant amount of data.
Tip This chapter covers reporting on PayPal transactions. If you would like your application to be able to accept payments via PayPal, you can do that with the paypal gem, available from http://dist.leetsoft.com/api/paypal/.
Another approach is to download data in CSV format through PayPal's web site. If you have a business PayPal account, you can use the following procedure to download your own PayPal data for this chapter's example. If you don't have a business PayPal account, or you don't have any activity to analyze, you can download sample data from the Source/Downloads area of the Apress web site (http://www.apress.com) or from http://rubyreporting.com/examples/ paypal_example.csv. (The reason you need a business account is that its data includes a number of additional fields that are not in the personal account data.)
Click Download My History to see a screen similar to Figure 7-2. Tip If you have a large volume of data, PayPal will put your report in a queue and e-mail you when it is ready for download.
Figure 7-2.PayPal Download History screen
Select a date range. It doesn't matter which range you pick for this example, as long as it reflects some activity on your PayPal account.
From the File Types for Download drop-down list, select Comma Delimited -All Activity.
Click Download History.
When prompted to save the file, do so. Remember the location, as you'll use this file in this chapter's example.
Note Why aren't we automating the download? Unfortunately, it's because there's no web service available to download it programmatically. It's possible to script the download using, say, the Ruby net/http.rb download library and an HTML parsing library like Hpricot, but unless you have explicit permission from the web site in question, that's typically not a good idea. Potentially, you could be blocked from PayPal or, in an extreme situation, get sued. However, if you do decide to take that route, you might want to employ a library that is easier to use than Ruby's built-in libraries, such as scRUBYt! (http://scrubyt.org/).
Now that you have a CSV file, you can analyze it. It's possible to analyze the data file directly in Ruby, but you might not want to write your own statistical code. Instead, you can load the data into MySQL, and then use MySQL's built-in aggregation functions to analyze it in detail.
Many companies use CSV files to transfer financial, inventory, and other data, so this approach can work with data from organizations other than PayPal.