Skip to content

Commit

Permalink
Test gateway via Echo API request
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsanger committed Feb 3, 2016
1 parent e8c5131 commit e79a54f
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
103 changes: 103 additions & 0 deletions src/Message/EchoRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Omnipay\Csob\Message;

use Guzzle\Http\Message\RequestInterface;
use Omnipay\Common\Message\ResponseInterface;

class EchoRequest extends AbstractRequest
{
public function setMerchantId($value)
{
$this->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;
}
}
8 changes: 8 additions & 0 deletions src/Message/EchoResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Omnipay\Csob\Message;

class EchoResponse extends AbstractResponse
{

}

0 comments on commit e79a54f

Please sign in to comment.