If you want to allow users on your website to make payments then PayPal is the best and the simplest option. Today we will see how to integrate PayPal payment gateway with your PHP online store. We will use the IPN(Instant Payment Notification) service of PayPal for processing the payments. PayPal sends responses for all payment actions(make payment, accept payment, refund payment, cancel payment) to the IPN URL set in your PayPal account settings. So lets go step by step and see how the whole process is done.
Step 1:Create a PayPal Sandbox account at https://developer.paypal.com/.
Step 2:Create two test accounts under this Sandbox account. One account will be for buyer and other for seller. You should select the ‘Preconfigured test accounts’ for simplicity.
Step 3: Enabling IPN
- To enable IPN, go to the ‘Test site’ of the seller account(see below screenshot).
- Now click on the ‘Profile’ menu link and then click on ‘Instant Payment Notification Preferences'(see below screenshot).
- Edit the IPN settings, add your IPN URL in the URL textbox and click on Save(see below screenshot).
Step 4: payments table
CREATE TABLE `payments`
(
`payment_id` int(11) AUTO_INCREMENT PRIMARY KEY,
`item_id` int(11),
`invoice_id` int(11),
`amount` float(10,2),
`amount_refunded` float(10,2),
`amount_refunded` float(10,2),
`transaction_id` varchar(125),
`status` varchar(10)
)
Step 5: The Products page
Our product page will contain a separate form for each product. The details in the forms will be product specific.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>WebSpeaks.in | Paypal Integration with PHP</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php
$paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
$paypal_id = 'bhardw_1325263769_biz@yahoo.co.in';//This is your seller id
$cancel_url = 'http://demos.frnzzz.com/paypal/store/index.php?action=cancel';
$return_url = 'http://demos.frnzzz.com/paypal/store/';
$products = array();
$products[] = array('id'=>156,'name'=>'Motorola 156 MX-VL','price'=>15, 'special_price'=>7, 'image'=>'laptop.gif');
$products[] = array('id'=>157,'name'=>'Iphone Apple','price'=>12, 'special_price'=>10, 'image'=>'p4.gif');
$products[] = array('id'=>158,'name'=>'Samsung Webcam','price'=>10, 'special_price'=>5, 'image'=>'p5.gif');
?>
<div id="main_container">
<div id="header">
<div id="logo">
<a href="index.html"><img src="images/webspeaks.gif" alt="" title="" border="0" width="260" height="140" /></a>
</div>
</div>
<div id="main_content">
<div class="center_content">
<div class="center_title_bar">Latest Products</div>
<?php
foreach($products as $product){
?>
<div class="prod_box">
<div class="top_prod_box"></div>
<div class="center_prod_box">
<div class="product_title"><a href="#"><?php echo $product['name'] ?></a></div>
<div class="product_img"><a href="#"><img src="images/<?php echo $product['image'] ?>" alt="" title="" border="0" /></a></div>
<div class="prod_price"><span class="reduce"><?php echo $product['price'] ?>$</span> <span class="price"><?php echo $product['special_price'] ?>$</span></div>
</div>
<div class="bottom_prod_box"></div>
<div class="prod_details_tab">
<form id='payment_form' action='<?php echo $paypal_url ?>' method='post' name='payform' />
<input type='hidden' name='business' value='<?php echo $paypal_id ?>' />
<input type='hidden' name='cmd' value='_xclick' />
<input type='hidden' name='amount' value='<?php echo $product['special_price'] ?>' />
<input type='hidden' name='no_shipping' value='0' />
<input type='hidden' name='item_name' value='<?php echo $product['name'] ?>' />
<input type='hidden' name='item_number' value='<?php echo $product['id'] ?>' />
<input type='hidden' name='invoice' value='WS-<?php echo $product['id'] ?>' />
<input type='hidden' name='currency_code' value='USD' />
<input type='hidden' name='cancel_return' value='<?php echo $cancel_url ?>' />
<input type='hidden' name='return' value='<?php echo $return_url ?>' />
<input type='image' src='https://www.sandbox.paypal.com/en_US/i/btn/btn_buynow_SM.gif' name='submit' style='display:{$display_button}' />
</form>
</div>
</div>
<?php
}
?>
</div>
</div>
</div>
<!-- end of main_container -->
</body>
</html>
Step 6: The payment process page
process_payment.php
<?php
$paypal_response = $_REQUEST;
//Check if it is the response from PayPal
if ( isset( $paypal_response ) ) {
$tx = $paypal_response['tx'];//Transaction code
$st = strtolower($paypal_response['payment_status']);//Payment status
$amount = $paypal_response['mc_gross'];//Amount
$cc = $paypal_response['mc_currency'];//Currency code
$item_number = $paypal_response['item_number'];//Item number
$invoice_id = $paypal_response['invoice'];
$user_email = 'bhardwajsonheight@gmail.com';
$site_name = 'WebSpeaks.in Test Store';
$eol = "rn";
$headers = 'MIME-Version: 1.0' . $eol;
$headers .= 'Content-type: text/html; charset=iso-8859-1' . $eol;
$headers .= 'From:' . $site_name . ' <store@webspeaks.in>'. $eol;
$headers .= 'Reply-To:' . $site_name . ' <store@webspeaks.in>' . $eol;
$headers .= 'Return-Path:' . $site_name . ' <store@webspeaks.in>' . $eol;
$eol = "rn<br />";
$thanks_text = $eol.$eol.'Thanks!'. $eol;
if($st == 'completed'){//Payment has been completed successfully
//Update payment in DB
mysql_query("UPDATE payments SET `status`='complete', `amount`='".$amount."' WHERE `invoice_id`='".$invoice_id." AND `item_id`='".$item_id."')");
//Send succes email to user
$email_tpl = 'Hello Buyer,' . $eol;
$email_tpl .= "We have received your payment of $amount $cc." . $eol;
$email_tpl .= "Thanks for the purchase." . $eol;
$email_tpl .= $eol . $thanks_text;
mail ($user_email, 'Payment Received', $email_tpl, $headers);
}elseif($st == 'pending'){//Payment is pending for approval
//Enter payment in DB
mysql_query("INSERT INTO payments (`item_id`, `invoice_id`, `amount`, `transaction_id`, `status`) VALUES ('".$item_number."', '".$invoice_id."', '".$amount."', '".$tx."', 'pending')");
$email_tpl = 'Hello Buyer,' . $eol;
$email_tpl .= "Your payment of $amount $cc is pending for approval. We will process it soon." . $eol;
$email_tpl .= $eol . $thanks_text;
mail ($user_email, 'Payment Pending', $email_tpl, $headers);
}elseif($st == 'refunded'){//Payment is refunded after approval
//Update payment in DB
mysql_query("UPDATE payments SET `status`='refunded', `amount_refunded`='".$amount."' WHERE `invoice_id`='".$invoice_id." AND `item_id`='".$item_id."')");
$email_tpl = 'Hello Buyer,' . $eol;
$email_tpl .= "Your payment of $amount $cc has been refunded." . $eol;
$email_tpl .= $eol . $thanks_text;
mail ($user_email, 'Payment Pending', $email_tpl, $headers);
}elseif($st == 'reversed'){//Payment is refunded before approval or it has been cancelled
$reason = strtolower($paypal_response['reason_code']);//Reason for payment reversal
if($reason == 'refund'){//Payment is refunded before approval
//Update payment in DB
mysql_query("UPDATE payments SET `status`='refunded', `amount_refunded`='".$amount."' WHERE `invoice_id`='".$invoice_id." AND `item_id`='".$item_id."')");
$email_tpl = 'Hello Buyer,' . $eol;
$email_tpl .= "Your payment of $amount $cc has been refunded." . $eol;
$email_tpl .= $eol . $thanks_text;
mail ($user_email, 'Payment Refunded', $email_tpl, $headers);
}elseif($reason == 'other'){//Payment has been cancelled
//Update payment in DB
mysql_query("UPDATE payments SET `status`='cancelled' WHERE `invoice_id`='".$invoice_id." AND `item_id`='".$item_id."')");
$email_tpl = 'Hello Buyer,' . $eol;
$email_tpl .= "Your payment of $amount $cc has been refunded." . $eol;
$email_tpl .= $eol . $thanks_text;
mail ($user_email, 'Payment Cancelled', $email_tpl, $headers);
}
}
}
Thank you for very detailed Explanation.
This is very very helpful for us.
This comment has been removed by the author.
Hi,
I am integrating PayPal for the first on WordPress site. I am following this article but unable to get the window which is supposed be opened when user clicks on his business account.
I have a seller and a buyer account(both are verified).
Please help.
@Chitrangada Rathore, This seems to be very much like a browser issue. Please check if your browser is blocking the popup windows. Please try the process in different browsers.
Thanks for replying Arvind,
Now the window and the further steps are visible to me (may be due to browser malfunctioning). Well, could you please tell me how to handle multiple sellers account with Paypal IPN API ?
As in my application, I have a site-admin who is providing an interface/plateform to other users for selling their products. So in this case, may be every seller is having an IPN handling URL.
Means for the site-admin I can create a handler PHP file. But what about for the other sellers? Will the same code work for them too or I need to create separate PHP handler file for all the sellers(I think this may result in overhead for mu application).
Please comment and suggest a better option.
Thanks
@Chitrangada Rathore, At first thought it seems to me that you must store the IPN urls for all the sellers in database and then based on the seller retrieve its IPN url. I think that you can use the same code for all sellers by this method.
Thanks a lot…
thanks, this is a very good example, I need this for my payment system.