0

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.

10
  • After you submit what form?
    – Jaypan
    Commented Jun 19, 2020 at 6:55
  • I have edited my question in order to add what you ask. Thanks for your help.
    – briast
    Commented Jun 19, 2020 at 7:03
  • What does your routing entry look like for the route mymodule_sermepa_redsys.payment_controller_doPayment?
    – Jaypan
    Commented Jun 19, 2020 at 7:05
  • I have edited my question with that content. Thanks.
    – briast
    Commented Jun 19, 2020 at 7:15
  • This may be relevant for your case softwareengineering.stackexchange.com/questions/99894/… Commented Jun 19, 2020 at 7:36

1 Answer 1

1

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.

1
  • Thanks for your help. It's clear, then, I was wrong with this way. I have changed it. I have created a custom form and I've added an action with the target url. By default, drupal forms are sent with method = POST. Now it works good and It redirects to bank gateway.
    – briast
    Commented Jun 22, 2020 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.