Skip to content

Commit

Permalink
Response entities
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMystikJonas authored and f3l1x committed Sep 25, 2023
1 parent 5da7363 commit 1f72559
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 16 deletions.
109 changes: 109 additions & 0 deletions src/Entity/Response/AbstractResponseEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php declare(strict_types = 1);

namespace Contributte\Comgate\Entity\Response;

use Contributte\Comgate\Exceptions\Logical\InvalidArgumentException;
use Contributte\Comgate\Exceptions\Runtime\InvalidComGateDataException;
use Contributte\Comgate\Http\Response;
use Psr\Http\Message\ResponseInterface;

abstract class AbstractResponseEntity extends Response
{

/**
* @param Response|ResponseInterface $origin
*/
public function __construct($origin)
{
if ($origin instanceof Response) {
$origin = $origin->getOrigin();
}

parent::__construct($origin);
}

/**
* @param string $fieldId
* @param int|string|null $default
* @return int|string|null
*/
public function get(string $fieldId, $default = null)
{
$value = $this->getData()[$fieldId] ?? $default;
if ($value !== null && !is_string($value) && !is_int($value)) {
throw new InvalidComGateDataException(sprintf('Comgate response field "%s" - unexpected type', $fieldId));
}

return $value;
}

/**
* @phpstan-return ($default is null ? (string|null) : string)
*/
protected function getString(string $fieldId, ?string $default = null): ?string
{
return (string) ($this->get($fieldId) ?? $default);
}

protected function getRequiredString(string $fieldId): string
{
$value = $this->getString($fieldId);
if ($value === null) {
throw new InvalidArgumentException(sprintf('Comgate response field "%s" - missing', $fieldId));
}

return $value;
}

/**
* @phpstan-return ($default is null ? (int|null) : int)
*/
protected function getInteger(string $fieldId, ?int $default = null): ?int
{
return (int) ($this->get($fieldId) ?? $default);
}

protected function getRequiredInteger(string $fieldId): int
{
$value = $this->getInteger($fieldId);
if ($value === null) {
throw new InvalidArgumentException(sprintf('Comgate response field "%s" - missing', $fieldId));
}

return $value;
}

/**
* @phpstan-return ($default is null ? (bool|null) : bool)
*/
protected function getBool(string $fieldId, ?bool $default = null): ?bool
{
return (bool) ($this->get($fieldId) ?? $default);
}

protected function getRequiredBool(string $fieldId): bool
{
$value = $this->getBool($fieldId);
if ($value === null) {
throw new InvalidArgumentException(sprintf('Comgate response field "%s" - missing', $fieldId));
}

return $value;
}

public function getErrorCode(): int
{
return $this->getRequiredInteger('code');
}

public function getErrorMessage(): ?string
{
return $this->getString('message');
}

public function isOk(): bool
{
return $this->getInteger('code', -1) === 0;
}

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

namespace Contributte\Comgate\Entity\Response;

class PaymentResponse extends AbstractResponseEntity
{

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

public function getRedirect(): ?string
{
return $this->getString('redirect');
}

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

namespace Contributte\Comgate\Entity\Response;

class PaymentStatusResponse extends AbstractResponseEntity
{

public function getMerchant(): string
{
return $this->getRequiredString('merchant');
}

public function getTest(): bool
{
return $this->getRequiredBool('test');
}

public function getPrice(): int
{
return $this->getRequiredInteger('price');
}

public function getCurr(): string
{
return $this->getRequiredString('curr');
}

public function getLabel(): string
{
return $this->getRequiredString('label');
}

public function getRefId(): string
{
return $this->getRequiredString('refId');
}

public function getPayerId(): ?string
{
return $this->getString('payerId');
}

public function getMethod(): ?string
{
return $this->getString('method');
}

public function getAccount(): ?string
{
return $this->getString('account');
}

public function getEmail(): string
{
return $this->getRequiredString('email');
}

public function getName(): ?string
{
return $this->getString('name');
}

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

public function getSecret(): string
{
return $this->getRequiredString('secret');
}

public function getStatus(): string
{
return $this->getRequiredString('status');
}

public function getPayerName(): ?string
{
return $this->getString('payerName');
}

public function getPayerAcc(): ?string
{
return $this->getString('payerAcc');
}

public function getFee(): string
{
return $this->getString('fee', 'unknown');
}

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

namespace Contributte\Comgate\Entity\Response;

class RecurringPaymentResponse extends AbstractResponseEntity
{

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

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

namespace Contributte\Comgate\Entity\Response;

class RefundResponse extends AbstractResponseEntity
{

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

namespace Contributte\Comgate\Entity\Response;

class StornoResponse extends AbstractResponseEntity
{

}
10 changes: 10 additions & 0 deletions src/Exceptions/Runtime/InvalidComGateDataException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);

namespace Contributte\Comgate\Exceptions\Runtime;

use Contributte\Comgate\Exceptions\RuntimeException;

class InvalidComGateDataException extends RuntimeException
{

}
26 changes: 15 additions & 11 deletions src/Gateway/PaymentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
use Contributte\Comgate\Entity\PaymentStatus;
use Contributte\Comgate\Entity\RecurringPayment;
use Contributte\Comgate\Entity\Refund;
use Contributte\Comgate\Entity\Response\PaymentResponse;
use Contributte\Comgate\Entity\Response\PaymentStatusResponse;
use Contributte\Comgate\Entity\Response\RecurringPaymentResponse;
use Contributte\Comgate\Entity\Response\RefundResponse;
use Contributte\Comgate\Entity\Response\StornoResponse;
use Contributte\Comgate\Entity\Storno;
use Contributte\Comgate\Http\HttpClient;
use Contributte\Comgate\Http\Response;

class PaymentService
{
Expand All @@ -21,37 +25,37 @@ public function __construct(HttpClient $client)
$this->client = $client;
}

public function create(Payment $payment): Response
public function create(Payment $payment): PaymentResponse
{
$data = $payment->toArray();

return $this->client->post('create', $data);
return new PaymentResponse($this->client->post('create', $data));
}

public function recurring(RecurringPayment $payment): Response
public function recurring(RecurringPayment $payment): RecurringPaymentResponse
{
$data = $payment->toArray();

return $this->client->post('recurring', $data);
return new RecurringPaymentResponse($this->client->post('recurring', $data));
}

public function status(PaymentStatus $status): Response
public function status(PaymentStatus $status): PaymentStatusResponse
{
return $this->client->post('status', $status->toArray());
return new PaymentStatusResponse($this->client->post('status', $status->toArray()));
}

public function refund(Refund $payment): Response
public function refund(Refund $payment): RefundResponse
{
$data = $payment->toArray();

return $this->client->post('refund', $data);
return new RefundResponse($this->client->post('refund', $data));
}

public function storno(Storno $payment): Response
public function storno(Storno $payment): StornoResponse
{
$data = $payment->toArray();

return $this->client->post('cancel', $data);
return new StornoResponse($this->client->post('cancel', $data));
}

}
5 changes: 0 additions & 5 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public function getStatusCode(): int
return $this->origin->getStatusCode();
}

public function isOk(): bool
{
return ($this->getParsedBody()['code'] ?? -1) === '0';
}

/**
* @return mixed[]
*/
Expand Down
Loading

0 comments on commit 1f72559

Please sign in to comment.