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 eBay.com in the United States, whereas applications can reach eBay.co.uk 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.
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.
Infos
- Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/magazine/188/Perl
- "What Every Computer Scientist Should Know About Floating-Point Arithmetic": http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
- eBay Developers Program application keys: https://developer.ebay.com/my/keys
- API documentation for
GetAccount
to retrieve an account's monthly eBay invoice: http://developer.ebay.com/Devzone/xml/docs/Reference/ebay/GetAccount.html
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.