Serialization of 'Closure' is not allowed I am getting the following error on .../checkout/ID/order_information page after submitting payment details for Review.


  Exception: Serialization of 'Closure' is not allowed in serialize() (line 14 of core/lib/Drupal/Component/Serialization/PhpSerialize.php).


I have tried to catch the exception with a try/catch block, but it  always gives that fatal error, if I unchecked My billing information is the same as my shipping information checkbox in the Payment information form.

I am using my custom payment method module for payment. Where should I look in the code to find what is causing the fatal error?

__construct() method :-

public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, PaymentTypeManager $payment_type_manager, PaymentMethodTypeManager $payment_method_type_manager, TimeInterface $time) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $payment_type_manager, $payment_method_type_manager, $time);

  if ($this-&gt;getMode() === 'test') {
    $url = "url given here";
  }
  else {
    $url = "url given here";
  }

  try {
    $this-&gt;soapClient = new SoapClient($url);
  }
  catch (SoapFault $e) {
    throw new PaymentGatewayException($e-&gt;getMessage());
  }
}
I believe this comes from loading a connection in the SOAP client, and storing it in memory with $this-&gt;soapClient. The problem is that the soap client cannot be serialized. Serialization is the process of turning something into a string, generally so it can be stored in a databse or a file or something, to be retrieved later. The SOAP connection is not something that can be stored, which is why you are facing the issue.

You could potentially store the SOAP client in the construct method, then make the connection using the client when you actually need it. I'm guessing that the client, without a connection, can probably be serialized. I may be wrong though, and you may need to call the soap client dynamically every time you need it.