From 8a21bd63cf04fa163690f73629092ef209d16c06 Mon Sep 17 00:00:00 2001 From: Alain Pellaux Date: Thu, 9 Jul 2020 16:49:59 +0200 Subject: [PATCH 1/2] Add server-to-server password support --- src/Message/AbstractXmlRequest.php | 16 ++++++++++++---- src/Message/XmlStatusRequest.php | 12 ++++++++++++ src/Traits/HasGatewayParameters.php | 17 +++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/Message/AbstractXmlRequest.php b/src/Message/AbstractXmlRequest.php index 570114a..07acee3 100644 --- a/src/Message/AbstractXmlRequest.php +++ b/src/Message/AbstractXmlRequest.php @@ -58,6 +58,8 @@ abstract class AbstractXmlRequest extends AbstractRequest abstract public function getData(); + abstract public function getAuthorization(); + /** * @param $requestElement * @return mixed @@ -149,13 +151,19 @@ protected function getHttpMethod() */ public function sendData($data) { + $headers = [ + 'Accept' => 'application/xml', + 'Content-type' => 'application/xml', + ]; + + if ($this->getAuthorization()) { + $headers['Authorization'] = $this->getAuthorization(); + } + $httpResponse = $this->httpClient->request( $this->getHttpMethod(), $this->getEndpoint(), - [ - 'Accept' => 'application/xml', - 'Content-type' => 'application/xml', - ], + $headers, $this->getRequestXml()->asXML() ); diff --git a/src/Message/XmlStatusRequest.php b/src/Message/XmlStatusRequest.php index cc8d47f..4c54695 100644 --- a/src/Message/XmlStatusRequest.php +++ b/src/Message/XmlStatusRequest.php @@ -62,4 +62,16 @@ public function getData() return $data; } + + /** + * @return string|null + */ + public function getAuthorization() + { + if ($this->getPassword()) { + return 'Basic ' . base64_encode($this->getMerchantId() . ':' . $this->getPassword()); + } + + return null; + } } diff --git a/src/Traits/HasGatewayParameters.php b/src/Traits/HasGatewayParameters.php index 07f87c4..b3a5890 100644 --- a/src/Traits/HasGatewayParameters.php +++ b/src/Traits/HasGatewayParameters.php @@ -777,4 +777,21 @@ public function getHmacKey() { return $this->getParameter('hmacKey2') ?: $this->getParameter('hmacKey1'); } + + /** + * @param string password for server-to-server services + * @return $this + */ + public function setPassword($value) + { + return $this->setParameter('password', $value); + } + + /** + * @return string + */ + public function getPassword() + { + return $this->getParameter('password'); + } } From df5c0f8a1d9748d7321e2f08282654db30371e3c Mon Sep 17 00:00:00 2001 From: Alain Pellaux Date: Fri, 17 Jul 2020 15:50:52 +0200 Subject: [PATCH 2/2] fix: Move get Authorization on abstract class --- src/Message/AbstractXmlRequest.php | 14 ++++++++++++-- src/Message/XmlStatusRequest.php | 12 ------------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Message/AbstractXmlRequest.php b/src/Message/AbstractXmlRequest.php index 07acee3..ce63fc5 100644 --- a/src/Message/AbstractXmlRequest.php +++ b/src/Message/AbstractXmlRequest.php @@ -58,8 +58,6 @@ abstract class AbstractXmlRequest extends AbstractRequest abstract public function getData(); - abstract public function getAuthorization(); - /** * @param $requestElement * @return mixed @@ -217,4 +215,16 @@ public function getApiEndpoint() { return $this->apiEndpoint; } + + /** + * @return string|null + */ + public function getAuthorization() + { + if ($this->getPassword()) { + return 'Basic ' . base64_encode($this->getMerchantId() . ':' . $this->getPassword()); + } + + return null; + } } diff --git a/src/Message/XmlStatusRequest.php b/src/Message/XmlStatusRequest.php index 4c54695..cc8d47f 100644 --- a/src/Message/XmlStatusRequest.php +++ b/src/Message/XmlStatusRequest.php @@ -62,16 +62,4 @@ public function getData() return $data; } - - /** - * @return string|null - */ - public function getAuthorization() - { - if ($this->getPassword()) { - return 'Basic ' . base64_encode($this->getMerchantId() . ':' . $this->getPassword()); - } - - return null; - } }