Skip to content

Instantly share code, notes, and snippets.

@angelleye
Created August 13, 2016 10:24
Show Gist options
  • Save angelleye/27b25f9f3772f8a78660d06c8528fd2e to your computer and use it in GitHub Desktop.
Save angelleye/27b25f9f3772f8a78660d06c8528fd2e to your computer and use it in GitHub Desktop.
Forward PayPal IPN data to multiple URLs.
<?php
// Curl function to send POST data to a server.
function curl_request($url, $req, $ssl)
{
$curl_result=$curl_err='';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", "Content-Length: ".strlen($req)));
curl_setopt($ch, CURLOPT_HEADER , 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $ssl);
curl_setopt($ch, CURLOPT_TIMEOUT, 90);
$curl_result = curl_exec($ch);
$curl_err = curl_error($ch);
curl_close($ch);
return $curl_result;
}
/////////////////////////////////////////////////////
// Set config fields
$sandbox = isset($_POST['test_ipn']) ? true : false;
$ppHost = $sandbox ? 'www.sandbox.paypal.com' : 'www.paypal.com';
$ssl = $_SERVER['SERVER_PORT'] == '443' ? true : false;
// Loop through POST data and generate NVP string to return back to PayPal (or forward on to secondary listeners)
$req = '';
foreach ($_POST as $key => $value)
{
$req .= "&$key.=".urlencode(stripslashes($value));
}
// Forward IPN data to secondary IPN listener
curl_request('http://www.domain.com/paypal/ipn/ipn-listener.php', $req, $ssl);
curl_request('http://www.otherdomain.com/paypal/ipn/ipn-listener.php', $req, $ssl);
curl_request('http://www.yetanotherdomain.com/paypal/ipn/ipn-listener.php', $req, $ssl);
// Validate with PayPal using CURL is much the same way we forwarded data to our secondary listener.
$req = 'cmd=_notify-validate'.$req;
$curl_result = curl_request($ppHost.'/cgi-bin/webscr', $req, $ssl);
$valid = strpos($curl_result, "VERIFIED")!==false ? true : false;
// Process accordingly
if($valid)
{
// Process valid IPN
}
else
{
// Process invalid IPN
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment