From 42d5f35e1da2956c9ea6e30d51f64c289a62edc2 Mon Sep 17 00:00:00 2001 From: cblach Date: Fri, 29 Nov 2019 18:50:23 +0100 Subject: [PATCH] fixed header merging, added json encoding exception --- lib/Scanpay.php | 11 ++++++++--- tests/charge.php | 2 -- tests/seq.php | 1 - 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/Scanpay.php b/lib/Scanpay.php index 32b9e93..96d02c6 100644 --- a/lib/Scanpay.php +++ b/lib/Scanpay.php @@ -43,7 +43,7 @@ protected function httpHeaders($oldHeaders, $o=[]) { if (isset($ret['idempotency-key'])) { $this->useidem = true; } - return array_values($ret); + return $ret; } protected function handleHeaders($curl, $hdr) { @@ -57,7 +57,7 @@ protected function handleHeaders($curl, $hdr) { protected function request($path, $opts=[], $data=null) { /* Merge headers */ $headers = $this->httpHeaders($this->headers, $this->opts); - $headers = $this->httpHeaders($headers, $opts); + $headers = array_values($this->httpHeaders($headers, $opts)); /* Merge other options */ $opts = array_merge($this->opts, $opts); $hostname = (isset($opts['hostname'])) ? $opts['hostname'] : 'api.scanpay.dk'; @@ -67,7 +67,6 @@ protected function request($path, $opts=[], $data=null) { CURLOPT_URL => 'https://' . $hostname . $path, CURLOPT_HTTPHEADER => $headers, CURLOPT_CUSTOMREQUEST => ($data === null) ? 'GET' : 'POST', - CURLOPT_POSTFIELDS => ($data === null) ? null : json_encode($data, JSON_UNESCAPED_SLASHES), CURLOPT_VERBOSE => isset($opts['debug']) ? $opts['debug'] : 0, CURLOPT_RETURNTRANSFER => 1, CURLOPT_CONNECTTIMEOUT => 20, @@ -75,6 +74,12 @@ protected function request($path, $opts=[], $data=null) { CURLOPT_USE_SSL => CURLUSESSL_ALL, CURLOPT_SSLVERSION => 6, ]; + if ($data !== null) { + $curlopts[CURLOPT_POSTFIELDS] = json_encode($data, JSON_UNESCAPED_SLASHES); + if ($curlopts[CURLOPT_POSTFIELDS] === false) { + throw new \Exception('Failed to JSON encode request to Scanpay: ' . json_last_error_msg()); + } + } if ($this->useidem) { $curlopts[CURLOPT_HEADERFUNCTION] = [$this, 'handleHeaders']; } diff --git a/tests/charge.php b/tests/charge.php index b35395c..d623d27 100644 --- a/tests/charge.php +++ b/tests/charge.php @@ -30,8 +30,6 @@ $charge = [ 'orderid' => 'charge-1023', - 'language' => 'da', - 'successurl' => 'https://docs.scanpay.dk/payment-link', 'items' => [ [ 'name' => 'Pink Floyd: The Dark Side Of The Moon', diff --git a/tests/seq.php b/tests/seq.php index 9a33fca..8ae200f 100644 --- a/tests/seq.php +++ b/tests/seq.php @@ -19,7 +19,6 @@ 'curl' => [ CURLOPT_TIMEOUT => 10, CURLOPT_SSL_FALSESTART => 1, - CURLOPT_TCP_FASTOPEN => 1, ], ];