Skip to content

Instantly share code, notes, and snippets.

@Serhioromano Serhioromano/paypal.php
Last active Dec 17, 2015

Embed
What would you like to do?
Convert PayPal Adaptive payment IPN into valide array.
public function decodePayPalIPN()
{
$raw = file_get_contents("php://input");
// sometimes we already have & in returnUrl or cancelUrl
$raw = str_replace("&", '^^^', $raw);
$post = array();
$pairs = explode('&', $raw);
foreach($pairs as $pair)
{
list($key, $value) = explode('=', $pair, 2);
$key = urldecode($key);
$value = urldecode($value);
// This is look for a key as simple as 'return_url' or as complex as 'somekey[x].property'
preg_match('/(\w+)(?:\[(\d+)\])?(?:\.(\w+))?/', $key, $key_parts);
switch(count($key_parts))
{
case 4:
// Original key format: somekey[x].property
// Converting to $post[somekey][x][property]
$post[$key_parts[1]][$key_parts[2]][$key_parts[3]] = $value;
break;
case 3:
// Original key format: somekey[x] Converting to $post[somkey][x]
$post[$key_parts[1]][$key_parts[2]] = $value;
break;
default:
// No special format
$post[$key] = str_replace('^^^', '&', $value);
break;
}
}
return $post;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.