Create a discount or coupon for a set dollar amount for a PayPal button

Here are the codes you can use to create a coupon or discount for a set dollar amount for your PayPal buttons (as opposed to a percentage amount, which I covered in a previous post on how to create a coupon for your PayPal button).

First, use this code for your button:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" onsubmit="this.target = 'paypal'; return ReadForm (this);">
<input type="hidden" name="cmd" value="_xclick" />
<input type="hidden" name="add" value="1" />
<input type="hidden" name="business" value="" />
<input type="hidden" name="item_name" value="10 sessions" />
<input type="hidden" name="amount" value="773.00" />
<input type="hidden" name="currency_code" value="USD" />
<input type="hidden" name="baseamt" value="773.00" />
<input type="hidden" name="basedes" value="10 sessions" />
Enter Coupon code
<input type     = "text" size     = "10"  name     = "coupcode"; />
<input type="button" value="Check code" onclick="coupval =this.form.coupcode.value;  ChkCoup();" /><br/><br/>
<input type="image" id="xx" disabled="disabled" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" onclick="CalculateOrder(this.form)" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

Make sure you use either your PayPal email address or your Merchant account ID number for this line:

<input type="hidden" name="business" value="" />

Next, we will create the Javascript file we need. Here is the code I use:

<!--
var discnt = 0;   // no default percent discount

var coupons = new Array (  // place to put coupon codes
  "coup1",                 // 1st coupon value - comma seperated
  "coup2",                 // 2nd coupon value - add all you want
  "coup3"                  // 3rd coupon value
);
var coupdc  = new Array (  // place to put discounts for coupon vals
  5,
  10,
  15
);
var coupval = "(blanket)"; // what user entered as coupon code

function ChkCoup () {      // check user coupon entry
var i;
  discnt = 0;              // assume the worst
  for (i=0; i<coupons.length; i++) {
    if (coupval == coupons&#91;i&#93;) {
      discnt = coupdc&#91;i&#93;;  // remember the discount amt
      alert ("This is a valid promo code! \n\n" + "$" + discnt +
             " discount now in effect.");
      return;
    }
  }
  alert ("'" + coupval + "'  is not a valid promo code!");
}

function Dollar (val) {      // force to valid dollar amount
var str,pos,rnd=0;
  if (val < .995) rnd = 1;  // for old Netscape browsers
  str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (rnd, pos + 3);
  return str;
}

function ReadForm (obj1) {  // apply the discount
var amt,des;
  amt = obj1.baseamt.value*1.0;       // base amount
  des = obj1.basedes.value;           // base description

  if (discnt > 0) {                   // only if discount is active
    amt = Dollar (amt - (discnt));
    des = des + ", " + "$" + discnt + "dis, COUP = " + coupval;
  }

  obj1.amount.value = Dollar (amt);
  obj1.item_name.value = des;
}
//-->

Here is a link to the Javascript file that you need – http://thelibzter.com/discount2.js. Feel free to right-click and save that file to your computer rather than creating your own Javascript file if you wish.

To add the passwords you want to use, change the names coup1coup2, and coup3 to the passwords that you want to use.

To add the discount amounts, change the 510, and 15 to the percentage that you want the discounts to be for.

The final step, which I use to make the discount button more secure, is to pack part of the Javascript code. I copy the line of the code that contains my discount passwords:

var coupons = <strong>new</strong> Array (  // place to put coupon codes   "coup1",                 // 1st coupon value - comma seperated   "coup2",                 // 2nd coupon value - add all you want   "coup3"                  // 3rd coupon value );

And paste it into Dean Edward’s packer – http://dean.edwards.name/packer/. Click “pack” and then copy the packed code and paste it into your Javascript file in place of the line above, which declared our passwords. This makes it more difficult for people to see your passwords.

Save the Javascript file as discount.js or whatever you want it to be called. Upload it to your server. Add this line of code to your website’s section:

<script type="text/javascript" src="http://yourwebsite.com/discount.js"></script>

Make sure you change the name of the Javascript file in the code above to the name of your own file.