creagia/redsys-php

Bizum - Redsys: invalid response from bank.

Alicia-ls opened this issue · 3 comments

Hola buenas,
he usado el código para vincular con la pasarela de pago de redsys y todo genial sin problemas. Quiero también integrar bizum y quería saber si esta biblioteca está preparada para tratar los datos, de momento he modificado el código de esta forma para que aparezca la ventana de bizum:

$redsysRequest->createPaymentRequest(
amount: 2,
orderNumber: rand(1000, 1000000),
currency: Currency::EUR,
transactionType: TransactionType::Autorizacion,
requestParameters: new RequestParameters(
merchantUrl: route('comprobar', $user_id),
merchantName : $enterprise_name,
urlOk: route('comprobar', $user_id),
urlKo: route('comprobar', $user_id),
consumerLanguage: ConsumerLanguage::Auto->value,
payMethods: PayMethod::Bizum->value,
productDescription: $course_name,
),
);

Pero a la hora de comprobar el pago, me da error en este punto:
$redsysNotification->setParametersFromResponse($_GET);

y el error que me aparece en el navegador es este:

Creagia\Redsys\Exceptions\InvalidRedsysNotification
Redsys: invalid response from bank.

Espero puedan ayudarme, gracias.

@Alicia-ls can you post the request values you receive on the $_GET variable? This exception is thrown when required data is missing from the bank response.

@Alicia-ls can you post the request values you receive on the $_GET variable? This exception is thrown when required data is missing from the bank response.

Estas son mis dos funciones, la primera para el form:

public function bizum($id)
{
$user_id = $id;
$users_courses = Users_course::join('courses', 'courses.id', '=', 'users_courses.course_id')
->join('webs', 'webs.id', '=', 'courses.web_id')
->join('enterprises', 'enterprises.id', '=', 'webs.enterprise_id')
->where('users_courses.user_id', $user_id)
->get(
array(
'users_courses.user_id',
'courses.name',
'courses.price',
'enterprises.name as enterprise_name'
)
);
foreach($users_courses as $users_courses){
$price = $users_courses->price;
$course_name = $users_courses->name;
$enterprise_name = $users_courses->enterprise_name;
}
$redsysClient = new RedsysClient(
merchantCode : env('REDSYS_MERCHANT_CODE'),
secretKey : env('REDSYS_KEY'),
terminal : env('REDSYS_TERMINAL'),
environment: \Creagia\Redsys\Enums\Environment::Test,
);

    $redsysRequest = new RedsysRequest($redsysClient);
    $redsysRequest->createPaymentRequest(
        amount: 2,
        orderNumber: rand(1000, 1000000),
        currency: Currency::EUR,
        transactionType: TransactionType::Autorizacion,
        requestParameters: new RequestParameters(
            merchantUrl: route('comprobar', $user_id),
            merchantName : $enterprise_name,
            urlOk: route('comprobar', $user_id),
            urlKo: route('comprobar', $user_id),
            consumerLanguage: ConsumerLanguage::Auto->value,
            payMethods: PayMethod::Bizum->value,
            productDescription: $course_name,
        ),
    );
    return $redsysRequest->getFormHtml();
}

Y la segunda que comprueba los datos:

public function comprobar($id)
{    
    $user_id = $id;
    $paid = now();

    $redsysClient = new RedsysClient(
        merchantCode : env('REDSYS_MERCHANT_CODE'),
        secretKey : env('REDSYS_KEY'),
        terminal : env('REDSYS_TERMINAL'),
        environment: \Creagia\Redsys\Enums\Environment::Custom,
        customBaseUrl: route('comprobar', $user_id),
    );
    $redsysNotification = new RedsysNotification($redsysClient);  

AQUÍ TODO OK

    $redsysNotification->setParametersFromResponse($_GET);

SE PARA JUSTO EN ESTE PUNTO, NUNCA LLEGO A RECIBIR NADA POR GET, ME SALTA EL ERROR

    try {
        $notificationData = $redsysNotification->checkResponse();
        Users_course::where('user_id', $user_id)->update(array(
            'paid' => $paid,
            'order' => $notificationData->order
        ));
        // Authorised payment
        return redirect()->route('users.congrats');

    } catch (DeniedRedsysPaymentNotification $e) {
        $errorMessage = $e->getMessage();
        // Denied payment with $errorMessage
        return redirect()->route('users.fail');
    }
    return view('users.notification');
}

On Redsys official docs, you'll find that notification is sent as a POST request, not GET. https://pagosonline.redsys.es/conexion-redireccion.html#:~:text=DS_MERCHANT_EMV3DS%20tipo%20JSON.-,Notificaci%C3%B3n%20Online,-La%20notificacio%CC%81n%20on

Maybe this could be the issue? Take a look at the official docs, the notification is a POST request un background, so you won't see the result on screen. urlOk and urlKo are only the return after the user clicks on continue, while notification is sent in background.

I haven't had a change to try Bizum yet, so I tested it to check everything is working fine and it worked for me. Take a look at this example: https://gist.github.com/dtorras/1b7bb59c7aafba50c60b5d41ac83aa6b

Also, if you are using this on Laravel, remember to disable CSRF verification on the VerifyCsrfToken middleware. In the example above, I modified my middle to this:

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array<int, string>
     */
    protected $except = [
        'notification'
    ];
}