How to create custom PayPal buttons using CSS, Images and Javascript
A custom PayPal button has long been one of the most wanted features, wherein web developers and designers wanted PayPal buttons to match their website styling and do more than just directing users to PayPal’s payment page. In this tutorial, you will learn how to create custom PayPal buttons.First things first – you need to have your PayPal button code handy. If you haven’t created a button yet, then do so by logging into your PayPal account. This is how your PayPal hosted button code will look like:-
1 2 3 4 5 6 |
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top"> <input type="hidden" name="cmd" value="_s-xclick"> <input type="hidden" name="hosted_button_id" value="1234567890"> <input type="image" src="https://www.paypalobjects.com/en_GB/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.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1"> </form> |
Copy the value of the input tag with name ‘hosted_button_id’. In the example above, it is 1234567890. Having the above code on your payment page will show the classic PayPal button with an image hosted in the url:- https://www.paypalobjects.com/en_GB/i/btn/btn_buynowCC_LG.gif
Style PayPal Button with CSS
Now we’ll see how you can add a style to the PayPal button so that it matches the color theme of your website. Here’s we are going to create a simple href based button which is blue in color:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<style> .buttonStyle { background-color:#00A6CC; border-color:#00A6CC; color:#ffffff; font-size: 15px; font-weight: bold; padding: 15px 30px; border-radius: 3px; line-height: 1.5em; text-decoration:none; } a.buttonStyle:hover { background-color:#00748E; text-decoration:underline; } </style> <a class="buttonStyle" href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=1234567890">Buy Now</a> |
The code above create a css class for the button/href which points to the URL https://www.paypal.com/cgi-bin/webscr and passes 2 argument values:-
- cmd: with value _s-xclick
- hosted_button_id: id of the button which you’ve extracted from the PayPal button code.
You can choose to directly copy the code above to your payment page, or make the desired changes wherever required.
Custom Images for PayPal buttons
Following the trails of the method above, you can also create custom PayPal buttons with the images of your choice. There are no restrictions for the size or dimensions of the image, and you can choose to use the extension in which you have it (.gif, .jpeg, .png, .svg or anything appropriate). Below is an example code of one such PayPal button created using a custom image:-
1 2 3 4 |
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=1234567890"> <img src="https://www.paypalobjects.com/webstatic/i/logo/rebrand/ppcom.svg" width="126px" height="33px"> </a> |
All we do is wrap an image that we want to show as a button, inside a href. The hyperlink reference points to a URL which in the example above is https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=1234567890. Specify the image source, its width and height and you are all set to create and host a PayPal button with a custom image.
PayPal button Javascript
To add some spice to the user experience, you can also choose to add some Javascript magic while trying to gel in a custom PayPal button on your payment page.
1 2 3 4 5 6 7 8 9 10 |
<script> function buyClicked() { //you can do some real good work here - like incrementing counters, changing styles, showing info/disclaimers/policy agreement etc. //Right now, we are limiting the actions to saying Thanks and directing users to the PayPal payment page alert("Thanks!"); window.location.href = "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=1234567890"; } </script> <a href="#" onclick="buyClicked();"><img src="https://www.paypalobjects.com/webstatic/i/logo/rebrand/ppcom.svg" width="126px" height="33px"></a> |
The code above will show a custom image PayPal button tied to a Javascript event which gets fired when the button is clicked. The user will be thanked with an alert message and will then be directed to PayPal. Make changes to the code as required and have fully functional and custom Javascript based PayPal buttons on your website.
awesome tip!!! i was trying to create paypal css buttons since ages!! didn’t knew it was this easy. lol