Hacking PayPal

Editor's note: The authors of PayPal Hacks offer two hacks designed to make PayPal more customer friendly, profitable, and accountable through creating PayPal buttons with more than two fields and creating a page that enables customers to check on the status of their orders. Plus, there is an additional hack to catch IPN system errors.

Include More Than Two Option Fields

Give your customers a large selection of
options when purchasing their items, despite the limitations of
payment buttons.

PayPal buttons enable you
to easily offer fixed products to your customers. Although some
flexibility is provided in the form of option fields [Hack #32],
PayPal currently supports only two such fields. If your product has
more than two options (e.g., Size, Color, and Material), you can
employ a little JavaScript code
and a hidden field to create as many option fields as you need.

Start with the basic Buy Now
button code [Hack #28] for a single
item, although this works with Shopping Cart, Subscription, and
Donation buttons as well:

Suppose the item you're selling has three options:
Color, Size, and Material. You can provide three drop-down lists
[Hack #32],
one for each option, with which your customers can customize their
purchases. To keep things simple, name your drop-down elements
custom1, custom2, and
custom3.

This code joins all three of the selected options into a single
variable, custom, to
be passed to PayPal. You'll need to add the custom
form element to your button as a hidden variable with no value
specified. The value will be populated by the JavaScript code when
the form is submitted. Here's an HTML form with form
options and the custom field:

Figure 4-5 shows the additional custom fields in
action. You can include as many option fields as you can fit on your
page.

Figure 4-5. Including additional option fields

You can continue adding as many option fields as you need, provided
that you use the same
custom# naming format.
Just be sure that the total character count for the labels
and their possible variable values does not
exceed 256 characters, the size limit of PayPal's
custom variable.

Add the HTML code to your PayPal button form between the opening and
closing <form> tags. Then add the following
JavaScript code to the head of the web page:

When the complete page is loaded (with the button code in the page
body and the JavaScript in the page head), the customer-selected
option fields will be concatenated into one string and passed through
to PayPal in the custom variable. For instance, if the form is
submitted with its default values, the custom variable will be set to
Color:White Size:Large Material:Spandex. The
string will appear in details of the transaction in your PayPal
account; your customers will never see it. If necessary,
you
can
also
parse this field out in the IPN page [Hack #80] .