Hack 58 Offer Discount Coupons

figs/moderate.gif figs/hack58.gif

Reward good customers and entice new buyers with electronic coupons.

Everyone loves a sale. Customers like them because they get a bargain, and merchants like them because they increase sales. For instance, Amazon.com uses their Share the Love system to entice customers to advertise the products they've just purchased, in exchange for 10% off future purchases.

PayPal doesn't offer a built-in mechanism to process discounts, but you can set up electronic coupons for your customers with your own code. This hack provides two ways to pull it off: at the browser (a.k.a. client-side) with JavaScript, and at the server using Microsoft's Active Server Pages.

5.15.1 Accepting Coupons on the Client Side

While traditional coupons consist of slips of paper presented at the checkout counter of your local grocery store, electronic coupons are nothing more than distinct strings of numbers and letters.

This JavaScript-powered example allows a customer to specify a coupon code and then purchase an item at a discounted price:



<!-- -->

<script language = "JavaScript">

function on1Verify( )


1.  var orderTotal=7.95;

  var on1Value=window.document.form1.on1.value;


2.  if((on1Value < 990) && (on1Value > 988))


    var newTotal=orderTotal-2;

    if(newTotal < 2)newTotal = 0;

3.  window.document.form1.on1.value="$2.00";

    window.document.form1.amount.value="$" + newTotal;


4.  if((on1Value) < 989 || (on1Value > 989))


    window.document.WEB_ORDER_FORM.on1.value=" -";







<form name="form1" action="https://www.paypal.com/cgi-bin/webscr"

                method="post" target="paypal">

<input type="hidden" name="cmd" value="_cart">

<input type="hidden" name="add" value="1">

<input type="hidden" value="seller@example-domain.com" name="business">

<input type="hidden" name="item_name" value="Coupon Code 1">

<input type="hidden" name="item_number" value="001">

<input type="hidden" value="Selected" name="on0">Select<BR>

<select name="os0">

<option value="Option 1" selected>Option 1

<option value="Option 2">Option 2

<option value="Option 3">Option 3



Enter Coupon number:<BR>

<input type="text" name="on1" size="10" onChange="on1Verify( )">

<br><input type="hidden" value="DISCOUNT" name="os1">

Total amount due:<BR>

5.  <input type="text" name="amount" value="7.95" size="10"> 

<input type="hidden" value="http://www.example-domain.com" name="return">

<input type="hidden" value="http://www.example-domain.com" 


<BR>Shipping:<BR> <SELECT name="shipping">

<OPTION value="5.00" >Standard

<OPTION value="10.00" >Next Day

<OPTION value="15.00" >Over Night


<input type="hidden" select_name="shipping" value=""> 

<input type="hidden" name="shipping2" value="5.00"> <BR>


<input type="text" name="handling" value="2.00" size="10"> 

<p><input type="submit" value="Submit" name="B1">




Anyone who views the source of this page will be able to discover the code needed to obtain the discount. To avoid this problem, you might want to obfuscate your code [Hack #36] or use server-side coupon verification, as described later in this hack.

The normal price of the item is $7.95, as specified on lines 1 and 5. The customer enters a valid coupon code (here, the code 989 is tested on lines 2 and 4, the purchase price drops by $2.00 (line 3). Figure 5-12 shows what the form looks like.

Figure 5-12. Processing coupons with a simple HTML form and some client-side JavaScript

5.15.2 Hacking the Hack

The code in the previous section is designed to accommodate a specific range of coupon codes. As shown, the range only allows 989, but you can increase this by changing line 2 to:

if((on1Value > 5381)&&(on1Value < 5478))

and line 4 to:

if((on1Value < 5382) || (on1Value > 5477))

Doing so instructs the script to accept any coupon code between 5382 and 5473, inclusive.

5.15.3 Verifying Coupons on the Server Side

The previous solution shows how to use the browser for simple coupon processing, but for better security and more flexibility, you'll want to enable the discount at the server.

This example uses a special (presumably secret) URL to enable the discount. The URL itself serves as the coupon, and once your customer has visited this page, a discount of your choice will apply. From your customer's perspective, getting the discount price is simple:

  1. The customer receives a promotional email from you that contains the coupon.

    Never send unsolicited email messages (also known as spam) to your customers. Let customers who want to hear about your specials opt in by adding their email addresses to your mailing list.

  2. The customer clicks the coupon link, and the resulting page shows a "Thank You" or some other confirmation, followed by links to your shopping pages.

  3. All applicable prices on your site subsequently reflect the discount for this customer.

Behind the scenes, the coupon page contains a script that sets a session variable for the customer's visit. Session variables are available with many scripting languages and are easy to implement with ASP, as in this example.

First, create the coupon page, the page shown to customers when they click your coupon links. This is also where the session variable is set:


'Set the session variable

        Session("discount") = "true"


<h1 align="center">Thanks for using your coupon</h1>

<p>Your discount has been enabled.</p>


'Redirect to storefront



Give the script a particularly obscure URL to prevent customers from accidentally discovering it, such as:


Next, modify your PayPal buttons to check for the discount session variable:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">

<input type="hidden" name="cmd" value="_xclick">

<input type="hidden" name="business" value="sales@wwjcd.biz">

<input type="hidden" name="item_name" value="Jackie Chan Bobble Head">

<input type="hidden" name="item_number" value="BH-JC1">

<input type="hidden" name="no_note" value="1">

<input type="hidden" name="currency_code" value="USD">

<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif"

                border="0" name="submit" alt="Make payments with PayPal - it's fast,

                free and secure!">

<% If Session("discount") = "true" Then %>

        <input type="hidden" name="amount" value=".90">

<% Else %>

        <input type="hidden" name="amount" value="1.00">

<% End If %>


Although you can see the code that checks for the discount setting (and the setting that is being checked), your buyers will never see it. Everything between the code markers (<% and %>) is processed and subsequently removed by your web server by the time your customers view the page.