How to integrate PayPal IPN with your website

After signing up for PayPal IPN with PayPal you have to start integration with your website. If you have worked with PayPal before and integrated PayPal Standard then most of the procedure is similar apart from minor modifications. For those of you who have just started integration with PayPal IPN then no need to panic. Its pretty simple.

First you have to write the <form> code in the file that will be submitting the order information to PayPal from your website. You can use the following code snippet and change it accordingly. Depending on your requirements you may need more fields for integration however the code below fulfills the basic requirements

<form name=”frmPal” action=”https://www.paypal.com/cgi-bin/webscr” method=”post”>
<input type=”hidden” name=”cmd” value=”_cart”>
<input type=”hidden” name=”business” value=”<merchant_email_address>”>
<input type=”hidden” name=”invoice” value=”<unique_number_to_identify_transaction>”>
<input type=”hidden” name=”currency_code” value=”<3_digit_currency_code>”>
<input type=”hidden” name=”handling_cart” value=”<handling_charges_if_applicable>”>
<input type=”hidden” name=”item_name_1″ value=”<product_name>”>
<input type=”hidden” name=”item_number_1″ value=”<product_number>”>
<input type=”hidden” name=”quantity_1″ value=”<product_quantity>”>
<input type=”hidden” name=”amount_1″ value=”<product_amount>”>
<input type=”hidden” name=”notify_url” value=”<notification_url>”>
<input type=”hidden” name=”return” value=”<success_page_url>”>
<input type=”hidden” name=”cancel_return” value=”<failure_page_url>”>
</form>

You may notice the notify_url field name in the form above. This is the URL which PayPal will call to verify that the information passed to it is correct and genuine. Please note that this URL must be accessible other wise PayPal IPN will not work. You can use the following code snippet as is. The following code receives the information posted by PayPal, saves the information to a text file and then opens a socket to PayPal and notifies it.

<?php
$req = ‘cmd=_notify-validate’;

$path=”;
$timestamp=date(‘y-m-d–H-i-s’);
$strFileName = ‘payapl_ipn_return_’. $timestamp.’.txt’;
$FILE=fopen($strFileName,’a’);
if (is_writable($strFileName)) {
foreach ($_POST as $key=>$value)
{
fwrite($FILE, $key.”: “.$value.”\n”);
}
}
else {
echo “File could not be written”;
exit;
}

// 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(‘<Secure_PayPal_URL>’, 443, $errno, $errstr, 30);

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
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
}
else if (strcmp ($res, “INVALID”) == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?>

Once PayPal is notified and all other payment related matters are checked the customer is sent to the success page otherwise to the failure page.

Hope the above helps

Tagged with: , ,

Leave a Reply

Your email address will not be published.