PayPal Buy Now Buttons with Coupon Codes
This article describes how to accept coupon codes with PayPal Buy Now buttons.
Note: The technique described will only work with one-price, no-selection buttons. Buttons with dropdown or other methods of selecting price or item variations won't work with this technique.
The place where the customer enters the coupon code looks something like this:
Coupon Code:
When a valid coupon code is provided, the user is redirected to a PayPal payment page with the coupon price for the product. Otherwise, the user is redirected to a PayPal page with the regular price.
Implementing Coupon Codes for Buy Now Buttons
Overview
When a person clicks the Buy Now button of the coupon code form, the form information is sent to a PHP script. The PHP script authenticates the coupon code the person entered.
If the coupon code is valid, the PHP script redirects the browser to a PayPal payment page with the coupon price for the product.
If the coupon code is invalid or no coupon code was provided, the browser is redirected to a PayPal payment page with the regular price.
Step-by-step
The PHP Script
Set up the PHP script, first. Here is the code. Three places need to be customized. Instructions are below the code.
<?php // The "<" and "?php" beginning this line need to be the first characters of this file. No space or other characters before it. /* PayPal Coupon Code Processor Version 1.2 December 1, 2019 Version 1.0 published July 28, 2011 Version 1.1 published August 27, 2011, Added ability to process forms and links with method GET. Version 1.2 published December 1, 2019, fixed typo. Will Bontrager Software, LLC https://www.willmaster.com/ Will Bontrager Software LLC This software is provided "AS IS," without any warranty of any kind, without even any implied warranty such as merchantability or fitness for a particular purpose. Will Bontrager Software, LLC grants you a royalty free license to use or modify this software provided this notice appears on all copies. */ /* Customization Area */ // Three places to customize. // Place 1 -- // Specify the PayPal URL for the regular product price. $RegularProductPriceURL = "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=67FTQDYVE3L96"; // Place 2 -- // Specify the PayPal URL for the coupon code price. $CouponCodePriceURL = "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5ALSH3MSKC59G"; // Place 3 -- // Between the quotation marks, list valid coupon codes, one coupon code per line. // Coupon codes are case-insensitive ("ABC123" is handled as if it was "abc123"). $ValidCouponCodes = " ABC 123456 44*DOGS 12&24 twelve "; /* End of Customization Area */ /////////////////////////////// $Valid = array(); $ValidCouponCodes = trim($ValidCouponCodes); foreach( preg_split('/\s+/',$ValidCouponCodes) as $code ) { $Valid[strtolower(trim($code))] = 1; } if( empty($_POST['ccode']) and isset($_GET['ccode']) ) { $_POST['ccode'] = $_GET['ccode']; } $SubmittedCode = isset($_POST['ccode']) ? strtolower(trim($_POST['ccode'])) : ''; $RedirectURL = ($SubmittedCode and $Valid[$SubmittedCode]) ? $CouponCodePriceURL : $RegularProductPriceURL; header("Location: $RedirectURL"); exit; ?>
Copy the above PHP code and save it with a plain text processor as paypalcoupon.php (or other file name with .php extension).
At about line 20, you'll see the customization section. There are three things to customize.
-
The first customization is to specify the URL for the regular product price. This is how to get the URL:
If you already have a Buy Now button for the regular product price —
Here is the format of the URL.
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=BUTTONID
In the URL, replace BUTTONID with the actual button ID.
The button ID can be found in the Buy Now button code as the value for field name="hosted_button_id". The value is between the quotation marks of that field's value= attribute. (As of this writing, the button ID PayPal generates has 13 characters composed of capital letters and numbers.)
If you do not have a Buy Now button for the regular product price —
Create a Buy Now button for the regular price. You'll find a Buy Now button link at the Merchant Services tab after logging in at the PayPal website.
When you have made the button code, you'll see a tab panel with two tabs. One says "Website" and the other says "Email". Use the Email tab. It contains the URL for the regular product price.
Now that you have the PayPal URL for the regular product price, replace the example URL in the PHP code with your own PayPal URL between the quotation marks following $RegularProductPriceURL =
-
The second customization is to specify the URL for the coupon price. This is how to get the URL:
If you already have a Buy Now button for the coupon price —
The format of the URL is the same as for the regular product price.
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=BUTTONID
In the URL, replace BUTTONID with the actual button ID.
The button ID can be found in the Buy Now button code as the value for field name="hosted_button_id". The value is between the quotation marks of that field's value= attribute.
If you do not have a Buy Now button for the coupon price —
Create a Buy Now button for the coupon price.
When you have made the button code, you'll see a tab panel with two tabs. One says "Website" and the other says "Email". The Email tab contains the URL for the coupon price.
Now that you have the PayPal URL for the coupon price, replace the example URL in the PHP code with your own PayPal URL between the quotation marks following $CouponCodePriceURL =
-
The last customization is to list the valid coupon codes.
Following $ValidCouponCodes = is a quotation mark. Several lines down is another quotation mark.
Between those two quotation marks are example coupon codes. Replace the examples with your own coupon codes.
Coupon codes are not case-sensitive. Capital letters are treated as if they are lower-case letters.
When the customization steps have been completed, upload the PHP script to your server somewhere that browsers can access. Make a note of its URL, as it will be needed for the coupon code form.
The Coupon Code Form
Here is example code for a coupon code form.
<form method="post" action="http://example.com/paypalcoupon.php"> Coupon Code: <input type="text" name="ccode" size="6"><br> <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"> </form>
In the form tag, replace http://example.com/paypalcoupon.php with the URL to the PHP script you installed in the previous implementation step.
The name="ccode" needs to remain as it is for the field where the coupon code is entered. Other attributes may be changed. And the "Coupon Code" text may be changed, as well.
If you wish, the type="image" field may be replaced with a different image or with a regular submit button.
When the coupon code form has been updated, publish it on your website.
paypalcoupon.php will work with forms using method POST or method GET. It will also work without a form using a link to the URL of paypalcoupon.php with ?ccode=CODE appended (where CODE is the coupon code).
Using Coupon Codes with Buy Now Buttons
When a buyer has a valid coupon code and types it into the field provided, the browser will be redirected to the PayPal payment page with the coupon price for the product. If an invalid code was typed in or no coupon code at all, the browser is redirected to the PayPal page with the regular price.
Valid codes are listed in the PHP script. Change them as desired.
You are now in a position to use coupon codes with PayPal Buy Now buttons.
Will Bontrager