Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,44 @@ try{
```


### Create terminal CloudPOS payment
#### API Documentation
https://apidoc.comgate.cz/api/rest-terminal
```php
use Comgate\SDK\Comgate;
use Comgate\SDK\Entity\Codes\CurrencyCode;
use Comgate\SDK\Entity\Codes\RequestCode;
use Comgate\SDK\Entity\Money;
use Comgate\SDK\Entity\TerminalPayment;
use Comgate\SDK\Entity\TerminalRefund;
use Comgate\SDK\Exception\ApiException;

$clientTerminal = Comgate::defaultsRest()
->setMerchant('123456') // get on portal.comgate.cz
->setSecret('foobarbaz') // get on portal.comgate.cz
->createTerminalClient();

$terminalPayment = new TerminalPayment();
$terminalPayment
->setPrice(Money::ofInt(4))
->setCurr(CurrencyCode::CZK)
->setRefId('123456');

try {
$createTerminalPaymentResponse = $clientTerminal->createPayment($terminalPayment);
if ($createTerminalPaymentResponse->getCode() === RequestCode::OK) {

// save ID of terminal payment in your system
echo 'created terminal payment: ' . $createTerminalPaymentResponse->getTransId();

} else {
var_dump($createTerminalPaymentResponse->getMessage());
}
} catch (ApiException $e) {
var_dump($e->getMessage());
}
```

### Debugging

#### Logging
Expand Down
122 changes: 122 additions & 0 deletions src/ClientTerminal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php declare(strict_types = 1);

namespace Comgate\SDK;

use Comgate\SDK\Entity\TerminalPayment;
use Comgate\SDK\Entity\TerminalRefund;
use Comgate\SDK\Entity\Request\TerminalPaymentCreateRequest;
use Comgate\SDK\Entity\Request\TerminalPaymentStatusRequest;
use Comgate\SDK\Entity\Request\TerminalPaymentCancelRequest;
use Comgate\SDK\Entity\Request\TerminalClosingRequest;
use Comgate\SDK\Entity\Request\TerminalRefundCreateRequest;
use Comgate\SDK\Entity\Request\TerminalRefundStatusRequest;
use Comgate\SDK\Entity\Request\TerminalRefundCancelRequest;
use Comgate\SDK\Entity\Request\TerminalStatusRequest;
use Comgate\SDK\Entity\Response\TerminalPaymentCreateResponse;
use Comgate\SDK\Entity\Response\TerminalPaymentStatusResponse;
use Comgate\SDK\Entity\Response\TerminalPaymentCancelResponse;
use Comgate\SDK\Entity\Response\TerminalClosingResponse;
use Comgate\SDK\Entity\Response\TerminalRefundCreateResponse;
use Comgate\SDK\Entity\Response\TerminalRefundStatusResponse;
use Comgate\SDK\Entity\Response\TerminalRefundCancelResponse;
use Comgate\SDK\Entity\Response\TerminalStatusResponse;
use Comgate\SDK\Http\ITransport;

class ClientTerminal
{
/** @var ITransport */
protected $transport;

public function __construct(ITransport $transport)
{
$this->transport = $transport;
}

/**
* Vytvoří novou platbu na terminálu
*/
public function createPayment(TerminalPayment $terminalPayment): TerminalPaymentCreateResponse
{
$request = new TerminalPaymentCreateRequest($terminalPayment);
$response = $this->transport->postJson($request->getUrn(), $request->toArray());
return new TerminalPaymentCreateResponse($response);
}

/**
* Zjistí stav platby na terminálu
*/
public function getPaymentStatus(string $transId): TerminalPaymentStatusResponse
{
$request = new TerminalPaymentStatusRequest($transId);
$response = $this->transport->get($this->replacePathParam($request->getUrn(), $transId));
return new TerminalPaymentStatusResponse($response);
}

/**
* Zruší platbu na terminálu
*/
public function cancelPayment(string $transId): TerminalPaymentCancelResponse
{
$request = new TerminalPaymentCancelRequest($transId);
$response = $this->transport->delete($this->replacePathParam($request->getUrn(), $transId));
return new TerminalPaymentCancelResponse($response);
}

/**
* Provede uzávěrku na terminálu
*/
public function createClosing(): TerminalClosingResponse
{
$request = new TerminalClosingRequest();
$response = $this->transport->postJson($request->getUrn(), $request->toArray());
return new TerminalClosingResponse($response);
}

/**
* Vytvoří nový návrat (refund) na terminálu
*/
public function createRefund(TerminalRefund $terminalRefund): TerminalRefundCreateResponse
{
$request = new TerminalRefundCreateRequest($terminalRefund);
$response = $this->transport->postJson($request->getUrn(), $request->toArray());
return new TerminalRefundCreateResponse($response);
}

/**
* Zjistí stav návratu na terminálu
*/
public function getRefundStatus(string $transId): TerminalRefundStatusResponse
{
$request = new TerminalRefundStatusRequest($transId);
$response = $this->transport->get($this->replacePathParam($request->getUrn(), $transId));
return new TerminalRefundStatusResponse($response);
}

/**
* Zruší návrat na terminálu
*/
public function cancelRefund(string $transId): TerminalRefundCancelResponse
{
$request = new TerminalRefundCancelRequest($transId);
$response = $this->transport->delete($this->replacePathParam($request->getUrn(), $transId));
return new TerminalRefundCancelResponse($response);
}

/**
* Zjistí stav terminálu
*/
public function getTerminalStatus(): TerminalStatusResponse
{
$request = new TerminalStatusRequest();
$response = $this->transport->get($request->getUrn());
return new TerminalStatusResponse($response);
}

/**
* Nahradí {transId} v URN skutečným transId
*/
private function replacePathParam(string $urn, string $transId): string
{
return str_replace('{transId}', $transId, $urn);
}
}
16 changes: 16 additions & 0 deletions src/Comgate.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ public static function defaults(): self
return $self;
}

/**
* @return static
*/
public static function defaultsRest(): self
{
$self = new static();
$self->url = Config::URL_REST;

return $self;
}

/**
* @return static
*/
Expand Down Expand Up @@ -81,6 +92,11 @@ public function createClient(): Client
return new Client($this->createTransport());
}

public function createTerminalClient(): ClientTerminal
{
return new ClientTerminal($this->createTransport());
}

protected function createConfig(): Config
{
return new Config($this->merchant, $this->secret, $this->url);
Expand Down
1 change: 1 addition & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class Config
{
public const URL = 'https://payments.comgate.cz/v1.0/';
public const URL_REST = 'https://payments.comgate.cz/v2.0/';
/** @var string */
private $merchant;

Expand Down
20 changes: 20 additions & 0 deletions src/Entity/Codes/TerminalPaymentStatusCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace Comgate\SDK\Entity\Codes;

final class TerminalPaymentStatusCode
{

public const PENDING = 'PENDING';
public const PAID = 'PAID';
public const CANCELLED = 'CANCELLED';
public const ERROR = 'ERROR';

public const SELF = [
self::PENDING,
self::PAID,
self::CANCELLED,
self::ERROR,
];

}
21 changes: 21 additions & 0 deletions src/Entity/Codes/TerminalStatusCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace Comgate\SDK\Entity\Codes;

final class TerminalStatusCode
{

public const ONLINE = 'ONLINE';
public const OFFLINE = 'OFFLINE';
public const BUSY = 'BUSY';
public const UNKNOWN = 'UNKNOWN';

public const SELF = [
self::ONLINE,
self::OFFLINE,
self::BUSY,
self::UNKNOWN,
];

}

19 changes: 19 additions & 0 deletions src/Entity/Request/TerminalClosingRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace Comgate\SDK\Entity\Request;

class TerminalClosingRequest implements IRequest
{
public function getUrn(): string
{
return 'terminalClosing.json';
}

/**
* @return array<string, bool|int|string|null>
*/
public function toArray(): array
{
return [];
}
}
40 changes: 40 additions & 0 deletions src/Entity/Request/TerminalPaymentCancelRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

namespace Comgate\SDK\Entity\Request;

class TerminalPaymentCancelRequest implements IRequest
{
/** @var string */
private $transId;

public function __construct(string $transId)
{
$this->transId = $transId;
}

public function getUrn(): string
{
return 'terminalPayment/transId/{transId}.json';
}

/**
* @return array<string, string>
*/
public function toArray(): array
{
return [
'transId' => $this->transId,
];
}

public function getTransId(): string
{
return $this->transId;
}

public function setTransId(string $transId): self
{
$this->transId = $transId;
return $this;
}
}
33 changes: 33 additions & 0 deletions src/Entity/Request/TerminalPaymentCreateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace Comgate\SDK\Entity\Request;

use Comgate\SDK\Entity\TerminalPayment;

class TerminalPaymentCreateRequest implements IRequest
{
/** @var TerminalPayment */
private $terminalPayment;

public function __construct(TerminalPayment $terminalPayment)
{
$this->terminalPayment = $terminalPayment;
}

public function getUrn(): string
{
return 'terminalPayment.json';
}

/**
* @return array<string, int|string|null>
*/
public function toArray(): array
{
return array_filter([
'price' => $this->terminalPayment->getPrice()->get(),
'curr' => $this->terminalPayment->getCurr(),
'refId' => $this->terminalPayment->getRefId(),
], function ($v) { return $v !== null; });
}
}
40 changes: 40 additions & 0 deletions src/Entity/Request/TerminalPaymentStatusRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

namespace Comgate\SDK\Entity\Request;

class TerminalPaymentStatusRequest implements IRequest
{
/** @var string */
private $transId;

public function __construct(string $transId)
{
$this->transId = $transId;
}

public function getUrn(): string
{
return 'terminalPayment/transId/{transId}.json';
}

/**
* @return array<string, string>
*/
public function toArray(): array
{
return [
'transId' => $this->transId,
];
}

public function getTransId(): string
{
return $this->transId;
}

public function setTransId(string $transId): self
{
$this->transId = $transId;
return $this;
}
}
Loading
Loading