so I have a custom store built off of PHP, JS and CSS and it sells virtual coins for a game and the coins are sent automatically by the script however I want to start offering discounts to customers via discounts codes and I currently came across this script that I have put together:
Javascript
<!--
var discnt = 0; // no default percent discount
var coupons = new Array ( // place to put coupon codes
"MavrkHD", "TheFUTBucks", "TOTW", "Mega", "LL"
);
var coupdc = new Array ( // place to put discounts for coupon vals
"10", "15", "7", "10", "20"
);
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 Sterling (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.amount.value*1.0; // base amount
des = obj1.basedes.value; // base description
if (discnt > 0) { // only if discount is active
amt = Sterling (amt - (amt * discnt/100.0));
des = des + " - " + discnt + "% off - COUP = " + coupval;
}
obj1.baseamt.value = Sterling (amt);
obj1.item_name.value = des;
}
//-->
</script>
PayPal Form Details
<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="<?=$functions->getPayPal();?>" />
<input type="hidden" name="item_name" value="Online Coin Currency" />
<input type="hidden" name="amount" id="amount" value="0.00" />
<input type="hidden" name="currency_code" value="GBP" />
<input type="hidden" name="baseamt" id="amount" value="0.00" />
<input type="hidden" name="basedes" value="Online Coin Currency (FUT)" />
<input type="hidden" name="custom" id="custom" value="">
<input type="hidden" name="return" value="RETURN LINK HERE";?>
<input type="hidden" name="notify_url" value="<?=HOST.'files/payments/paypal.php';?>">
<input type="hidden" name="cancel_return" value="<?=HOST.'cancelled';?>">
Enter Coupon code
<input type = "text" size = "10" name = "coupcode"; />
<input type="button" value="Apply code" onclick="coupval =this.form.coupcode.value; ChkCoup();" /><br/><br/>
<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>
So, it all works like sending information to PayPal I have one issue about PayPal displaying the calculated prices. For example: if I take out the id of amount and baseamt and just leave value and put a value of 12.99 and then go on my store and go through the process of buying the virtual coins and then get to the discount code area and proceed to paypal I will get a discount price to pay at paypal but when I use the id="amount" which is a variable for depending on how many coins the customer to buy it'll send that price to paypal, it doesn't discount the price and I have been trying endlessly to fix this! help? Thanks.