Skip to main content
se eliminaron 5 caracteres en el cuerpo
Origen Enlace
Route::post('/ipn/paypal', ['uses' => 'PaypalController@ipn', 'as' => 'webhooks'paypal.paypal']ipn']);
Route::post('/ipn/paypal', ['uses' => 'PaypalController@ipn', 'as' => 'webhooks.paypal']);
Route::post('/ipn/paypal', ['uses' => 'PaypalController@ipn', 'as' => 'paypal.ipn']);
Origen Enlace

convertir formulario ipn paypal php a laravel

tengo un código de ejemplo sobre ipn(paypal) que esta en php. que me funciona correctamente pero quisiera implementarlo dentro de un controlador de laravel y no lo consigo. ya que "NO" tengo muchos conocimientos de programación.

la ruta del ipn ejemplo: test.com/ipn/paypal

entonces lo que hago es crear una ruta:

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

Después creo el controlador PaypalController.php con la 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);
    }
}

pero cuando hago el test de https://developer.paypal.com/developer/ipnSimulator/ no logra conectar y no se que hacer. ya que si creo un test de ipn a "test.com/ipn/paypal.php"

(paypal.php) <?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);
    }
}

si me funciona, así que debe algo que hago mal en la redacción del código al formato laravel o cuando pasa por el request.