Home Coupon Code Apply Paypal discount coupon code at checkout for Multiple Prices (Core Javascript Method)
Coupon Code - Javascript - PayPal - Tech - June 30, 2016

Apply Paypal discount coupon code at checkout for Multiple Prices (Core Javascript Method)

Recently I had searched about to apply PayPal discount coupon code at the time of checkout. I searched a lot but did not find for multipriced products on paypal button. Paypal do not have a discount coupon facilities so we have to do it ourselves. The best method is using PHP script but my project was with a simple static html site with page name .html extension.

I started to write a script with the help of Internet and even not using JQuery.

See the picture for Paypal button

 You need some code to furnish the Paypal Form to accept coupon code and need to add Enter Coupon Code Fileds and Apply Code button.

And to handel this you need Javascript

/**
    PayPal Form
**/
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top" onsubmit="this.target = 'paypal'; return ReadForm (this);" onkeypress="return event.keyCode != 13;">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="[email protected]">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="This is my First Product">
<input type="hidden" name="item_number" value="602">
<input type="hidden" name="button_subtype" value="services">
<input type="hidden" name="no_note" value="0">
<input type="hidden" name="cn" value="Add special instructions to the seller:">
<input type="hidden" name="no_shipping" value="2">
<input type="hidden" name="undefined_quantity" value="1">
<input type="hidden" name="cancel_return" value="http://WEBSITE.com/PRODUCTS.html">
<input type="hidden" name="weight_unit" value="lbs">
<input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynow_LG.gif:NonHosted">
<table>
<tr>
  <td><input type="hidden" name="on0" value="Colors">
    Colors</td>
</tr>
<tr>
  <td><select name="os0">
      <option  value="Black">Black $99.00 USD</option>
      <option  value="Blue">Blue $99.00 USD</option>
      <option value="Purple">Purple $99.00 USD</option>
      <option  value="Pink">Pink $129.00 USD</option>
    </select></td>
</tr>
<tr>
  <td><input type="hidden" name="currency_code" value="USD">
    <input type="hidden" name="option_select0" value="Black">
    <input type="hidden" name="option_amount0" value="99.00">
    <input type="hidden" name="option_select1" value="Blue">
    <input type="hidden" name="option_amount1" value="99.00">
    <input type="hidden" name="option_select2" value="Purple">
    <input type="hidden" name="option_amount2" value="99.00">
    <input type="hidden" name="option_select3" value="Pink">
    <input type="hidden" name="option_amount3" value="129.00">
    <input type="hidden" name="baseamt" value="99.00" />
    <input type="hidden" name="option_index" value="0">
    <input type="hidden" name="basedes" value="1 Item" />
    Enter Coupon code<br>
    <input type="text" size="10" name="coupcode"  />
    <input id="ChkCodeOne" type="button" value="Apply Code" onclick="coupval = this.form.coupcode.value;  ChkCoup();" />
    <br>
    <br>
    <input id="BuyNowOne" type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" border="0" name="submit"  alt="PayPal - The safer, easier way to pay online!" >
    <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"></td>
</tr>
</table>
</form>

Now Add Javascript to make it functional

## JS ##
<script type="text/javascript">  
<!--
var discnt = 0;       // no default percent discount
var coupons = new Array (    // place to put coupon codes
  "FKAT15",                  // 1st coupon value - comma seperated
  "FLAT50"                  // 2nd coupon value - add all you want
     
);
var coupdc  = new Array (    // place to put discounts for coupon vals
  15,
  10
);
var coupval = "(blanket)";   // what user entered as coupon code

function ChkCoup () {        // check user coupon entry
var i;
  discnt = 0;                
  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! nn" + discnt + "%" + 
    " Discount now in effect.");
  document.getElementById("ChkCodeOne").onclick = function() {
    this.disabled = true;
  }   
   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,amt1,amt2,amt3,amt4,amt5,des ;
 amt = obj1.option_amount0.value*1.0;   
 amt1 = obj1.option_amount1.value*1.0;   // base amount
 amt2 = obj1.option_amount2.value*1.0;   // base amount
 amt3 = obj1.option_amount3.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));
 amt1 = Dollar (amt1 - (amt1 * discnt/100.0));
 amt2 = Dollar (amt2 - (amt2 * discnt/100.0));
 amt3 = Dollar (amt3 - (amt3 * discnt/100.0));
 amt4 = Dollar (amt4 - (amt4 * discnt/100.0));
 amt5 = Dollar (amt5 - (amt5 * discnt/100.0));
 des = des + ", " + discnt + "%" + "dis, COUP = " + coupval;
 document.getElementById("BuyNowOne").style.display = 'none'; // Disable Button After Click
   
  }

  obj1.option_amount0.value = Dollar (amt);
  obj1.option_amount1.value = Dollar (amt1);
  obj1.option_amount2.value = Dollar (amt2);
  obj1.option_amount3.value = Dollar (amt3);
  obj1.option_amount4.value = Dollar (amt4);
  obj1.option_amount5.value = Dollar (amt5);
   
}
//-->
</script>  

Now when everything is done you need to add more coupon code and values. We have done some security mesures like it won’t take second time the coupon code.
PayPal will not work if the users hits apply coupon code more than one times and using enter the form will not submit so that duplicate coupon code will not applicable to it.

Now you need to minify or obfuscate the JavaScript code so that a user cannot view your coupon code by seeing your Page View Source Code.

I would prefer to use http://www.hightools.net/javascript-encrypter.php for JavaScript encryption. Just like Below Image

Any Comments most welcome the Javascript can be optimised or improved, I have tested and this method is working fine. You can use JQuery instead.

Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also

The Speed of Nine Planet

The speed of Sun : Astronomically the Sun is fixed and it is the planets which are moving …