diff --git a/src/Client/Response.php b/src/Client/Response.php new file mode 100644 index 0000000..529e49b --- /dev/null +++ b/src/Client/Response.php @@ -0,0 +1,74 @@ +collect()->toArray()) + ); + } + + public function toArray(): array + { + return $this->data->toArray(); + } + + public function offsetExists(mixed $offset): bool + { + return isset($this->data[$offset]); + } + + public function offsetGet(mixed $offset): mixed + { + return $this->data->get($offset); + } + + public function offsetSet(mixed $offset, mixed $value): void + { + // + } + + public function offsetUnset(mixed $offset): void + { + unset($this->data[$offset]); + } + + public function __get($key) + { + return $this->data->get($key); + } + + public function __isset(mixed $key) + { + return $this->offsetExists($key); + } + + public function __unset(mixed $key) + { + $this->offsetUnset($key); + } +} diff --git a/src/DTO/BaseDto.php b/src/DTO/BaseDto.php index 407b897..317c1c4 100644 --- a/src/DTO/BaseDto.php +++ b/src/DTO/BaseDto.php @@ -7,15 +7,8 @@ use Illuminate\Support\Fluent; use Routegroup\Imoje\Payment\Lib\Utils; -/** - * @template TKey of array-key - * @template TValue - */ abstract class BaseDto extends Fluent { - /** - * @var array - */ protected array $casts = []; public function __construct($attributes = []) @@ -24,10 +17,6 @@ public function __construct($attributes = []) parent::__construct($attributes); } - /** - * @param array $attributes - * @return array $attributes - */ private function castAttributes(array $attributes): array { foreach ($this->casts as $attributeKey => $cast) { @@ -58,20 +47,13 @@ public function castAttribute(string $castType, mixed $value): mixed }; } - /** - * @return array $attributes - */ public function toArray(): array { return app(Utils::class)->transformValues($this->attributes); } - /** - * @param array $attributes - */ public static function make(array $attributes): BaseDto { - /* @phpstan-ignore-next-line */ return new static($attributes); } } diff --git a/src/DTO/Response/Api/PaymentProfileResponseDto.php b/src/DTO/Response/Api/PaymentProfileResponseDto.php index 35fe178..c032643 100644 --- a/src/DTO/Response/Api/PaymentProfileResponseDto.php +++ b/src/DTO/Response/Api/PaymentProfileResponseDto.php @@ -21,9 +21,6 @@ */ class PaymentProfileResponseDto extends BaseDto { - /** - * {@inheritdoc} - */ protected array $casts = [ 'year' => 'int', 'month' => 'int', diff --git a/src/DTO/Response/Api/PaymentResponseDto.php b/src/DTO/Response/Api/PaymentResponseDto.php index c545f7d..e6a6d92 100644 --- a/src/DTO/Response/Api/PaymentResponseDto.php +++ b/src/DTO/Response/Api/PaymentResponseDto.php @@ -22,9 +22,6 @@ */ class PaymentResponseDto extends BaseDto { - /** - * {@inheritdoc} - */ protected array $casts = [ 'amount' => 'int', 'created' => 'int', diff --git a/src/DTO/Response/Api/TransactionResponseDto.php b/src/DTO/Response/Api/TransactionResponseDto.php index 17a5e94..f365a7a 100644 --- a/src/DTO/Response/Api/TransactionResponseDto.php +++ b/src/DTO/Response/Api/TransactionResponseDto.php @@ -31,9 +31,6 @@ */ class TransactionResponseDto extends BaseDto { - /** - * {@inheritdoc} - */ protected array $casts = [ 'amount' => 'int', 'created' => 'int', diff --git a/src/DTO/Response/CancelledResponseDto.php b/src/DTO/Response/CancelledResponseDto.php index 50248f8..f8cf5bf 100644 --- a/src/DTO/Response/CancelledResponseDto.php +++ b/src/DTO/Response/CancelledResponseDto.php @@ -12,9 +12,6 @@ */ class CancelledResponseDto extends BaseDto { - /** - * {@inheritdoc} - */ protected array $casts = [ 'payment' => PaymentResponseDto::class, ]; diff --git a/src/DTO/Response/ChargeProfileResponseDto.php b/src/DTO/Response/ChargeProfileResponseDto.php index 147fdaf..ff5a4b2 100644 --- a/src/DTO/Response/ChargeProfileResponseDto.php +++ b/src/DTO/Response/ChargeProfileResponseDto.php @@ -12,9 +12,6 @@ */ class ChargeProfileResponseDto extends BaseDto { - /** - * {@inheritdoc} - */ protected array $casts = [ 'transaction' => TransactionResponseDto::class, ]; diff --git a/src/DTO/Response/OneClickResponseDto.php b/src/DTO/Response/OneClickResponseDto.php index 855e7c0..f81c2cb 100644 --- a/src/DTO/Response/OneClickResponseDto.php +++ b/src/DTO/Response/OneClickResponseDto.php @@ -14,9 +14,6 @@ */ class OneClickResponseDto extends BaseDto { - /** - * {@inheritdoc} - */ protected array $casts = [ 'transaction' => TransactionResponseDto::class, 'payment' => PaymentResponseDto::class, diff --git a/src/DTO/Response/ProfileResponseDto.php b/src/DTO/Response/ProfileResponseDto.php new file mode 100644 index 0000000..fd1298f --- /dev/null +++ b/src/DTO/Response/ProfileResponseDto.php @@ -0,0 +1,18 @@ + PaymentProfileResponseDto::class, + ]; +} diff --git a/src/DTO/Response/RefundResponseDto.php b/src/DTO/Response/RefundResponseDto.php new file mode 100644 index 0000000..3bec58f --- /dev/null +++ b/src/DTO/Response/RefundResponseDto.php @@ -0,0 +1,18 @@ + TransactionResponseDto::class, + ]; +} diff --git a/src/Exceptions/CantChangeTransactionStatus.php b/src/Exceptions/CantChangeTransactionStatus.php index e67b537..e4190c5 100644 --- a/src/Exceptions/CantChangeTransactionStatus.php +++ b/src/Exceptions/CantChangeTransactionStatus.php @@ -8,8 +8,5 @@ class CantChangeTransactionStatus extends InvalidArgumentException { - /** - * {@inheritdoc} - */ protected $message = 'Cant change transaction status.'; } diff --git a/src/Exceptions/InvalidDtoNameException.php b/src/Exceptions/InvalidDtoNameException.php new file mode 100644 index 0000000..ae1ab6a --- /dev/null +++ b/src/Exceptions/InvalidDtoNameException.php @@ -0,0 +1,12 @@ +url->createRefundUrl($transactionId); - return $this - ->request() - ->post($url, $dto); + return Response::resolve( + $this->request()->post($url, $dto), + RefundResponseDto::class + ); } public function getProfile( string $profileId ): Response { - return $this->request()->get(); + $url = $this->url->createProfileIdUrl($profileId); + + return Response::resolve( + $this->request()->get($url), + ProfileResponseDto::class + ); } public function chargeProfile( @@ -40,9 +49,10 @@ public function chargeProfile( ): Response { $url = $this->url->createChargeProfileUrl(); - return $this - ->request() - ->post($url, $dto); + return Response::resolve( + $this->request()->post($url, $dto), + ChargeProfileResponseDto::class + ); } public function deactivateProfile( @@ -50,9 +60,10 @@ public function deactivateProfile( ): Response { $url = $this->url->createProfileIdUrl($profileId); - return $this - ->request() - ->delete($url); + return Response::resolve( + $this->request()->delete($url), + ProfileResponseDto::class + ); } protected function request(): PendingRequest diff --git a/tests/Lib/ApiTest.php b/tests/Lib/ApiTest.php index 5e6905a..f3dc48b 100644 --- a/tests/Lib/ApiTest.php +++ b/tests/Lib/ApiTest.php @@ -2,6 +2,7 @@ use Illuminate\Http\Client\Request; use Illuminate\Support\Facades\Http; +use Routegroup\Imoje\Payment\Client\Response; use Routegroup\Imoje\Payment\DTO\Request\ChargeProfileRequestDto; use Routegroup\Imoje\Payment\DTO\Request\RefundRequestDto; use Routegroup\Imoje\Payment\Lib\Api; @@ -12,10 +13,23 @@ Http::fake(); $this->api = app(Api::class); $this->url = app(Url::class); + + // @todo replace with tests... + $mock = Mockery::mock('overload:'.Response::class); + $mock + ->allows('resolve') + ->andReturnSelf(); +}); + +afterEach(function (): void { + Mockery::close(); }); it('makes refund request', function (): void { - $dto = RefundRequestDto::make(['amount' => 100]); + $dto = RefundRequestDto::make([ + 'amount' => 100, + ]); + $this->api->createRefund($dto, '$transaction_id$'); Http::assertSent(function (Request $request) {