Skip to content

Commit

Permalink
Added idempotency key to part activate
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoffer-webbhuset committed May 6, 2021
1 parent 81dd8ce commit 057ac8a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 19 additions & 18 deletions src/Adapter/SoapAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public function creditInvoice(array $data): array
return $response;
}


public function getInvoiceInformation(array $data): array
{
$bodyData = array_merge($data, $this->baseBodyData);
Expand All @@ -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;
}
Expand All @@ -130,7 +126,6 @@ private function soapResponseToArray($soapResponse): array
return $soapResponse;
}


private function getSoapHeader(): array
{
$ns = $this->invoiceServiceNamespace;
Expand All @@ -141,7 +136,6 @@ private function getSoapHeader(): array
return $header;
}


/**
*
* Sends a SOAP request
Expand All @@ -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;
}
}


}
}
4 changes: 3 additions & 1 deletion src/Invoice/Administration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 057ac8a

Please sign in to comment.