How to create a coupon discount for PayPal buttons

Here are the codes I use to create a coupon discount for PayPal buttons.

First, create your PayPal button:

  1. Log in to your PayPal account.
  2. Click on the Merchant Services Tab and select “Buy Now Button”
  3. Click create a “Buy Now” button.
  4. Enter your information to customize the button to your needs.
  5. Click Step 2 and uncheck the “Save button at PayPal”
  6. Click “Create Button.”
  7. Click “Remove code protection”
  8. Click “Select Code” to select the button code.

Next, paste the code for your PayPal button into your webpage.

To add the check discount code box, paste this line of code before the “input type=”image” part of the button code:

Enter Coupon code
<input type="text" size="10" name="coupcode"; />
<input type="button" value="Check code" onclick="coupval =this.form.coupcode.value;  ChkCoup();" />

You can also use this block of code for your button and edit as needed rather than generating your own button from PayPal:

<form action="" method="post" onsubmit=" = '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="1 Item" />
<input type="hidden" name="amount" value="50.00" />
<input type="hidden" name="currency_code" value="USD" />
<input type="hidden" name="baseamt" value="50.00" />
<input type="hidden" name="basedes" value="1 Item" />
Enter Coupon code
<input type="text" size="10" name="coupcode"; />
<input type="button" value="Check code" onclick="coupval =this.form.coupcode.value;  ChkCoup();" />
<input type="image" id="xx" src="" border="0" name="submit" onclick="CalculateOrder(this.form)" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="" width="1" height="1">

Make sure you add the correct value (either your PayPal email address or your Merchant account ID number) to this line:

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

If you want to force people to use a discount code before adding an item to their cart, make sure you add this line of code to the disabled="disabled"

This disables the button until the correct coupon code is entered. This is only necessary if you are using the button ONLY for people who have discount codes. If you want people to be able to make purchases whether they have a discount code or not, you don’t need to add that line.

Next, you will want to create the Javascript file which contains the passwords and discounts for your button. Copy and paste this code into a new text file:

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
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[i]) {
      discnt = coupdc[i];  // remember the discount amt
      alert ("This is a valid promo code! \n\n" + discnt + "%" + 
             " discount now in effect.");
  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 - (amt * discnt/100.0));
    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 will need – Feel free to right-click and save the file to your desktop so that you can edit it and upload it to your own site.

To add the coupon codes you want to use, change the names coup1, coup2, and coup3 to the passwords that you want to use.

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

You will want to make sure that the button amount is higher than any of the discounted prices. For instance, if you want to offer three different discounts – one which reduces the price to $750, one which reduces is to $725, and one which reduces it to $700 – you will want to make sure that the amount of the original button is at least $750, and then add the discounts for the correct percentages to bring the price down to the amount you want.

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 – 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 coupon codes.

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=""></script>

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

Let me know if you run into any issues or have any questions.

Meet the author

Libby Fisher is an experienced freelance web developer, recently relocated from Seattle to Boston, and passionate about developing websites that are both aesthetically appealing and intuitively usable - or as she prefers: "beautifully effective."

  • Jane

    Can I use this code to offer a certain dollar amount of, in addition to percent off codes? Such, one code is for 10% off, but another code is for $10 off?

    Also, how can I have the coupon applied to the total cart, rather than a single item? Thanks so much.

    • Joe Schmo

      For anyone who wants this, simply change the math code:

      if (discnt > 0) { // only if discount is active
      amt = Dollar (amt – discnt * (amt / NUMBER PRICE OF YOUR PRODUCT));
      des = des + “, $” + discnt + ” OFF ” + “PROMO”

      • Libby

        Thanks Joe!

  • Ian Williams

    Hi Libby

    Thanks for this tutorial, I was tearing my hair out with paypal tech support over such a simple feature…

    Anyway, like a few others, I cant get it to work, and I also need to change the dollar denomination to GBP if possible.

    My users need to fill out the form on the page and enter a discount code if they have one.

    Can I email you with a link to the form so you can see where I may have gone wrong?



    • Libby

      Hi Ian,

      I am sorry for the slow response on this but yes – please email it to if you still want help :)

  • Mike Ryczek

    Hi Libby,

    I’ve been using this code for a client’s site for a while now and it works perfectly for what is needed. However, I just realized that if the user enters a coupon code for one item, it applies the same discount for that item to any other of the 6 total items as well, including 2 items that aren’t supposed to be discounted. I have a separate form for each item so I’m not sure why this is happening. Any thoughts?



    • Libby

      Hi Mike,

      Hmm thats interesting! Can you please email a link to your client’s site to and I’ll take a look?


  • Gordon Reynolds

    Hello Libby,

    I cant seem to get it to work properly. The button is disabled, but when the correct code is entered and the javascript pop acknowledges, but the submit button remains disabled.

    • Libby

      Hi Gordon,

      Please paste a link to your site here or email it to libby! and I’ll take a look. Thanks!!

  • Vitor

    I can create a paypal coupon code 100% off?

  • Michael Cooper

    Hello, just a quick question. When i click on the Ckeck code button, nothing happens, is this correct? Or am i missing something?

  • hackman

    Having the list of your coupons in JS is the most stupiedst thing. A user can display the code of your page and see all the array. using AJAX with php is better