Search code examples
phphtmlpaypalpaypal-buttons

Is it possible to add discount code to a paypal button?


I'm looking to add a discount code to this Paypal button but don't know how to go about doing it. Can anyone help or suggest how too, I just need it so a customer can enter a the code and receive 10% off the price

The code is below for the button

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<p>Please click on the link to pay</p>
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="upload" value="1" />
<input type="hidden" name="business" value="<?php echo C_OUR_EMAIL; ?>" />
<input type="hidden" name="first_name" value="<?php echo   strfordisp($ofirstname); ?>" />
<input type="hidden" name="last_name" value="<?php echo strfordisp($olastname); ?>" />
<input type="hidden" name="address1" value="<?php echo strfordisp($theorder["oaddress"]); ?>" />
<input type="hidden" name="address2" value="<?php echo strfordisp($theorder["oaddress2"]); ?>" />
<input type="hidden" name="address3" value="<?php echo strfordisp($theorder["oaddress3"]); ?>" />
<input type="hidden" name="city" value="<?php echo strfordisp($theorder["otown"]); ?>">
<input type="hidden" name="zip" value="<?php echo strfordisp($theorder["opostcode"]); ?>" />
<?php $orderdets = mysql_query("select * from c4d_orderitems where orderid='" . $_SESSION["db_order"] . "' and confirm");
$iloop = 1;
while ($orderrow = mysql_fetch_array($orderdets))
{
$itemdesc = $orderrow["itemtypedesc"];
$itemdesc .= " to " . $orderrow["dpostcode"];
$itemprice = $orderrow["cost"] + $orderrow["surcharge"] +     $orderrow["insurancecost"];
?>
<input type='hidden' name="item_name_<?php echo $iloop; ?>" value='<?php   echo strfordisp($itemdesc); ?>' />
<input type='hidden' name="item_number_<?php echo $iloop; ?>" value='<?php echo $orderrow["itemtype"]; ?>' />
<input type='hidden' name="amount_<?php echo $iloop; ?>" value='<?php
  // pctrends
  if ((strtoupper($ofirstname)=="PCTRENDSTEST") ||   (strtoupper($olastname)=="PCTRENDSTEST") || (substr($_SERVER['REMOTE_ADDR'],0,11)=="82.152.55.1"))
echo("0.01");
else echo $itemprice;
?>' />
<input type='hidden' name="quantity_<?php echo $iloop; ?>" value='1' />

<?
    $iloop++;
}
?>
<input type="hidden" name="return" value="<?php echo C_SITE_ROOT; ?>stage7.php" />
<input type="hidden" name="cancel-return" value="<?php echo C_SITE_ROOT; ?>order-cancel.php" />
<input type="hidden" name="notify_url" value="<?php echo C_SITE_ROOT; ?>paypal.php" />
<input type="hidden" name="rm" value="2" />
<input type="hidden" name="invoice" value="<?php echo $_SESSION["db_order"]; ?>" />
<input type="hidden" name="currency_code" value="GBP" />
<input type="hidden" name="no-shipping" value="1" />
<input type="hidden" name="button_subtype" value="products" />
<input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest" />
<input type="hidden" name="country" value="GB" />
<p class='fmenu'><input type="image" src="https://www.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"></p>
</form>

Solution

  • You must check for the discount-code validity (with AJAX) and make the appropriate calculations to apply the discount to the amount variable (with JavaScript) before to send the payment notification.

    The user writes the discount-code insiede the custom variable where you can store whatever you want for your own tracking purposes.

    // Check here on keypress with JS and AJAX for discount code validity
    <input type="text" name="custom" value="" />
    

    If the discount-code is verified, apply the discount to the amount variable. Something like this:

    //  Javascript and jQuery
    if(verified == true) {
        var discounted = $("[name='amount']").val() - ($("[name='amount']").val()/100)*10);
        $("[name='amount']").val(discounted);
    }
    else {
        alert("Discount code not found");
    }
    

    Then when you send the payment and get the information from paypal (the IPN response) process it like so:

    $discountCode = $_POST["custom"];
    // ... Discount-code validity check ...
    $discountedAmount = $originalAmount - (($originalAmount/100)*10);
    // And then check if the paid amount corresponds to the due amount
    if($discountedAmount == $_POST["amount"])