paypal / ipn-code-samples

Other
562 stars 486 forks source link

IPN response VERIFIED, raw data on php://input fine, but nothing on $_POST #149

Open gagregori opened 5 years ago

gagregori commented 5 years ago

$response is verified I have all data from IPN Simulator on a txt file, but when I try to find a value in post nothing is there, and I have wrote post in txt file, used vardump, print_r it has nothing on Post, but why if there is all data on raw input? Please help me, I'm soo tired of trying to figure out alone.

gagregori commented 5 years ago

listener.txt

gagregori commented 5 years ago

<?php if ($_SERVER['REQUEST_METHOD'] != 'POST') { header('Location: index.php'); exit(); }

$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();

foreach ($raw_post_array as $keyval) {
    $keyval = explode('=', $keyval);
    if (count($keyval) == 2) {
        // Since we do not want the plus in the datetime string to be encoded to a space, we manually encode it.
        if ($keyval[0] === 'payment_date') {
            if (substr_count($keyval[1], '+') === 1) {
                $keyval[1] = str_replace('+', '%2B', $keyval[1]);
            }
        }
        $myPost[$keyval[0]] = urldecode($keyval[1]);
    }
}
// Build the body of the verification post request, adding the _notify-validate command.
$req = 'cmd=_notify-validate';
$get_magic_quotes_exists = false;
if (function_exists('get_magic_quotes_gpc')) {
    $get_magic_quotes_exists = true;
}
foreach ($myPost as $key => $value) {
    if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
        $value = urlencode(stripslashes($value));
    } else {
        $value = urlencode($value);
    }
    $req .= "&$key=$value";
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
$response = curl_exec($ch);
curl_close($ch);

if ($response == "VERIFIED") {
    $cEmail = $_POST['payer_email'];
    $name = $_POST['first_name'] . " " . $_POST['last_name'];
    $price = $_POST['mc_gross'];
    $currency = $_POST['mc_currency'];
    $item = $_POST['item_number'];
    $paymentStatus = $_POST['payment_status'];
    file_put_contents('test3.txt', file_get_contents('php://input'));       
}

?>

gagregori commented 5 years ago

I'm gonna write a function to make an array from raw input since I dont have a POST to use,