Discount Code

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.

Overview

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.

Javascript

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() 
{
/* Newtek Gateway API uses UMsubtotal, UMdiscount, UMshipping, UMtax as total amounts for the order.
  UMamount = UMsubtotal + UMshipping - UMdiscount + UMtax
  Newtek Gateway does not automatically calculate UMamount for us.
  Instead of using a simmpler calculation
  Qty * (Base Amount - Discount) * (1 + Tax) + Shipping = Amount,
  we'll use Newtek Gateway 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>

HTML

Find:

<body>

And replace it with:

<body 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]">

Replace with:

<input type="hidden" name="UMdiscount" value="[UMdiscount]">

Find:

<div class="form-group">
    <span class="col-form-label col-sm-4 col-xs-12 ">Order Amount</span>
    <div class="col-md-8 col-xs-12 ">
        <input id="" name="UMamount" class="form-control" type="text" placeholder="Order Amount" value="[UMamount]">
    </div>
</div>

And replace it with:

<div class="form-group">
    <span class="col-form-label col-sm-4 col-xs-12 ">Order Date</span>
    <div class="col-md-8 col-xs-12 ">
        <input id="UMorderdate" name="UMorderdate" class="form-control displayonly" type="text"  readonly="readonly" placeholder="Order Date" value="[UMorderdate]">
    </div>
</div>
<div class="form-group">
    <span class="col-form-label col-sm-4 col-xs-12 ">Amount</span>
    <div class="col-md-8 col-xs-12 ">
        <input type="text" readonly="true" name="UMsubtotal" value="20.00">
    </div>
</div>
<div class="form-group">
    <span class="col-form-label col-sm-4 col-xs-12 ">Enter Discount Code</span>
    <div class="col-md-8 col-xs-12 ">
        <input type="text" name="DiscountCode" onChange="calculateDiscount()">
        </input>
    <div id="discountmessage">&nbsp;
    </div>
</div>
</div>
<div class="form-group">
<span class="col-form-label col-sm-4 col-xs-12 ">Discount</span>
<div class="col-md-8 col-xs-12 ">
    <div id="discount">&nbsp;
    </div>
</div>
</div>
<div class="form-group">
<span class="col-form-label col-sm-4 col-xs-12 ">Total Charge</span>
<div class="col-md-8 col-xs-12 ">
    <input type="hidden" name="UMamount" value="[UMamount]">
    <div id="totalamount">&nbsp;
    </div>
</div>
</div>