sveawebpay / php-integration

SDK for Sveas payment methods (standalone and Svea Checkout)
Other
15 stars 19 forks source link

[Feature Request] Grab the raw HTTP log for SOAP as well #92

Open timint opened 3 years ago

timint commented 3 years ago

It's nice that the raw log for the hosted payment transactions can be retrieved. I am missing the same feature for the SOAP endpoints.

Here is sample code for implementing this:

  class SOAPClientExtended extends SOAPClient {
    public $lastRequest = [];
    public $lastResponse = [];

    public function __construct($wsdl, $options=array()) {

      if (empty($options['trace'])) $options['trace'] = true;
      if (!isset($options['exceptions'])) $options['exceptions'] = true;

      parent::__construct($wsdl, $options);
    }

    public function __doRequest($request, $location, $action, $version, $one_way = 0) {

      $timestamp_start = time();
      $microtime_start = microtime(true);

        $result = parent::__doRequest($request, $location, $action, $version, $one_way);

      // parent::__getLastResponse() // Can sometimes return null, however print_r($result, true) always return xml content
      $response_body = print_r($result, true);

      $this->lastRequest = array(
        'timestamp' => $timestamp_start,
        'head' => parent::__getLastRequestHeaders(),
        'body' => functions::xml_pretty_print(parent::__getLastRequest()),
      );

      $this->lastResponse = array(
        'timestamp' => time(),
        'head' => parent::__getLastResponseHeaders() . PHP_EOL,
        'body' => functions::xml_pretty_print($response_body),
        'data_amount' => strlen(parent::__getLastResponseHeaders()) + strlen($response_body),
        'duration' => round(microtime(true) - $microtime_start, 3),
      );

      return $result;
    }

    public function ____soapCall($function_name, $arguments, $options=array(), $input_headers=null, $one_way = 0) {
      return parent::__soapCall($function_name, $arguments, $options, $input_headers);
    }

    public function getLog() {
      return "## [". date('Y-m-d H:i:s', $this->lastRequest['timestamp']) ."] Raw HTTP Request ##########\r\n\r\n" .
             $this->lastRequest['head'] .
             $this->lastRequest['body'] ."\r\n\r\n" .
             "## [". date('Y-m-d H:i:s', $this->lastResponse['timestamp']) ."] Raw HTTP Response ". number_format($this->lastResponse['data_amount'], 0, '.', ',') ." bytes transferred in ". (float)$this->lastResponse['duration'] ." s ##########\r\n\r\n" .
             $this->lastResponse['head'] .
             $this->lastResponse['body'];
    }
  }