-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5da7363
commit 1f72559
Showing
10 changed files
with
305 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.