28 November 2011

Simple and secure payment gateway using paypal

Many freelancer and blogger has started to take payment using paypal for selling their service or product.Most of them use HTML hidden variable in their checkout form.But i believe it is not a safe method , since we can edit html code using firebug or any other console.

Secure Payment gateway with paypal


Today i am sharing a secure and simple way to accept payment with paypal just like advance payment gateway.


Step1: Designing your cart page.

In the step 1 your going to design the cart page in your website.Create a index.html or add the following code to your cart page


Simple and secure payment gateway using paypal

Html Code

<div id="container">
<div id="cart">
<h2>Check out Form</h2>
<form action="checkout.php" method="POST">
<table>
<tr>
<td>
Select the product you like to buy<br/>
<select name="ProductName">
<option value="0">Select</option>
<option value="1">Facebook Fan Page</option>
<option value="2">Wordpress Theme</option>
</select>
</td>
</tr>
<tr>
<td>
Select the amount of<br/>
<select name="amount">
<option value="0">Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="2">3</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" value="Pay with paypal"/></td>
</tr>
</table>
</form>
</div>
</div>


CSS Code

body{
font-family:'segoe ui';
}
select{
padding:5px;
margin:10px 0px;
}
#container{
margin:100px auto;
padding:20px;
width:450px;
background:#EEE;
border:1px solid #CCC;
}
#cart{
width:100%;
height:100%;
}

Step2:
In this part we are going to add the logic of the payment gateway.Since we are going to submit the form onload user can't change the amount value,which i believe as the secure one.Create a new page checkout.php and add the following code in it.

HTML Code
<body onload="return checkout();">
<h3>Processing..</h3>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" id="paypal">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="codingwoo@gmail.com">
<input type="hidden" name="item_name" value="<?php echo $item_name;?>">
<input type="hidden" name="quantity" value="<?php echo$_POST['amount'];?>">
<input type="hidden" name="item_number" value="<?php echo $_POST['ProductName'];?>">
<input type="hidden" name="amount" value="<?php echo $amount;?>">
<input type="hidden" name="no_shipping" value="0">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="lc" value="AU">
<input type="hidden" name="bn" value="PP-BuyNowBF">
</form>
</body>
<script type="text/javascript">
<!--
function checkout(){
document.forms["paypal"].submit();
}
//-->

Now add the php code to the top of the page in checkout.php


<?php
error_reporting(0);
switch ($_POST['ProductName'])
{
case 1:
$item_name = 'Facebook Fan Page';
$amount = $_POST['amount'] * 9;// add your product or service price
break;
case 2:
$item_name = 'Wordpress Theme';
$amount = $_POST['amount'] * 25;// add your product or service price
break;
}
?>

if you need a success message page for your payment method just add the following code to your checkout.php form.


<input type="hidden" name="return" value="your success message url">


I hope this tutorial is more useful , if you have any query or need to work for you contact us.

About Editorial Staff
Nathan Shri is a Web Developer ,Opensource lover,and the editor of the codingwoo based in India ,who loves to learn new stuffs and and share.You can be his friend on Facebook.

Post a Comment