Tuesday, April 26, 2011

Mfarhanonline:Payment System with Paypal

Fav Tag:

Mfarhanonline Dev & Design: I had received a comment on my previous post Payment system with Paypal about injecting wrong product price values via third party site using FORM, this is a valid point. So that I had updated my previous post code "success.php" re-confirming the product price details before payment success message and added new field class 'currecy type' on "products" table.

I received a tutorial requests from my reader that asked to me how to implement payment gateway system with Paypal API. In this post I want to explain how to work with Paypal Sandbox test accounts for payment system development and sending arguments while click buy now button. It's simple and very easy to integrate in your web projects.

 

Payment System

 

Sample database design for Payment system. Contains there table users, products and sales.

Payment process database design

Users

CREATE TABLE `users` (

`uid` int(11) AUTO_INCREMENT PRIMARY KEY,

`username` varchar(255) UNIQUE KEY,

`password` varchar(255),

`email` varchar(255) UNIQUE KEY,

)

Products

CREATE TABLE `products`

(

`pid` int(11) AUTO_INCREMENT PRIMARY KEY,

`product` varchar(255),

product_img` varchar(100),

`price` int(11),

`currency` varchar(10),

)

Sales

CREATE TABLE `sales`

(

`sid` int(11) AUTO_INCREMENT PRIMARY KEY,

`pid` int(11),

`uid` int(11),

`saledate` date,

`transactionid` varchar(125),

FOREIGN KEY(uid) REFERENCES users(uid),

FOREIGN KEY(pid) REFERENCES products(pid)

)

Step 1

Create a Paypal Sandbox account at https://developer.paypal.com/

Step 2

Now create test accounts for payment system. Take a look at Sandbox menu left-side top Sandbox->Test Accounts

Creating Paypal Test Account

 

Step 3

Here I have created two accounts Buyer (personal) and Seller (merchant/business)

Paypal test accounts

products.php

Contains PHP code. Displaying records from products table product image, product name and product price. Here you have to give your business(seller) $paypal_id id. Modify paypal button form return and cancel_return URLs.

<?php

session_start();

requiredb_config.php‘;

$uid=$_SESSION['uid'];

$username=$_SESSION['username'];

$paypal_url=’https://www.sandbox.paypal.com/cgi-bin/webscr‘; // Test Paypal API URL

$paypal_id=’your_seller_id‘; // Business email ID

?>

<body>

<h2>Welcome, <?php echo $username;?></h2>

<?php

$result = mysql_query(“SELECT * from products“);

while($row = mysql_fetch_array($result))

{

?>

<img src=”images/<?php echo $row['product_img'];?>” />

Name: <?php echo $row['product'];?>

Price: <?php echo $row['price'];?>$

// Paypal Button

<form action=’<?php echo $paypal_url; ?>‘ method=’post‘ name=’form<?php echo $row['pid']; ?&gt;’>

<input type=’hidden‘ name=’business‘ value=’<?php echo $paypal_id; ?>‘>

<input type=’hidden‘ name=’cmd‘ value=’_xclick‘>

<input type=’hidden‘ name=’item_name‘ value=’<?php echo $row['product'];?>‘>

<input type=’hidden‘ name=’item_number‘ value=’<?php echo $row['pid'];?>‘>

<input type=’hidden‘ name=’amount‘ value=’<?php echo $row['price'];?>‘>

<input type=’hidden‘ name=’no_shipping‘ value=’1‘>

<input type=’hidden‘ name=’currency_code‘ value=’USD‘>

<input type=’hidden‘ name=’cancel_return‘ value=’http://yoursite.com/cancel.php‘>

<input type=’hidden‘ name=’return‘ value=’http://yoursite.com/success.php‘>

<input type=”image” src=”https://paypal.com/en_US/i/btn/btn_buynowCC_LG.gif” name=”submit“>

</form>

 

<?php

}

?>

</body>

success.php

Paypal payment success return file. Getting Paypal argument like item_number. Paypal data success.php?tx=270233304D340491B&st=Completed&amt=22.00&cc=USD&cm=&item_number=1

<?php

session_start();

requiredb_config.php‘;

$uid = $_SESSION['uid'];

$username=$_SESSION['username'];

$item_no = $_GET['item_number'];

$item_transaction = $_GET['tx']; // Paypal transaction ID

$item_price = $_GET['amt']; // Paypal received amount

$item_currency = $_GET['cc']; // Paypal received currency type

//Getting product details

$sql=mysql_query(“select product,price,currency from producst where pid=’$item_no’“);

$row=mysql_fetch_array($sql);

$price=$row['price'];

$currency=$row['currency'];

//Rechecking the product price and currency details

if($item_price==$price && item_currency==$currency)

{

$result = mysql_query(“INSERT INTO sales(pid, uid, saledate,transactionid) VALUES(‘$item_no’, ‘$uid’, NOW(),’$item_transaction’)“);

if($result)

{

echo<h1>Welcome, $username</h1>“;

echo<h1>Payment Successful</h1>“;

}

}

else

{

echo “Payment Failed”;

}

?>

 

cancel.php

Paypal API cancel_return file.

<?php

session_start();

$username=$_SESSION['username'];

echo<h1>Welcome, $username</h1>“;

echo<h1>Payment Canceled</h1>“;

?>

 

Step 4

When your web application test payment system workflow is completed. Change the form action development API URLs to original API URLs and give valid $paypal_id seller email id.

$paypal_url=’https://www.sandbox.paypal.com/cgi-bin/webscr‘;

to

$paypal_url=’https://www.paypal.com/cgi-bin/webscr‘;

Share and Enjoy: Print Digg Sphinn del.icio.us Facebook Mixx Google Bookmarks Blogplay Add to favorites BarraPunto Bitacoras.com BlinkList blogmarks Blogosphere News blogtercimlap connotea Current Design Float Diggita Diigo DotNetKicks DZone eKudos email Fark Faves Fleck FriendFeed FSDaily Global Grind Gwar HackerNews Haohao HealthRanker HelloTxt Hemidemi Hyves Identi.ca IndianPad Internetmedia Kirtsy laaik.it LaTafanera LinkaGoGo LinkArena LinkedIn Linkter Live Meneame MisterWong MisterWong.DE MOB MSN Reporter muti MyShare MySpace N4G Netvibes Netvouz NewsVine NuJIJ PDF Ping.fm Posterous Propeller QQ书签 Ratimarks Rec6 Reddit RSS Scoopeo Segnalo SheToldMe Simpy Slashdot Socialogs SphereIt StumbleUpon Suggest to Techmeme via Twitter Technorati ThisNext Tipd Tumblr Twitter Upnews viadeo FR Webnews.de Webride Wikio Wikio FR Wikio IT Wists Wykop Xerpi Yahoo! Bookmarks Yahoo! Buzz Yigg 豆瓣 豆瓣九点

http://www.mfarhanonline.com/2011042623971/payment-system-with-paypal/

0 comments :

Popular Posts