How to integrate Paypal with PHP
I had to go through these steps and learned a lot, so I thought I should summarize my learning, so that its useful to others.
I will show you how to get your users make a payment to you from your site using Paypal and then show them a page where you can thank them for it. Hopefully you have a website that is up and running and have decided to use Paypal for handling payments.
Step 1. Enable IPIN NotificationLogin to your paypal account and upgrade it to a Premier account. By doing this, you enable IPIN (Instant Payment Notification) support for your account. This is useful to check if a successful payment has been made.
Once your account is upgraded to Premier, visit the Profile menu under My Account. Under Selling Preference you will find Instant Payment Notification Preferences. Choose IPN Settings and then enter the Notification URL and Enable the IPIN service. The Notification URL will be the URL of the page that the user will process the results from Paypal IPIN.
Step 2. Set up Auto-Return Page (Optional but Good to have)Also setup the Auto-Return page, this is the page that will thank the user for the payment made. You will find this option in the Website Payment Preferences link under the Selling Preferences tab of your Profile. Enable Auto-Return and enter the URL on your website.
Step 3. Create the Payment ButtonUnder the Merchant Services Menu, look for PayPal Website Payments Standard and under the Key features tab, click on the Buy Now Buttons link.
Enter the Item Name, the Price and the currency. The rest of the fields are optional. The item name will be the one visible to the user when he visits paypal after clicking the button on your site, so make it descriptive. Click on Create Button.
Copy the code that is generated and paste in on a page in your site that will be used to obtain payments.
Step 4. Create the payment processing pagesNow create the page specified in your Notification URL (Step 1) and enter the following:
<?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); //for live
//$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30); //sandbox
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// check the payment_status is Completed
if(strcmp($payment_status,"Completed") == 0) {
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
if(strcmp($payment_status,$my_email) == 0) {
// check that payment_amount/payment_currency are correct
if($payment_currency == 'USD' && $payment_amount == "10.00"){
// process payment
$to = $my_email; //enter your email here
foreach ($_POST as $key => $value) { $body .= "\n$key: $value"; }
mail($to, $subject, $body, $headers);
}
}
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?>You need to add a check for an existing transaction ID. This would require you to store the transaction ID in the db and then make a call to the db to ensure that the transaction ID is not already used.
The above code will send you the details of all the fields received from the IPIN. You could replace this with code that you need to handle the payment like adding credits to a member, gold, etc.
Your auto-return page (refer to Step 2) should contain something similar:
“Thank you for your payment. Your transaction has been completed, and a receipt for your purchase has been emailed to you. You may log into your account at
www.paypal.com to view details of this transaction.”
MY TIP: Register for a Developer account and test the IPIN feature using the sandbox feature, before you use it in production.
: You can add a hidden field in the code for the button, to store a user id of the user. The name of the field that I used was 'custom'. This will be returned to your notification URL and you could use it for crediting users.
All feedback/comments are welcome.
I was not sure if this belonged in this section or the Revenue & Promotion section. The Mods may move it if they see fit.