convert form ipn paypal php to laravel

0

I have an example code about ipn (paypal) that is in php. that works correctly for me but I would like to implement it within a laravel driver and I do not get it. since "NO" I have a lot of programming knowledge.

the ipn path example: test.com/ipn/paypal

then what I do is create a route:

Route::post('/ipn/paypal', ['uses' => 'PaypalController@ipn', 'as' => 'paypal.ipn']);

Then I create the PaypalController.php driver with the function (ipn)

public function ipn(Request $request)
{
    define("DEBUG", 0);
    define("USE_SANDBOX", 1);

    $request        = json_decode($request->getContent(), true);
    $raw_post_array = explode('&', $request);
    $myPost         = array();

    foreach ($raw_post_array as $keyval) {
        $keyval = explode('=', $keyval);
        if (count($keyval) == 2) {
            $myPost[$keyval[0]] = urldecode($keyval[1]);
        }

    }

    $req = 'cmd=_notify-validate';

    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";
    }

    if (USE_SANDBOX == true) {
        $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
    } else {
        $paypal_url = "https://www.paypal.com/cgi-bin/webscr";
    }

    $ch = curl_init($paypal_url);

    if ($ch == false) {
        return false;
    }

    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);

    if (DEBUG == true) {
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
    }

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

    $res = curl_exec($ch);

    if (curl_errno($ch) != 0) // cURL error
    {
        if (DEBUG == true) {
            error_log(date('[Y-m-d H:i e] ') . "No se puede conectar a PayPal para validar el IPN: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
        }
        curl_close($ch);
        exit;
    } else {
        if (DEBUG == true) {
            error_log(date('[Y-m-d H:i e] ') . "HTTP respuesta:" . curl_getinfo($ch, CURLINFO_HEADER_OUT) . " para IPN: $req" . PHP_EOL, 3, LOG_FILE);
            error_log(date('[Y-m-d H:i e] ') . "HTTP respuesta a la validacion: $res" . PHP_EOL, 3, LOG_FILE);
        }
        curl_close($ch);
    }
}

but when I do the link test he can not connect and I do not know what to do. since if I create a test of ipn to "test.com/ipn/paypal.php"

(paypal.php)     

define("DEBUG", 0);
define("USE_SANDBOX", 1);
define("LOG_FILE", "resultadopaypal.txt");

$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) {
        $myPost[$keyval[0]] = urldecode($keyval[1]);
    }

}

$req = 'cmd=_notify-validate';

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";
}

if (USE_SANDBOX == true) {
    $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
} else {
    $paypal_url = "https://www.paypal.com/cgi-bin/webscr";
}

$ch = curl_init($paypal_url);

if ($ch == false) {
    return false;
}

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);

if (DEBUG == true) {
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
}

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

$res = curl_exec($ch);

if (curl_errno($ch) != 0) // cURL error
{
    if (DEBUG == true) {
        error_log(date('[Y-m-d H:i e] ') . "No se puede conectar a PayPal para validar el IPN: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
    }
    curl_close($ch);
    exit;
} else {
    if (DEBUG == true) {
        error_log(date('[Y-m-d H:i e] ') . "HTTP respuesta:" . curl_getinfo($ch, CURLINFO_HEADER_OUT) . " para IPN: $req" . PHP_EOL, 3, LOG_FILE);
        error_log(date('[Y-m-d H:i e] ') . "HTTP respuesta a la validacion: $res" . PHP_EOL, 3, LOG_FILE);
    }
    curl_close($ch);
}

$tokens = explode("\r\n\r\n", trim($res));
$res    = trim(end($tokens));

if (strcmp($res, "VERIFIED") == 0) {

    $txt = fopen("paypalpagos.txt", "a+");

    ob_start();
    var_dump($_POST);
    $result = ob_get_clean();

    fputs($txt, $result);
    @fclose($fp);

    if (DEBUG == true) {
        error_log(date('[Y-m-d H:i e] ') . "IPN Verificado: $req " . PHP_EOL, 3, LOG_FILE);
    }
} else if (strcmp($res, "INVALID") == 0) {
    if (DEBUG == true) {
        error_log(date('[Y-m-d H:i e] ') . "IPN Invalido: $req" . PHP_EOL, 3, LOG_FILE);
    }
}

If it works for me, so you should do something wrong in the code writing to the laravel format or when going through the request.

    
asked by Luis Alberto Cárdenas Vargas 21.01.2017 в 19:59
source

0 answers