How to redirect the user to an external website with POST data? I want to implement an offsite payment method and everything is going well except the fact that I can't find how to redirect the user to the offsite payment gateway after submitting the cart checkout review

When the user submits the checkout review, the orderSubmit function is called. Inside this function I create the array with all the parameters required in the request, but I don't know how to send this request

public function orderSubmit(OrderInterface $order) {
    $fields = array(
      'version' =&gt; $this-&gt;configuration['version'],
      'TPE' =&gt; $this-&gt;configuration['tpe'],
      'date' =&gt; date('d/m/Y:H:i:s', time()),
      'montant' =&gt; round($order-&gt;getTotal(), 2).\Drupal::state()-&gt;get('uc_currency_code', 'USD'),
      'reference' =&gt; $order-&gt;id(),
      'texte-libre' =&gt; md5(uniqid(rand(), TRUE)),
      'mail' =&gt; $order-&gt;getEmail(),
      'lgue' =&gt; 'FR',
      'societe' =&gt; $this-&gt;configuration['company'],
      'url_retour' =&gt; Url::fromRoute('uc_cmic.cancel', array('absolute' =&gt; TRUE)),
      'url_retour_ok' =&gt; Url::fromRoute('uc_cmic.success', array('absolute' =&gt; TRUE)),
      'url_retour_err' =&gt; Url::fromRoute('uc_cmic.return', array('absolute' =&gt; TRUE)),
      'MAC' =&gt; "",
      'options' =&gt; ""
    );
    $fields = uc_cmcic_complete_request($this-&gt;configuration['security_key'], $fields); // This function fills the 'MAC' key by using the security key
    $host = UC_CMCIC_URL_CM_TEST;

//TODO send the request and redirect the user to the offsite payment gateway here
  }


What I tried to do and didn't work :

$response = \Drupal::httpClient()-&gt;request('POST', $host, [
  'form_params' =&gt; $fields,
]);


I tried some other variations of this too (like returning the $response after), which didn't work either

Question : What can I do to redirect the user to the offsite payment gateway while sending POST data in the request ?
In D8 for redirection you need to use RedirectResponse Object &amp; thats best way forward. However for improved security the same RedirectResponse object does not allow External URL redirections. For this You may want to use TrustedRedirectResponse. Sample below with assumption that you are trying to post data to the payment getway which can be added as a query string on the URL.

use Drupal\Core\Routing\TrustedRedirectResponse;

public function myController() {
  return new TrustedRedirectResponse($host.'?xyz=abc);
}


PREFERRED

However the above suggestion does has security implications as the data is exposed on the URL. Below solution is a minor tweak from your code.

$response = \Drupal::httpClient()-&gt;post($host, [
        'form_params' =&gt; $fields,
        'headers' =&gt; [
          'Content-type' =&gt; 'application/x-www-form-urlencoded',
        ],
    ]);


You can try below if you would like to capture response (If its an XML / JSON)

$response = \Drupal::httpClient()-&gt;post($host, [
        'form_params' =&gt; $fields,
        'headers' =&gt; [
          'Content-type' =&gt; 'application/x-www-form-urlencoded',
        ],
    ])-&gt;getBody()-&gt;getContents();