From 057ac8a316f769ce602be6e63104c7cc6f8832b7 Mon Sep 17 00:00:00 2001 From: Kristoffer Karlsson Date: Thu, 6 May 2021 13:03:14 +0200 Subject: [PATCH] Added idempotency key to part activate --- src/Adapter/AdapterInterface.php | 3 ++- src/Adapter/SoapAdapter.php | 37 ++++++++++++++++---------------- src/Invoice/Administration.php | 4 +++- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/Adapter/AdapterInterface.php b/src/Adapter/AdapterInterface.php index 4013ab5..fa8adaf 100644 --- a/src/Adapter/AdapterInterface.php +++ b/src/Adapter/AdapterInterface.php @@ -76,9 +76,10 @@ public function getInvoiceInformation(array $data) :array; * Part activate the invoice. . Returns response if success otherwise throws ResponseError * * @param array $data + * @param array $idempotencyKey * @return array */ - public function partActivateInvoice(array $data):array; + public function partActivateInvoice(array $data, $idempotencyKey = ""):array; /** * Part credit the invoice. . Returns response if success otherwise throws ResponseError diff --git a/src/Adapter/SoapAdapter.php b/src/Adapter/SoapAdapter.php index db33d65..06eac47 100644 --- a/src/Adapter/SoapAdapter.php +++ b/src/Adapter/SoapAdapter.php @@ -79,7 +79,6 @@ public function creditInvoice(array $data): array return $response; } - public function getInvoiceInformation(array $data): array { $bodyData = array_merge($data, $this->baseBodyData); @@ -90,24 +89,21 @@ public function getInvoiceInformation(array $data): array return $response; } - private function getInvoiceServiceUrl(): string { if ($this->config->isTestMode()) { - return $this->testInvoiceServiceUrl; } return $this->invoiceServiceUrl; } - - public function partActivateInvoice(array $data): array + public function partActivateInvoice(array $data, $idempotencyKey = ""): array { $bodyData = array_merge($data, $this->baseBodyData); $url = $this->getInvoiceServiceUrl(); - $response = $this->sendRequest($url, $this->partActivateInvoiceFunction, $bodyData); + $response = $this->sendRequest($url, $this->partActivateInvoiceFunction, $bodyData, $idempotencyKey); return $response; } @@ -130,7 +126,6 @@ private function soapResponseToArray($soapResponse): array return $soapResponse; } - private function getSoapHeader(): array { $ns = $this->invoiceServiceNamespace; @@ -141,7 +136,6 @@ private function getSoapHeader(): array return $header; } - /** * * Sends a SOAP request @@ -155,29 +149,36 @@ private function getSoapHeader(): array * * @return array response */ - public function sendRequest(string $url,string $action, array $bodyData): array + public function sendRequest(string $url, string $action, array $bodyData, $idempotencyKey = ""): array { + $clientOptions = $this->soapClientOptions; + if ($idempotencyKey != "") { + $clientOptions['stream_context'] = stream_context_create( + [ + 'http' => [ + 'header' => "Idempotency-Key: $idempotencyKey" + ] + ] + ); + } $soapClient = new \SoapClient( $url, - $this->soapClientOptions + $clientOptions ); $header = $this->getSoapHeader(); $soapClient->__setSoapHeaders($header); - try{ - $response = $soapClient->__soapCall($action,[$bodyData]); + try { + $response = $soapClient->__soapCall($action, [$bodyData]); return $this->soapResponseToArray($response); - - } catch (\SoapFault $e){ + } catch (\SoapFault $e) { $lastRequest = (string)$soapClient->__getLastRequest(); - $responseError = new ResponseError($e, $lastRequest,$e->getCode(),$e->getMessage()); + $responseError = new ResponseError($e, $lastRequest, $e->getCode(), $e->getMessage()); throw $responseError; } } - - -} \ No newline at end of file +} diff --git a/src/Invoice/Administration.php b/src/Invoice/Administration.php index 91a491c..928a8af 100644 --- a/src/Invoice/Administration.php +++ b/src/Invoice/Administration.php @@ -52,7 +52,9 @@ public function partActivateInvoice( 'InvoiceNo' => $invoiceNo, 'ArticleList' => $articleList->getArticleList() ]; - $response = $this->adapter->partActivateInvoice($data); + $idempotencyKey = $invoiceNo; + + $response = $this->adapter->partActivateInvoice($data, $idempotencyKey); return $response; }