This is an example of how to add a discount code to the payment form. It assumes no other changes have been made to the payment form.
The strategy is to validate the discount code that the customer enters into the form. If the discount code passes validation, we subtract the amount of discount from the total amount. We use UMdiscount variable for best integration with reports and other API calls.
In this example, the base amount is $20. The discount used is “discount” with a discount of $5.00.
Add the following code to the header:
<script type="text/javascript"> <!-- //updateTotal function used to update UMamount if parts of the form require an update to amount. //In this case, if someone enters LA for UMbillstate after they enter quantity. function updateTotal() { if (document.epayform.UMamount.value > 0) { calculateTotal(); } } function calculateDiscount() { var discountInput = document.epayform.DiscountCode.value; var discountindividual = 5.00; var discounttotal = 0; var discountmessage = ""; if (discountInput=="discount") { discounttotal=discountindividual; discountmessage = "discount message"; } else if (discountInput != "") { discounttotal=0; discountmessage = "Not a valid discount code"; } else { discounttotal=0; discountmessage = ""; } document.epayform.UMdiscount.value = roundCurrency(discounttotal); document.getElementById('discountmessage').innerHTML = discountmessage; calculateTotal(); } function calculateTotal() { /* USAePay API uses UMsubtotal, UMdiscount, UMshipping, UMtax as total amounts for the order. UMamount = UMsubtotal + UMshipping - UMdiscount + UMtax USAePay does not automatically calculate UMamount for us. Instead of using a simmpler calculation Qty * (Base Amount - Discount) * (1 + Tax) + Shipping = Amount, we'll use USAePay variables to better tie in with it's API. */ //calculate subtotal var subtotal = document.epayform.UMsubtotal.value; subtotal = roundCurrency(subtotal); var discounttotal = document.epayform.UMdiscount.value; if (discounttotal != 0) { document.getElementById('discount').innerHTML = "$" + currencyFormatted(discounttotal); } var total = subtotal - discounttotal; total = roundCurrency(total); document.epayform.UMamount.value = total ; document.getElementById('totalamount').innerHTML = "$" + currencyFormatted(total) ; } //round Currency function roundCurrency(amount) { var i = Math.abs(amount); i = Math.round (i * 100)/100; return i; } //format Currency function currencyFormatted(amount) { var i = parseFloat(amount); if(isNaN(i)) { i = 0.00; } var minus = ''; if(i < 0) { minus = '-'; } i = Math.abs(i); i = parseInt((i + .005) * 100); i = i / 100; s = new String(i); if(s.indexOf('.') < 0) { s += '.00'; } if(s.indexOf('.') == (s.length - 2)) { s += '0'; } s = minus + s; return s; } //--> </script>
Find
<body link="#000080" vlink="#000080" alink="#000080" text="#000000" bgcolor="#D4D7E4">
and replace it with
<body link="#000080" vlink="#000080" alink="#000080" text="#000000" bgcolor="#D4D7E4" onload="calculateTotal()">
Find the part of the form with the hidden inputs. It should look like this:
<input type="hidden" name="UMsubmit" value="1"> <input type="hidden" name="UMkey" value="[UMkey]"> <input type="hidden" name="UMredirDeclined" value="[UMredirDeclined]"> <input type="hidden" name="UMredirApproved" value="[UMredirApproved]"> <input type="hidden" name="UMhash" value="[UMhash]"> <input type="hidden" name="UMcommand" value="[UMcommand]"> <input type="hidden" name="UMamount" value="[UMamount]"> <input type="hidden" name="UMtax" value="[UMtax]"> <input type="hidden" name="UMinvoice" value="[UMinvoice]"> <input type="hidden" name="UMcustid" value="[UMcustid]"> <input type="hidden" name="UMrecurring" value="[UMrecurring]"> <input type="hidden" name="UMaddcustomer" value="[UMaddcustomer]"> <input type="hidden" name="UMbillamount" value="[UMbillamount]"> <input type="hidden" name="UMcustreceipt" value="[UMcustreceipt]"> <input type="hidden" name="UMschedule" value="[UMschedule]"> <input type="hidden" name="UMnumleft" value="[UMnumleft]"> <input type="hidden" name="UMstart" value="[UMstart]"> <input type="hidden" name="UMexpire" value="[UMexpire]"> <input type="hidden" name="UMdescription" value="[UMdescription]"> <input type="hidden" name="UMechofields" value="[UMechofields]"> <input type="hidden" name="UMformString" value="[UMformString]">
Delete:
<input type="hidden" name="UMamount" value="[UMamount]">
Add the following code below:
<input type="hidden" name="UMdiscount" value="[UMdiscount]">
Find
<tr> <td bgcolor="#F0F0F0" width="234" align="right"><font size="2" face="Verdana">Order Amount:</font></td> <td bgcolor="#F0F0F0" width="450">[UMamount] </td> </tr>
and replace it with
<tr> <td bgcolor="#F0F0F0" width="234" align="right"><font size="2" face="Verdana">Amount:</font></td> <td bgcolor="#F0F0F0" width="450"><input type="hidden" name="UMsubtotal" value="20.00" size=10>$20.00 </td> </tr> <tr> <td bgcolor="#F0F0F0" width="234" align="right"><font size="2" face="Verdana">Enter Discount Code: </font></td> <td bgcolor="#F0F0F0" width="450"><input type="text" name="DiscountCode" size=11 onChange="calculateDiscount()"></input> <div id="discountmessage"> </div></td> </tr> <tr> <td bgcolor="#F0F0F0" width="234" align="right"><font size="2" face="Verdana">Discount:</font></td> <td bgcolor="#F0F0F0" width="450"><div id="discount"> </div> </td> </tr> <tr> <td bgcolor="#F0F0F0" width="234" align="right"><font size="2" face="Verdana">Total Charge:</font></td> <td bgcolor="#F0F0F0" width="450"><input type="hidden" name="UMamount" value="[UMamount]"><div id="totalamount"> </div> </td> </tr>