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="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="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="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 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
  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[i]) {
      discnt = coupdc[i];  // 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 - (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 – http://thelibzter.com/discount.js. 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 – 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 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="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.

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

Meet the author

My name is Libby Fisher and I am an experienced freelance web developer based in Seattle WA. I am passionate about developing websites that are both beautiful AND easily usable because I believe that the most effective websites are a perfect balance of aesthetic appeal and usability/intuitiveness.

  • 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.

  • 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?

    Thanks

    Ian

  • Pete

    Libby,

    Great post. How about simply looking at a dropdown list and applying a fixed discount % based on the choice on the list? We want to have a choice on a dropdown “where did you hear about us” that would generate a discount. In our case, if you were at a conference, you’d get the discount.

  • http://thelibzter.com Libby Fisher

    Hi Pete,

    Great question! I just wrote a blog post on how to do that – http://icode4you.net/use-a-dropdown-menu-to-apply-a-discount-to-your-paypal-button. Please let me know if that doesn’t answer your question. Thanks!

  • Laura

    Thank-you so much for this! It is exactly what I’ve been looking for and after a few days of working on it (js newbie) I’ve gotten it to work for me.

    BUT when using the code as given on this post I’m seeing a DOLLAR amt discount instead of the PERCENTAGE off that I really want. What am I missing? 😳

  • http://thelibzter.com Libby Fisher

    Hi Laura,

    Could you please send me a link to the page you are working on so that I can take a look at it? If you want, feel free to email it to libby@thelibzter.com and I will see if I can figure out the issue.

    Thanks!!

  • Yann

    It work’s fine. But is it possible to be sure that only one rebate can be used for each item (now if I enter two codes, i have two rebates for the same product).

    Thanks!

  • http://thelibzter.com Libby Fisher

    Hi,

    I believe if you follow the steps in this tutorial – http://www.willmaster.com/library/features/paypal-buy-now-buttons-with-coupon-codes.php – you can create a separate discount for each item. Please let me know if that does not do what you are looking for. Thanks!

  • http://mysticangeldesign.com Misty

    Hi,I have been racking my brain for three nights now, and I’m just so tired it looks the same to me now, if there is any insinght you can give me i’d be extremely thankful. I have gotten the check coupon button and the buy now buttons to work but never together, and now both don’t work.
    Here is the page to the website now with the current button ~
    http://bainswhips.com/ourwhips.html and here is the page with code I have been working on,
    ~ http://bainswhips.com/ourwhips_wc.html you can see at the bottom I have added a simplified button with the Check Coupon code
    I have been on a mission for three nights trying to get this right, and I don’t think I would even see it now if I try.
    Thanks for any information at all you can send my way !!!

  • http://www.rescuepegs.com Steven

    Hi Libby,

    I followed your instructions to the letter I think. The coupon code, check code, and Buy Now buttons all show up, but fail to execute when clicked. Nothing happens.

    Any ideas?

    Thanks

  • http://thelibzter.com Libby Fisher

    Hi Misty,

    I believe the problem is because you don’t have a field called “baseamt” in your button code, and that is what the discount is calculated off of. You could try changing “os0″ to “baseamt” and see if that works, without looking at it closer I am not sure if that will work or not but I would try it. If it doesn’t work, please let me know and I can try to figure it out.

    Thanks!

  • http://thelibzter.com Libby Fisher

    Hi Steven,

    Please send me a link to the page you are working on, either here in a comment or at my email address – libby@thelibzter.com – and I will take a look at what you’ve got.

    Thanks!

  • Steven

    Thanks Libby,

    Here is the web page: http://www.rescuepegs.com/order-working-discount2.html

  • http://thelibzter.com Libby Fisher

    Hi Steven,

    Ok, the first thing I see is that in the source code of your HTML page, the quotation marks are not in plain text, it looks like they were copied and pasted from Word perhaps? Anyways, I would try retyping the line in the head section of your web page that calls the Javascript and see if that makes a difference. Let me know if it doesn’t and I will look into it further. Thanks!

  • http://candlesbysuzy.com suzy

    i am currently working on a new website through vista print. their system allows me to create a paypal shopping cart. I’d like to offer a % discount if the customer has a coupon cde, but with vista I do not see the html or js wording. any ideas how i could still add the discount?

  • http://thelibzter.com Libby Fisher

    Hi Suzy,

    If you want to send me the login information for your site, I’d be happy to log in and take a look and see if it is possible to add the codes you would need in order to offer a paypal discount. My email address is libby@thelibzter.com. Thanks!

  • Jamie

    Hi Libby,

    Thanks sooooo much for posting this code. I tried to read through all of the comments to find my answer w/o having to ask you but my client would like to offer Free Shipping. I’m not sure based on what I’ve read so far that this is an option since this would be a variable amount based on the buyer’s location. Any thoughts on making this work?

    Thanks again for all of the code and your quick replies to all of the comments!!!!

    Jamie

  • http://thelibzter.com Libby Fisher

    Hi Jamie,

    Are you trying to offer Free Shipping when someone has a coupon code for it or just in general?

    Thanks!

  • http://www.barepantryshow.com Barbara Grinage

    Hi Libby, I did all the step until I got this one and then I’m lost..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: I built my website in Weebly’s drag and drop program and don’t know how to save or upload the Java script to my server/website. The code and button shows up, but they don’t work. Any help would be greatly appreciated.

  • http://thelibzter.com Libby Fisher

    Hi Barbara,

    I have never had a weebly site so I am not positive about this but I dont know if there is a way for you to upload the Javascript that you need, in which case this solution may not work for you :( Could you try contacting someone at weebly and asking about whether or not you are able to upload files and access the “head” section of your website? You will need both of those capabilities in order to make this solution work. Thanks!

  • Pedro

    In order to use custom javascript with Weebly, you will need to host the scripts somewhere else, like dropbox.com. Here’s what to do:

    1. Open a free dropbox account (if you use this link, you’ll get 500MB of extra storage space: http://db.tt/FTeGeRA2)

    2. make a folder called “javascript” (or whatever) within the “Public” folder on dropbox

    3. Save your discount.js file in that folder

    4. Right-click on the file, select “Copy public link”, then click “Copy to clipboard

    5. Paste the link you just copied in the src code:

    That should do it. If you have trouble finding where to put the <script… stuff, let me know.

  • http://www.bringitupsexy.com Mi

    Hi there,
    I completely understand all your steps until I get to this one:

    “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:”

    I created a next text file, but where do I paste the code once I have made my updates?

    I also uploaded and saved the .js file to my server, but does that require mortification too?

    I’m a relative novice at Javascript, so any help you could offer is appreciated.

  • http://thelibzter.com Libby Fisher

    Hi,

    You paste the Javascript code into the text file and save it with a .js extension. If you’d like to send me a link to the page you are working on, either here or to my email address (libby@thelibzter.com), I can take a look at what you’ve got so far.

    Thanks!

  • Matt

    Hi,
    I have a similar problem to some of the earlier posts. The coupon code, check code, and Buy Now buttons all show up, but fail to execute when clicked. Nothing happens. Site is http://www.friendshipwinefest.com/ticket.html

    Thanks,

    Matt

  • http://thelibzter.com Libby Fisher

    Hi Matt,

    Well the first thing I notice is that it doesn’t look like you are calling the Javascript file in the < head > section of your web page so that would be the first thing to address. Once you do that, does the discount work?

    Thanks!

  • http://thelibzter.com Libby Fisher

    Hi Pedro,

    Thank you for sharing, that it very good to know because I get a lot of questions on how to make this work with a weebly site. Great tip! :)

    ~Libby

  • Dave

    I am having this same problem. It seems the disabled=”disabled” piece of the image tag is keeping the button from firing, even when the correct discount code is entered. Does anyone know what part of the code causes the button to read the discount code and enable the button?

    Thanks in advance for any help!

  • http://thelibzter.com Libby Fisher

    Hi Dave,

    It is the onclick=”coupval =this.form.coupcode.value; ChkCoup();” code that checks to make sure the coupon code is valid and it is the onclick=”CalculateOrder(this.form) line in the code that causes the discount to be calculated/applied when the coupon code is valid and the person wants to add the item to the cart. Hope that makes sense!