Perl script monitors payments and feedback in eBay sales

Saint Bureaucratius

The documentation on the eBay Developers Program pages [3] is organized quite chaotically; the individual APIs overlap for historical reasons. The API Reference page for the GetAccount request [4] shows which parameters it needs to retrieve an invoice created on a specific date.

As Listing 2 shows, the post request sent over the eBay API will then still require a few HTTP header values alongside the XML body. The X-EBAY-API-SITEID header value determines which eBay department and country the request is sent to, and the 0 value is valid for in the United States, whereas applications can reach with the number 3.

The X-EBAY-API-COMPATIBILITY-LEVEL header specifies the lowest version of the web API with which the client is able to work. Currently, version 959 is the most up to date; the value entered in the script (863) is for academic purposes only. To get the output in XML, line 22 sets the Content-Type header to text/xml. The fact that the application runs the GetAccount server method is already stated in the XML body of the post request, but Saint Bureaucratius demands that it appear again in the X-EBAY-API-CALL-NAME header in line 19.

eBay always generates bills at the end of the month. To jump from the current day to the last day of the past month (i.e., the last billing date), Listing 2 sets the value for the current day to 1 (i.e., the first of the current month); then it subtracts one day from there. It therefore lands on the last day of the past month.

The DateTime function ymd converts the date for the March 2016 invoice, for example, into the 20160331 format, which the eBay API recognizes if the SpecifiedInvoice value is entered in the request under AccountHistorySelection. The date of this invoice must then be added with InvoiceDate.

The script looks for the sensitive auth token in a file named token, from which it reads it in line 27, removing a potential newline character at the end of the file in line 28 and then integrating the key into the XML blob via the $token variable as of line 35. The date of the invoice is found in the $invoice_date variable and is also added to the XML; line 38 interpolates the value.

If an error occurs during the retrieval of data, line 47 aborts the script and prints the error report from eBay. Typical problems include the portal reacting badly to unwanted spaces and line breaks in the XML data, although it does fortunately describe them in detail in its error reports. If all goes smoothly, line 50 prints the returning XML to the standard output, where the next stage of the processing pipeline accesses the data.

Adding Up (Correctly)

The script in Listing 3 grabs the XML blob from Listing 2 and searches inside it for the line items (Figure 8) and the total, relying on the XML::Simple CPAN module to do so. It then verifies whether the addition of the line items produces the total sum listed. The beauty of XML::Simple is that it converts the entire XML into a huge Perl data structure in which a script can labor away at its convenience. Tags are converted into hashes, and it creates Perl arrays from lists of single entries. For text that occurs between two XML tags as their content, XML::Simple creates a hash entry named content. Using this name, for instance, in line 30 in Listing 3, the script extracts the invoice's total amount from the InvoiceBalance tag.

Figure 8: The invoice in XML format lets you add single entries to verify whether the total is correct.

As I already said, you are strongly advised not to simply add up dollar (or Euro) and cent amounts as floating-point values [2]. Instead, careful programmers should calculate everything in cents and process these as integers to subsequently separate the last two numbers from the result and account for them in cents.

The Math::Currency CPAN module does this correctly and formats the result attractively, even if the input data isn't provided correctly, as in Ebay's one-digit decimal-point format (e.g., $1.6). However, the course of time has taken its toll on the CPAN module; its last release is six years old, and the test suite it includes rattles through the test with creaks and groans and fails in the end. The module does work, however, so I simply forced the CPAN shell to install it by typing:

cpanm --force Math::Currency

The XML invoice contains not only the line items and the running total, but also the amounts already transferred to eBay by the customer from previous bills. Line 15 acknowledges by looking for ItemIDs with a value of 0, screening out all fees unrelated to individual item sales.

Monitoring the Checkout Assistant

The error in the eBay invoice brought me around to the idea of also monitoring the line items on my receipt when I next visit the supermarket. Who would notice if the total shown there was a few cents off from the sum total of the line items? No one, except me, of course! I've already made a plan to monitor them with a script that gobbles up the OCR data from the receipt and runs the test. Potentially something you can look forward to reading in a future column.


  1. Listings for this article:
  2. "What Every Computer Scientist Should Know About Floating-Point Arithmetic":
  3. eBay Developers Program application keys:
  4. API documentation for GetAccount to retrieve an account's monthly eBay invoice:

The Author

Mike Schilli works as a software engineer in the San Francisco Bay Area. He can be contacted at Mike's homepage can be found at

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95