Question

I want to redirect to a url (Sermepa/Redsys) with POST and some form parameters in a Controller. The process is I submit a form that collect some data, and when the user submit this form, it redirect to my controller. In this controller I prepare the post data and then I want to redirect to bank gateway with POST.

<?php

namespace Drupal\mymodule_sermepa_redsys\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\mymodule_sermepa_redsys\RedsysAPI;
use Drupal\node\NodeInterface;

/**
 * Class DoPaymentController.
 */
class DoPaymentController extends ControllerBase {

  public function doPayment(NodeInterface $node = null) {

    if ($node!==null) {
    
      $redsys = new RedsysAPI();
      $config = \Drupal::config('sermepa_redsys.settings');      

      // Completamos los datos para la petición
      $redsys->setParameter("DS_MERCHANT_AMOUNT", $config->get('ammount'));
      $redsys->setParameter("DS_MERCHANT_ORDER", $node->id());
      $redsys->setParameter("DS_MERCHANT_MERCHANTCODE", $config->get('merchant_code'));
      $redsys->setParameter("DS_MERCHANT_CURRENCY", $config->get('currency'));
      $redsys->setParameter("DS_MERCHANT_TRANSACTIONTYPE", $config->get('transaction_type'));
      $redsys->setParameter("DS_MERCHANT_TERMINAL", $config->get('terminal'));
      $redsys->setParameter("DS_MERCHANT_MERCHANTURL", $config->get('url'));
      $redsys->setParameter("DS_MERCHANT_URLOK", $config->get('merchant_url_ok'));
      $redsys->setParameter("DS_MERCHANT_URLKO", $config->get('merchant_url_ko'));

      $version= $config->get('version');
      $kc = $config->get('merchant_password');
      
      $params = $redsys->createMerchantParameters();
      $signature = $redsys->createMerchantSignature($kc);
      
      $post_data['Ds_SignatureVersion'] = $version;
      $post_data['Ds_MerchantParameters'] = $params;
      $post_data['Ds_Signature'] = $signature;
      
      $client = \Drupal::httpClient();          
      $response = $client->request('POST', $config->get('url_webservice'), ['form_params' => $post_data]);
      return $response;
      
    }
    else {
      return [
        '#markup' => t('Error. No nid'),
      ];
    }
  }
}

What I expected is after sumbit the form, controller was called, and then the user was redirected to bank payment page. The form is a "create node" of a custom content type. In my .module file I have added a custom submit:

function mymodule_form_node_mycustomtype_form_alter(&$form, FormStateInterface &$form_state) {  
...
$form['actions']['submit']['#submit'][]  = '_mycustom_form_submit';
}

function _mycustom_form_submit(array &$form, FormStateInterface $form_state) {
    $url = Url::fromRoute('mymodule_sermepa_redsys.payment_controller_doPayment');
    $url->setRouteParameters(array('node' => $form_state->getValue('nid')));
    $form_state->setRedirectUrl($url); 
}

My routing file:

mymodule_sermepa_redsys.payment_controller_doPayment:
  path: '/doPayment/{node}'
  defaults:
    _controller: '\Drupal\mymodule_sermepa_redsys\Controller\DoPaymentController::doPayment'
    _title: 'Do Payment'
  requirements:
    _permission: 'access content'

Note: The class RedsysAPI is a helper class to prepare the data to POST, but It's irrelevant in the context of this question.

Was it helpful?

Solution

You can't redirect with Post properties. You can only do a request, which you've done. The $response you've created in the controller will contain the results of your HTTP request to the remote service. You'll have to retrieve the the relevant data from that object, and do something with it. You are currently returning that from your function. $response is a Psr\Http\Message\ResponseInterface object. If you just return this, your controller will not know what to do with it.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top