Skip to content

Commit

Permalink
fixed header merging, added json encoding exception
Browse files Browse the repository at this point in the history
  • Loading branch information
cblach committed Nov 29, 2019
1 parent 71ddc3f commit 42d5f35
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
11 changes: 8 additions & 3 deletions lib/Scanpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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';
Expand All @@ -67,14 +67,19 @@ 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,
CURLOPT_TIMEOUT => 20,
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'];
}
Expand Down
2 changes: 0 additions & 2 deletions tests/charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 0 additions & 1 deletion tests/seq.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
'curl' => [
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_FALSESTART => 1,
CURLOPT_TCP_FASTOPEN => 1,
],
];

Expand Down

0 comments on commit 42d5f35

Please sign in to comment.