From e79a54f963fb062e497ef54093ca2a3174c7f329 Mon Sep 17 00:00:00 2001 From: Michal Sanger Date: Wed, 3 Feb 2016 18:22:07 +0100 Subject: [PATCH] Test gateway via Echo API request --- src/Gateway.php | 25 +++++++++ src/Message/EchoRequest.php | 103 +++++++++++++++++++++++++++++++++++ src/Message/EchoResponse.php | 8 +++ 3 files changed, 136 insertions(+) create mode 100644 src/Message/EchoRequest.php create mode 100644 src/Message/EchoResponse.php diff --git a/src/Gateway.php b/src/Gateway.php index 60cc695..e1942cd 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -5,6 +5,7 @@ use Omnipay\Common\AbstractGateway; use Omnipay\Common\Exception\InvalidResponseException; use Omnipay\Csob\Message\CompletePurchaseRequest; +use Omnipay\Csob\Message\EchoRequest; use Omnipay\Csob\Message\InitPaymentRequest; use Omnipay\Csob\Message\ProcessPaymentRequest; use Omnipay\Csob\Message\PaymentResponse; @@ -124,6 +125,30 @@ public function completePurchase(array $parameters = array()) return $this->createRequest(CompletePurchaseRequest::class, $parameters); } + /** + * @param string $merchantId + * @param string $httpMethod + * @return Message\EchoResponse + * @throws \Exception + * @see https://github.com/csob/paymentgateway/wiki/eAPI-1.5-EN#getpost-httpsapiplatebnibranacsobczapiv15echo- + */ + public function testGateway($merchantId, $httpMethod = 'GET') + { + $data = [ + 'merchantId' => $merchantId, + 'dttm' => date('Ymdhis'), + ]; + + /** @var EchoRequest $request */ + $request = $this->createRequest(EchoRequest::class, $data); + if ($httpMethod === 'POST') { + $response = $request->sendViaPost(); + } else { + $response = $request->sendViaGet(); + } + return $response; + } + protected function createRequest($class, array $parameters) { if (!($this->signator instanceof DataSignator)) { diff --git a/src/Message/EchoRequest.php b/src/Message/EchoRequest.php new file mode 100644 index 0000000..56e4719 --- /dev/null +++ b/src/Message/EchoRequest.php @@ -0,0 +1,103 @@ +setParameter('merchantId', $value); + } + + public function setDttm($value) + { + $this->setParameter('dttm', $value); + } + + /** + * Get the raw data array for this message. The format of this varies from gateway to + * gateway, but will usually be either an associative array, or a SimpleXMLElement. + * + * @return mixed + */ + public function getData() + { + $data = [ + "merchantId" => $this->getParameter('merchantId'), + "dttm" => $this->getParameter('dttm'), + ]; + $data['signature'] = $this->signData($data); + return $data; + } + + private function signData($data) + { + $arrayKeys = [ + 'merchantId', 'dttm', + ]; + return $this->getSignator()->sign($data, $arrayKeys); + } + + /** + * Send the request with specified data + * + * @param mixed $data The data to send + * @return ResponseInterface + */ + public function sendData($data) + { + return $this->sendViaPost(); + } + + public function sendViaPost() + { + $apiUrl = $this->getApiUrl() . '/echo'; + $data = json_encode($this->getData()); + + $httpRequest = $this->httpClient->createRequest( + RequestInterface::POST, + $apiUrl, + ['Content-Type' => 'application/json'], + $data + ); + + return $this->response = $this->sendAndProcessResponse($httpRequest); + } + + public function sendViaGet() + { + $data = $this->getData(); + $apiUrl = implode('/', [ + $this->getApiUrl(), + 'echo', + $data['merchantId'], + $data['dttm'], + urlencode($data['signature']), + ]); + + $httpRequest = $this->httpClient->createRequest( + RequestInterface::GET, + $apiUrl + ); + + return $this->response = $this->sendAndProcessResponse($httpRequest); + } + + /** + * @param RequestInterface $httpRequest + * @return EchoResponse + */ + private function sendAndProcessResponse(RequestInterface $httpRequest) + { + $httpResponse = $httpRequest->send(); + $data = $httpResponse->json(); + + $response = new EchoResponse($this, $data); + $response->setVerifier($this->getVerifier()); + + return $response; + } +} \ No newline at end of file diff --git a/src/Message/EchoResponse.php b/src/Message/EchoResponse.php new file mode 100644 index 0000000..0d33409 --- /dev/null +++ b/src/Message/EchoResponse.php @@ -0,0 +1,8 @@ +