diff --git a/.phpstan/baseline.neon b/.phpstan/baseline.neon index 3c80de2..229818d 100644 --- a/.phpstan/baseline.neon +++ b/.phpstan/baseline.neon @@ -25,6 +25,16 @@ parameters: count: 1 path: ../src/MobilePay/AppPayment/Payments/GetPaymentsResponseTest.php + - + message: "#^Parameter \\#1 \\$payload of class MobilePayPhp\\\\MobilePay\\\\AppPayment\\\\Refunds\\\\CreateRefundResponse constructor expects array\\, array given\\.$#" + count: 1 + path: ../src/MobilePay/AppPayment/Refunds/CreateRefundResponseTest.php + + - + message: "#^Parameter \\#1 \\$payload of class MobilePayPhp\\\\MobilePay\\\\AppPayment\\\\Refunds\\\\GetRefundResponse constructor expects array\\, array given\\.$#" + count: 1 + path: ../src/MobilePay/AppPayment/Refunds/GetRefundResponseTest.php + - message: "#^Parameter \\#2 \\$array of function array_map expects array, array\\|int\\|string given\\.$#" count: 1 @@ -34,3 +44,28 @@ parameters: message: "#^Parameter \\#1 \\$payload of class MobilePayPhp\\\\MobilePay\\\\AppPayment\\\\Refunds\\\\GetRefundsResponse constructor expects array\\, array\\\\>\\|int\\> given\\.$#" count: 1 path: ../src/MobilePay/AppPayment/Refunds/GetRefundsResponseTest.php + + - + message: "#^Parameter \\#1 \\$payload of class MobilePayPhp\\\\MobilePay\\\\Webhook\\\\Response\\\\SingleWebhook constructor expects array\\{events\\: array, signatureKey\\: string, url\\: string, webhookId\\: string, paymentPointId\\?\\: string\\}, array given\\.$#" + count: 1 + path: ../src/MobilePay/Webhook/Response/SingleWebhook.php + + - + message: "#^Parameter \\#1 \\$callback of function array_map expects \\(callable\\(array\\|string\\)\\: mixed\\)\\|null, Closure\\(array\\)\\: MobilePayPhp\\\\MobilePay\\\\Webhook\\\\Response\\\\SingleWebhook given\\.$#" + count: 1 + path: ../src/MobilePay/Webhook/Response/WebhookCollection.php + + - + message: "#^Parameter \\#1 \\$payload of class MobilePayPhp\\\\MobilePay\\\\Webhook\\\\Response\\\\SingleWebhook constructor expects array\\{events\\: array, signatureKey\\: string, url\\: string, webhookId\\: string, paymentPointId\\?\\: string\\}, array given\\.$#" + count: 1 + path: ../src/MobilePay/Webhook/Response/WebhookCollection.php + + - + message: "#^Parameter \\#1 \\$payload of class MobilePayPhp\\\\MobilePay\\\\Webhook\\\\Response\\\\WebhookCollection constructor expects array\\{webhooks\\: array\\{events\\: array, signatureKey\\: string, url\\: string, webhookId\\: string, paymentPointId\\?\\: string\\}\\}, array given\\.$#" + count: 1 + path: ../src/MobilePay/Webhook/Response/WebhookCollection.php + + - + message: "#^Strict comparison using \\=\\=\\= between false and non\\-empty\\-string will always evaluate to false\\.$#" + count: 1 + path: ../src/MobilePay/Webhook/Schema/Validator.php diff --git a/.phpstan/cognitive-complexity.neon b/.phpstan/cognitive-complexity.neon index c142af5..babf08d 100644 --- a/.phpstan/cognitive-complexity.neon +++ b/.phpstan/cognitive-complexity.neon @@ -1,15 +1,2 @@ includes: - - ../vendor/symplify/phpstan-rules/config/packages/cognitive-complexity/cognitive-complexity-services.neon - -services: - - - class: Symplify\PHPStanRules\CognitiveComplexity\Rules\FunctionLikeCognitiveComplexityRule - tags: [phpstan.rules.rule] - arguments: - maxMethodCognitiveComplexity: 8 - - - - class: Symplify\PHPStanRules\CognitiveComplexity\Rules\ClassLikeCognitiveComplexityRule - tags: [phpstan.rules.rule] - arguments: - maxClassCognitiveComplexity: 50 + - ../vendor/tomasvotruba/cognitive-complexity/config/extension.neon \ No newline at end of file diff --git a/.phpstan/config.neon b/.phpstan/config.neon index 3326f0a..740a2d5 100644 --- a/.phpstan/config.neon +++ b/.phpstan/config.neon @@ -1,4 +1,5 @@ includes: + - ../vendor/phpstan/phpstan-phpunit/extension.neon - cognitive-complexity.neon - type-coverage.neon diff --git a/.phpstan/type-coverage.neon b/.phpstan/type-coverage.neon index 74fee4c..db50ec3 100644 --- a/.phpstan/type-coverage.neon +++ b/.phpstan/type-coverage.neon @@ -1,33 +1,2 @@ includes: - - ../vendor/symplify/phpstan-rules/config/services/services.neon - -services: - - - class: Symplify\PHPStanRules\Rules\Explicit\PropertyTypeDeclarationSeaLevelRule - tags: [phpstan.rules.rule] - arguments: - minimalLevel: 0.99 - #printSuggestions: false - - - class: Symplify\PHPStanRules\Rules\Explicit\ParamTypeDeclarationSeaLevelRule - tags: [phpstan.rules.rule] - arguments: - minimalLevel: 0.92 - #printSuggestions: false - - - - class: Symplify\PHPStanRules\Rules\Explicit\ReturnTypeDeclarationSeaLevelRule - tags: [phpstan.rules.rule] - arguments: - minimalLevel: 0.94 - #printSuggestions: false - - - - class: Symplify\PHPStanRules\Collector\FunctionLike\ParamTypeSeaLevelCollector - tags: [phpstan.collector] - - - class: Symplify\PHPStanRules\Collector\FunctionLike\ReturnTypeSeaLevelCollector - tags: [phpstan.collector] - - - class: Symplify\PHPStanRules\Collector\ClassLike\PropertyTypeSeaLevelCollector - tags: [phpstan.collector] \ No newline at end of file + - ../vendor/tomasvotruba/type-coverage/config/extension.neon \ No newline at end of file diff --git a/TODO.md b/TODO.md index 2e5d958..ce3fa39 100644 --- a/TODO.md +++ b/TODO.md @@ -47,9 +47,11 @@ ## github project structure --- mobilepay-php ----- mobilepay-php-sdk-src ----- mobilepay-php-sdk ----- mobilepay-bundle ----- mobilepay-recipe ----- omnipay-mobilepay +- mobilepay-php + - mobilepay-php-sdk-src + - mobilepay-php-sdk + - mobilepay-bundle + - mobilepay-recipe + - omnipay-mobilepay + - webhook-provider-symfony + - sylius-mobilepay-plugin diff --git a/composer.json b/composer.json index 674052f..f5f1e2f 100644 --- a/composer.json +++ b/composer.json @@ -29,20 +29,24 @@ }, "require": { "php": ">=8.1", - "psr/http-client": "^1.0", - "psr/http-factory": "^1.0", - "ramsey/uuid": "^4.5" + "justinrainbow/json-schema": "^5.2.12", + "psr/http-client": "^1.0.1", + "psr/http-factory": "^1.0.1", + "ramsey/uuid": "^4.7.1" }, "require-dev": { - "brianium/paratest": "^6.6", - "friendsofphp/php-cs-fixer": "^3.13", - "nyholm/psr7": "^1.5", - "phpstan/phpstan": "^1.9", - "phpunit/phpunit": "^9", - "rector/rector": "^0.14.7", - "spatie/phpunit-snapshot-assertions": "^4.2", - "symfony/http-client": "^6.1", - "symplify/phpstan-rules": "^11.1" + "brianium/paratest": "^6.8", + "friendsofphp/php-cs-fixer": "^3.13.2", + "nyholm/psr7": "^1.5.1", + "phpstan/phpstan": "^1.9.7", + "phpstan/phpstan-phpunit": "^1.3.3", + "phpunit/phpunit": "^9.5.27", + "rector/rector": "^0.14.8", + "spatie/phpunit-snapshot-assertions": "^4.2.16", + "symfony/http-client": "^6.2.2", + "symplify/phpstan-rules": "^11.1.24", + "tomasvotruba/cognitive-complexity": "^0.0.1", + "tomasvotruba/type-coverage": "^0.0.1" }, "provide": { "php-http/client-implementation": "*" diff --git a/phpstan.neon b/phpstan.neon index 4b9d644..3cca21f 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -6,3 +6,13 @@ includes: parameters: paths: - src + + cognitive_complexity: + class: 50 + function: 8 + + type_coverage: + return_type: 78 + param_type: 84 + property_type: 20 + print_suggestions: false diff --git a/rector.php b/rector.php index ef999e9..b13822f 100644 --- a/rector.php +++ b/rector.php @@ -6,18 +6,30 @@ use Rector\Config\RectorConfig; use Rector\PHPUnit\Rector\Class_\AddSeeTestAnnotationRector; use Rector\Set\ValueObject\LevelSetList; +use Rector\Set\ValueObject\SetList; return static function (RectorConfig $rectorConfig): void { $rectorConfig->paths([ - __DIR__ . '/src' + __DIR__.'/src', ]); - # php - $rectorConfig->sets([LevelSetList::UP_TO_PHP_80]); - $rectorConfig->sets([LevelSetList::UP_TO_PHP_81]); - - $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + $rectorConfig->skip([ + // bug: rector ends in an error refactoring this class + __DIR__.'/src/MobilePay/AppPayment/PaymentPoints/PaymentPointState.php', + // bug: rector applies FinalizeClassesWithoutChildrenRector + __DIR__.'/src/Api/Exception/ClientException.php', + ]); - # phpunit - $rectorConfig->rule(AddSeeTestAnnotationRector::class); + $rectorConfig->rules([ + InlineConstructorDefaultToPropertyRector::class, + AddSeeTestAnnotationRector::class, + ]); + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_81, + SetList::CODE_QUALITY, + SetList::DEAD_CODE, + SetList::EARLY_RETURN, + SetList::TYPE_DECLARATION, + SetList::PRIVATIZATION, + ]); }; diff --git a/src/Api/Client.php b/src/Api/Client.php index 4adb203..3f853d8 100644 --- a/src/Api/Client.php +++ b/src/Api/Client.php @@ -24,6 +24,11 @@ public function request(RequestInterface $request): ResponseInterface { $psr7Request = $this->requestFactory->createRequest($request->getMethod(), $this->apiHost.$request->getUri()); + // todo: add custom header + // foreach($request->getHeaders() as $header) { + // $psr7Request = $psr7Request->withHeader($header->getName(), $header->getValue()); + // } + $psr7Request = $psr7Request ->withHeader('Accept', 'application/json') ->withHeader('Content-Type', 'application/json') @@ -31,8 +36,13 @@ public function request(RequestInterface $request): ResponseInterface ; $requestBody = $request->getBody(); - if (!empty($requestBody)) { - $psr7Request = $psr7Request->withBody($this->streamFactory->createStream(json_encode($requestBody, JSON_THROW_ON_ERROR))); + if (null !== $requestBody) { + $psr7Request = $psr7Request->withBody( + $this->streamFactory->createStream( + // todo: add custom encoder + json_encode($requestBody, JSON_THROW_ON_ERROR) + ) + ); } $psr7Response = $this->httpClient->sendRequest($psr7Request); @@ -42,6 +52,7 @@ public function request(RequestInterface $request): ResponseInterface return $this->responseHandler->handle( $statusCode, + // todo: add custom decoder empty($contents) ? [] : json_decode($contents, true, 512, JSON_THROW_ON_ERROR) ); } diff --git a/src/Api/Exception/ClientException.php b/src/Api/Exception/ClientException.php index c9e7d81..07f848b 100644 --- a/src/Api/Exception/ClientException.php +++ b/src/Api/Exception/ClientException.php @@ -4,6 +4,9 @@ namespace MobilePayPhp\Api\Exception; +/** + * @see \MobilePayPhp\Api\Exception\ClientExceptionTest + */ class ClientException extends ResponseException { public function __construct(int $code, string $message) diff --git a/src/Api/Exception/ServerException.php b/src/Api/Exception/ServerException.php index 0b90404..2d9d7c4 100644 --- a/src/Api/Exception/ServerException.php +++ b/src/Api/Exception/ServerException.php @@ -4,7 +4,10 @@ namespace MobilePayPhp\Api\Exception; -class ServerException extends ResponseException +/** + * @see \MobilePayPhp\Api\Exception\ServerExceptionTest + */ +final class ServerException extends ResponseException { public function __construct(int $code, string $message) { diff --git a/src/Api/IsDeleteTrait.php b/src/Api/IsDeleteTrait.php new file mode 100644 index 0000000..e3f5c4b --- /dev/null +++ b/src/Api/IsDeleteTrait.php @@ -0,0 +1,21 @@ +validator = new Validator(); + } + + private function validate(array $payload, string $schemaPath): void + { + // JsonSchema Validator needs an object + if (false === $json = json_encode($payload, JSON_THROW_ON_ERROR)) { + // todo throw ValidationException + // throw ValidationException::jsonError(); + } + + $data = json_decode($json, false, 512, JSON_THROW_ON_ERROR); + + $this->validator->validate($data, (object) ['$ref' => 'file://'.$schemaPath]); + + if (!$this->validator->isValid()) { + throw ValidationException::fromValidationErrors(array_map(static fn (array $error): \MobilePayPhp\Api\ValidationError => ValidationError::fromArray($error), $this->validator->getErrors())); + } + } +} + +final class ValidationException extends \RuntimeException +{ + public function __construct( + private readonly array $validationErrors + ) { + } + + public static function fromValidationErrors(array $validationErrors): self + { + return new self($validationErrors); + } + + /** + * @return mixed[] + */ + public function getValidationErrors(): array + { + return $this->validationErrors; + } +} + +final class ValidationError +{ + public function __construct( + private readonly string $property, + private readonly string $message + ) { + } + + public static function fromArray(array $error): self + { + return new self($error['property'], $error['message']); + } + + public function getProperty(): string + { + return $this->property; + } + + public function getMessage(): string + { + return $this->message; + } +} diff --git a/src/Api/Response.php b/src/Api/Response.php index 058e252..c254d1a 100644 --- a/src/Api/Response.php +++ b/src/Api/Response.php @@ -20,6 +20,9 @@ public function getStatusCode(): int return $this->statusCode; } + /** + * @return mixed[] + */ public function getBody(): array { return $this->body; diff --git a/src/Api/RetryClientTest.php b/src/Api/RetryClientTest.php index 18fc5d2..eae0d30 100644 --- a/src/Api/RetryClientTest.php +++ b/src/Api/RetryClientTest.php @@ -31,7 +31,7 @@ public function test_client_is_called_three_times(): void } } -class TestClient implements ClientInterface +final class TestClient implements ClientInterface { public array $requests = []; @@ -43,7 +43,7 @@ public function request(RequestInterface $request): ResponseInterface } } -class TestRequest implements RequestInterface +final class TestRequest implements RequestInterface { use IsGetTrait; diff --git a/src/Api/Validation/ValidationTrait.php b/src/Api/Validation/ValidationTrait.php index 6469bd9..975a8a2 100644 --- a/src/Api/Validation/ValidationTrait.php +++ b/src/Api/Validation/ValidationTrait.php @@ -20,7 +20,7 @@ final public static function validate(array $data, ValidationRule ...$validation { // 1. all mandatory are there $neededProperties = array_filter( - array_map(fn (ValidationRule $validationRule) => $validationRule->isMandatory() ? $validationRule->getName() : null, $validationRules) + array_map(fn (ValidationRule $validationRule): ?string => $validationRule->isMandatory() ? $validationRule->getName() : null, $validationRules) ); if ($missingData = array_diff_key(array_flip($neededProperties), $data)) { @@ -40,6 +40,9 @@ final public static function validate(array $data, ValidationRule ...$validation } } + /** + * @return mixed[] + */ private static function findEmptyProperties(array $neededProperties, array $data): array { $emptyProperties = []; @@ -52,6 +55,9 @@ private static function findEmptyProperties(array $neededProperties, array $data return $emptyProperties; } + /** + * @return mixed[] + */ private static function findInvalidProperties(array $validationRules, array $data): array { $invalidProperties = []; diff --git a/src/MobilePay/AppPayment/ClientTestTrait.php b/src/MobilePay/AppPayment/ClientTestTrait.php index 24fc228..b6d9aa8 100644 --- a/src/MobilePay/AppPayment/ClientTestTrait.php +++ b/src/MobilePay/AppPayment/ClientTestTrait.php @@ -6,7 +6,10 @@ use MobilePayPhp\Api\Client; use MobilePayPhp\Api\ClientInterface; +use MobilePayPhp\Api\RetryClient; use MobilePayPhp\MobilePay\AppPayment\Payments\Client as PaymentsClient; +use MobilePayPhp\MobilePay\Id; +use MobilePayPhp\MobilePay\ResponseHandler; use Symfony\Component\HttpClient\HttpClient; use Symfony\Component\HttpClient\Psr18Client; @@ -17,7 +20,7 @@ trait ClientTestTrait { private PaymentsClient $paymentsClient; - private function getMobilePayClient(): ClientInterface + private function getMobilePayClient(bool $retry = false): ClientInterface { $apiHost = 'https://api.sandbox.mobilepay.dk'; @@ -28,8 +31,9 @@ private function getMobilePayClient(): ClientInterface $httpClient = HttpClient::create(); $psr18HttpClient = new Psr18Client($httpClient); + $mobilePayClient = new Client($psr18HttpClient, $psr18HttpClient, $psr18HttpClient, new ResponseHandler(), $apiHost, $apiKey); - return new Client($psr18HttpClient, $psr18HttpClient, $psr18HttpClient, new ResponseHandler(), $apiHost, $apiKey); + return $retry ? new RetryClient($mobilePayClient) : $mobilePayClient; } private function createPayment(): Id diff --git a/src/MobilePay/AppPayment/Exception/MobilePayClientException.php b/src/MobilePay/AppPayment/Exception/MobilePayClientException.php index 804b5e5..eeb2c19 100644 --- a/src/MobilePay/AppPayment/Exception/MobilePayClientException.php +++ b/src/MobilePay/AppPayment/Exception/MobilePayClientException.php @@ -8,7 +8,7 @@ use MobilePayPhp\Api\Validation\ValidationRule; use MobilePayPhp\Api\Validation\ValidationTrait; -class MobilePayClientException extends ClientException +final class MobilePayClientException extends ClientException { use ValidationTrait; diff --git a/src/MobilePay/AppPayment/PaymentPoints/Client.php b/src/MobilePay/AppPayment/PaymentPoints/Client.php index 5c45cc9..75eabec 100644 --- a/src/MobilePay/AppPayment/PaymentPoints/Client.php +++ b/src/MobilePay/AppPayment/PaymentPoints/Client.php @@ -6,9 +6,6 @@ use MobilePayPhp\Api\ClientInterface; -/** - * @see \MobilePayPhp\MobilePay\AppPayment\PaymentPointsGatewayTest - */ final class Client { public function __construct(private readonly ClientInterface $client) diff --git a/src/MobilePay/AppPayment/PaymentPoints/ClientTest.php b/src/MobilePay/AppPayment/PaymentPoints/ClientTest.php new file mode 100644 index 0000000..11d79f0 --- /dev/null +++ b/src/MobilePay/AppPayment/PaymentPoints/ClientTest.php @@ -0,0 +1,61 @@ + 10, + 'paymentPoints' => [[ + 'paymentPointId' => '56cefd9e-71a4-403c-927a-28f4f35fcc2d', + 'paymentPointName' => 'Test', + 'state' => 'active', + ]], + ]))); + + $paymentPointRequest = $client->getPaymentPoints(); + + static::assertSame(10, $paymentPointRequest->getPageSize()); + static::assertNull($paymentPointRequest->getNextPageNumber()); + static::assertCount(1, $paymentPoints = $paymentPointRequest->getPaymentPoints()); + static::assertSame('56cefd9e-71a4-403c-927a-28f4f35fcc2d', $paymentPoints[0]->getPaymentPointId()->toString()); + static::assertSame('Test', $paymentPoints[0]->getPaymentPointName()); + static::assertTrue($paymentPoints[0]->getState()->isActive()); + } +} + +class MockPaymentPointsClient implements ClientInterface +{ + public function __construct( + private readonly ResponseInterface $response + ) { + } + + public function request(RequestInterface $request): ResponseInterface + { + return $this->response; + } +} diff --git a/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointResponse.php b/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointResponse.php index 17e0da2..c60bb73 100644 --- a/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointResponse.php +++ b/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointResponse.php @@ -6,7 +6,7 @@ use MobilePayPhp\Api\Validation\ValidationRule; use MobilePayPhp\Api\Validation\ValidationTrait; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; /** * @see \MobilePayPhp\MobilePay\AppPayment\PaymentPoints\GetPaymentPointResponseTest diff --git a/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointResponseTest.php b/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointResponseTest.php index 1a2ffe3..8fef46c 100644 --- a/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointResponseTest.php +++ b/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointResponseTest.php @@ -10,7 +10,7 @@ * @covers \MobilePayPhp\MobilePay\AppPayment\PaymentPoints\GetPaymentPointResponse * * @uses \MobilePayPhp\Api\Validation\ValidationRule - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * @uses \MobilePayPhp\MobilePay\AppPayment\PaymentPoints\PaymentPointState * * @group unit diff --git a/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointsRequestTest.php b/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointsRequestTest.php index 93dec1a..aa5d203 100644 --- a/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointsRequestTest.php +++ b/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointsRequestTest.php @@ -10,7 +10,7 @@ * @covers \MobilePayPhp\MobilePay\AppPayment\PaymentPoints\GetPaymentPointsRequest * @covers \MobilePayPhp\Api\IsGetTrait * - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * * @group unit */ diff --git a/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointsResponse.php b/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointsResponse.php index 63d810f..25dbec2 100644 --- a/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointsResponse.php +++ b/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointsResponse.php @@ -35,7 +35,7 @@ public function __construct(array $payload) $this->pageSize = (int) $payload['pageSize']; $this->nextPageNumber = isset($payload['nextPageNumber']) ? (int) $payload['nextPageNumber'] : null; - $this->paymentPoints = array_map(fn (array $paymentPoint) => new GetPaymentPointResponse($paymentPoint), $payload['paymentPoints'] ?? []); + $this->paymentPoints = array_map(fn (array $paymentPoint): GetPaymentPointResponse => new GetPaymentPointResponse($paymentPoint), $payload['paymentPoints'] ?? []); } public static function fromResponse(ResponseInterface $response): self diff --git a/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointsResponseTest.php b/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointsResponseTest.php index fd3e020..bacfd70 100644 --- a/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointsResponseTest.php +++ b/src/MobilePay/AppPayment/PaymentPoints/GetPaymentPointsResponseTest.php @@ -12,7 +12,7 @@ * * @uses \MobilePayPhp\Api\Response * @uses \MobilePayPhp\Api\Validation\ValidationRule - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * @uses \MobilePayPhp\MobilePay\AppPayment\PaymentPoints\GetPaymentPointResponse * @uses \MobilePayPhp\MobilePay\AppPayment\PaymentPoints\PaymentPointState * diff --git a/src/MobilePay/AppPayment/Payments/CancelPaymentRequest.php b/src/MobilePay/AppPayment/Payments/CancelPaymentRequest.php index a617ae8..ec27b5d 100644 --- a/src/MobilePay/AppPayment/Payments/CancelPaymentRequest.php +++ b/src/MobilePay/AppPayment/Payments/CancelPaymentRequest.php @@ -6,7 +6,7 @@ use MobilePayPhp\Api\IsPostTrait; use MobilePayPhp\Api\RequestInterface; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; /** * @see \MobilePayPhp\MobilePay\AppPayment\Payments\CancelPaymentRequestTest diff --git a/src/MobilePay/AppPayment/Payments/CancelPaymentRequestTest.php b/src/MobilePay/AppPayment/Payments/CancelPaymentRequestTest.php index 265fdce..4ebe91a 100644 --- a/src/MobilePay/AppPayment/Payments/CancelPaymentRequestTest.php +++ b/src/MobilePay/AppPayment/Payments/CancelPaymentRequestTest.php @@ -4,14 +4,14 @@ namespace MobilePayPhp\MobilePay\AppPayment\Payments; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; use PHPUnit\Framework\TestCase; /** * @covers \MobilePayPhp\MobilePay\AppPayment\Payments\CancelPaymentRequest * @covers \MobilePayPhp\Api\IsPostTrait * - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * * @group unit */ diff --git a/src/MobilePay/AppPayment/Payments/CapturePaymentRequest.php b/src/MobilePay/AppPayment/Payments/CapturePaymentRequest.php index 5473840..4d93e7a 100644 --- a/src/MobilePay/AppPayment/Payments/CapturePaymentRequest.php +++ b/src/MobilePay/AppPayment/Payments/CapturePaymentRequest.php @@ -7,7 +7,7 @@ use MobilePayPhp\Api\IsPostTrait; use MobilePayPhp\Api\RequestInterface; use MobilePayPhp\MobilePay\AppPayment\Amount; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; /** * @see \MobilePayPhp\MobilePay\AppPayment\Payments\CapturePaymentRequestTest @@ -28,7 +28,7 @@ public function getUri(): string } /** - * @return array{amount: int} + * @return array{amount: int}|null */ public function getBody(): ?array { diff --git a/src/MobilePay/AppPayment/Payments/CapturePaymentRequestTest.php b/src/MobilePay/AppPayment/Payments/CapturePaymentRequestTest.php index 40af9f2..3e12f3a 100644 --- a/src/MobilePay/AppPayment/Payments/CapturePaymentRequestTest.php +++ b/src/MobilePay/AppPayment/Payments/CapturePaymentRequestTest.php @@ -5,7 +5,7 @@ namespace MobilePayPhp\MobilePay\AppPayment\Payments; use MobilePayPhp\MobilePay\AppPayment\Amount; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; use PHPUnit\Framework\TestCase; /** @@ -13,7 +13,7 @@ * @covers \MobilePayPhp\Api\IsPostTrait * * @uses \MobilePayPhp\MobilePay\AppPayment\Amount - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * * @group unit */ diff --git a/src/MobilePay/AppPayment/Payments/Client.php b/src/MobilePay/AppPayment/Payments/Client.php index 7260970..798165a 100644 --- a/src/MobilePay/AppPayment/Payments/Client.php +++ b/src/MobilePay/AppPayment/Payments/Client.php @@ -6,7 +6,7 @@ use MobilePayPhp\Api\ClientInterface; use MobilePayPhp\MobilePay\AppPayment\Amount; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; /** * @see \MobilePayPhp\MobilePay\AppPayment\Payments\ClientTest diff --git a/src/MobilePay/AppPayment/Payments/ClientTest.php b/src/MobilePay/AppPayment/Payments/ClientTest.php index 58d7d0f..9089ac4 100644 --- a/src/MobilePay/AppPayment/Payments/ClientTest.php +++ b/src/MobilePay/AppPayment/Payments/ClientTest.php @@ -6,12 +6,13 @@ use MobilePayPhp\MobilePay\AppPayment\Amount; use MobilePayPhp\MobilePay\AppPayment\ClientTestTrait; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; use PHPUnit\Framework\TestCase; /** * @covers \MobilePayPhp\MobilePay\AppPayment\Payments\Client * @covers \MobilePayPhp\Api\Client + * @covers \MobilePayPhp\Api\RetryClient * @covers \MobilePayPhp\Api\IsGetTrait * @covers \MobilePayPhp\Api\IsPostTrait * @covers \MobilePayPhp\Api\Response @@ -19,7 +20,7 @@ * @covers \MobilePayPhp\Api\Validation\ValidationTrait * @covers \MobilePayPhp\MobilePay\AppPayment\Amount * @covers \MobilePayPhp\MobilePay\AppPayment\DateTimeFactory - * @covers \MobilePayPhp\MobilePay\AppPayment\Id + * @covers \MobilePayPhp\MobilePay\Id * @covers \MobilePayPhp\MobilePay\AppPayment\Payments\CancelPaymentRequest * @covers \MobilePayPhp\MobilePay\AppPayment\Payments\CapturePaymentRequest * @covers \MobilePayPhp\MobilePay\AppPayment\Payments\CreatePaymentRequest @@ -28,7 +29,7 @@ * @covers \MobilePayPhp\MobilePay\AppPayment\Payments\GetPaymentResponse * @covers \MobilePayPhp\MobilePay\AppPayment\Payments\PaymentState * @covers \MobilePayPhp\MobilePay\AppPayment\Payments\ReservePaymentRequest - * @covers \MobilePayPhp\MobilePay\AppPayment\ResponseHandler + * @covers \MobilePayPhp\MobilePay\ResponseHandler * * @group e2e */ @@ -43,7 +44,7 @@ public function setUp(): void static::fail('No paymentPointId is set, check your MOBILEPAY_PAYMENTPOINT_ID env var.'); } - $this->paymentsClient = new Client($this->getMobilePayClient(), $paymentPointId); + $this->paymentsClient = new Client($this->getMobilePayClient(true), $paymentPointId); } public function test_cancelPayment(): void diff --git a/src/MobilePay/AppPayment/Payments/CreatePaymentRequest.php b/src/MobilePay/AppPayment/Payments/CreatePaymentRequest.php index b0ee422..d8d0507 100644 --- a/src/MobilePay/AppPayment/Payments/CreatePaymentRequest.php +++ b/src/MobilePay/AppPayment/Payments/CreatePaymentRequest.php @@ -7,7 +7,7 @@ use MobilePayPhp\Api\IsPostTrait; use MobilePayPhp\Api\RequestInterface; use MobilePayPhp\MobilePay\AppPayment\Amount; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; /** * @see \MobilePayPhp\MobilePay\AppPayment\Payments\CreatePaymentRequestTest @@ -37,7 +37,7 @@ public function getUri(): string } /** - * @return array{amount: int, idempotencyKey: string, paymentPointId: string, redirectUri: string, reference: string, description: string} + * @return array{amount: int, idempotencyKey: string, paymentPointId: string, redirectUri: string, reference: string, description: string}|null */ public function getBody(): ?array { diff --git a/src/MobilePay/AppPayment/Payments/CreatePaymentRequestTest.php b/src/MobilePay/AppPayment/Payments/CreatePaymentRequestTest.php index 0d24ef7..4d2c3b2 100644 --- a/src/MobilePay/AppPayment/Payments/CreatePaymentRequestTest.php +++ b/src/MobilePay/AppPayment/Payments/CreatePaymentRequestTest.php @@ -5,7 +5,7 @@ namespace MobilePayPhp\MobilePay\AppPayment\Payments; use MobilePayPhp\MobilePay\AppPayment\Amount; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; use PHPUnit\Framework\TestCase; /** @@ -13,7 +13,7 @@ * @covers \MobilePayPhp\Api\IsPostTrait * * @uses \MobilePayPhp\MobilePay\AppPayment\Amount - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * * @group unit */ diff --git a/src/MobilePay/AppPayment/Payments/CreatePaymentResponse.php b/src/MobilePay/AppPayment/Payments/CreatePaymentResponse.php index 21755f5..888336c 100644 --- a/src/MobilePay/AppPayment/Payments/CreatePaymentResponse.php +++ b/src/MobilePay/AppPayment/Payments/CreatePaymentResponse.php @@ -7,7 +7,7 @@ use MobilePayPhp\Api\ResponseInterface; use MobilePayPhp\Api\Validation\ValidationRule; use MobilePayPhp\Api\Validation\ValidationTrait; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; /** * @see \MobilePayPhp\MobilePay\AppPayment\Payments\CreatePaymentResponseTest diff --git a/src/MobilePay/AppPayment/Payments/CreatePaymentResponseTest.php b/src/MobilePay/AppPayment/Payments/CreatePaymentResponseTest.php index 1243d50..7d76b7b 100644 --- a/src/MobilePay/AppPayment/Payments/CreatePaymentResponseTest.php +++ b/src/MobilePay/AppPayment/Payments/CreatePaymentResponseTest.php @@ -10,7 +10,7 @@ * @covers \MobilePayPhp\MobilePay\AppPayment\Payments\CreatePaymentResponse * * @uses \MobilePayPhp\Api\Validation\ValidationRule - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * * @group unit */ diff --git a/src/MobilePay/AppPayment/Payments/GetPaymentRequest.php b/src/MobilePay/AppPayment/Payments/GetPaymentRequest.php index 8800d0d..19f4174 100644 --- a/src/MobilePay/AppPayment/Payments/GetPaymentRequest.php +++ b/src/MobilePay/AppPayment/Payments/GetPaymentRequest.php @@ -6,7 +6,7 @@ use MobilePayPhp\Api\IsGetTrait; use MobilePayPhp\Api\RequestInterface; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; /** * @see \MobilePayPhp\MobilePay\AppPayment\Payments\GetPaymentRequestTest diff --git a/src/MobilePay/AppPayment/Payments/GetPaymentRequestTest.php b/src/MobilePay/AppPayment/Payments/GetPaymentRequestTest.php index 73678d2..3021b67 100644 --- a/src/MobilePay/AppPayment/Payments/GetPaymentRequestTest.php +++ b/src/MobilePay/AppPayment/Payments/GetPaymentRequestTest.php @@ -4,14 +4,14 @@ namespace MobilePayPhp\MobilePay\AppPayment\Payments; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; use PHPUnit\Framework\TestCase; /** * @covers \MobilePayPhp\MobilePay\AppPayment\Payments\GetPaymentRequest * @covers \MobilePayPhp\Api\IsPostTrait * - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * * @group unit */ diff --git a/src/MobilePay/AppPayment/Payments/GetPaymentResponse.php b/src/MobilePay/AppPayment/Payments/GetPaymentResponse.php index 6044ec6..e791ce1 100644 --- a/src/MobilePay/AppPayment/Payments/GetPaymentResponse.php +++ b/src/MobilePay/AppPayment/Payments/GetPaymentResponse.php @@ -9,7 +9,7 @@ use MobilePayPhp\Api\Validation\ValidationTrait; use MobilePayPhp\MobilePay\AppPayment\Amount; use MobilePayPhp\MobilePay\AppPayment\DateTimeFactory; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; /** * @see \MobilePayPhp\MobilePay\AppPayment\Payments\GetPaymentResponseTest @@ -75,12 +75,20 @@ public static function fromResponse(ResponseInterface $response): self public function isAmountReserved(Amount $reservedAmount): bool { - return $this->state->isReserved() && $reservedAmount->getAmountInCent() === $this->amount; + if (!$this->state->isReserved()) { + return false; + } + + return $reservedAmount->getAmountInCent() === $this->amount; } public function isAmountCaptured(Amount $capturedAmount): bool { - return $this->state->isCaptured() && $capturedAmount->getAmountInCent() === $this->amount; + if (!$this->state->isCaptured()) { + return false; + } + + return $capturedAmount->getAmountInCent() === $this->amount; } public function getPaymentId(): Id diff --git a/src/MobilePay/AppPayment/Payments/GetPaymentResponseTest.php b/src/MobilePay/AppPayment/Payments/GetPaymentResponseTest.php index 10f970d..611a90f 100644 --- a/src/MobilePay/AppPayment/Payments/GetPaymentResponseTest.php +++ b/src/MobilePay/AppPayment/Payments/GetPaymentResponseTest.php @@ -14,7 +14,7 @@ * @uses \MobilePayPhp\MobilePay\AppPayment\Payments\PaymentState * @uses \MobilePayPhp\MobilePay\AppPayment\Amount * @uses \MobilePayPhp\MobilePay\AppPayment\DateTimeFactory - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * * @group unit */ diff --git a/src/MobilePay/AppPayment/Payments/GetPaymentsResponse.php b/src/MobilePay/AppPayment/Payments/GetPaymentsResponse.php index 7cb84b9..e18b227 100644 --- a/src/MobilePay/AppPayment/Payments/GetPaymentsResponse.php +++ b/src/MobilePay/AppPayment/Payments/GetPaymentsResponse.php @@ -39,7 +39,7 @@ public function __construct(array $payload) $this->pageSize = (int) $payload['pageSize']; $this->nextPageNumber = isset($payload['nextPageNumber']) ? (int) $payload['nextPageNumber'] : null; - $this->payments = array_map(fn (array $payment) => new GetPaymentResponse($payment), $payload['payments'] ?? []); + $this->payments = array_map(fn (array $payment): \MobilePayPhp\MobilePay\AppPayment\Payments\GetPaymentResponse => new GetPaymentResponse($payment), $payload['payments'] ?? []); } public static function fromResponse(ResponseInterface $response): self diff --git a/src/MobilePay/AppPayment/Payments/GetPaymentsResponseTest.php b/src/MobilePay/AppPayment/Payments/GetPaymentsResponseTest.php index 9fc6e1b..9b9ff3b 100644 --- a/src/MobilePay/AppPayment/Payments/GetPaymentsResponseTest.php +++ b/src/MobilePay/AppPayment/Payments/GetPaymentsResponseTest.php @@ -10,7 +10,7 @@ * @covers \MobilePayPhp\MobilePay\AppPayment\Payments\GetPaymentsResponse * @covers \MobilePayPhp\MobilePay\AppPayment\Payments\GetPaymentResponse * - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * @uses \MobilePayPhp\MobilePay\AppPayment\DateTimeFactory * @uses \MobilePayPhp\MobilePay\AppPayment\Payments\PaymentState * @uses \MobilePayPhp\Api\Validation\ValidationRule diff --git a/src/MobilePay/AppPayment/Payments/ReservePaymentRequest.php b/src/MobilePay/AppPayment/Payments/ReservePaymentRequest.php index 5d1a1cb..ebcc326 100644 --- a/src/MobilePay/AppPayment/Payments/ReservePaymentRequest.php +++ b/src/MobilePay/AppPayment/Payments/ReservePaymentRequest.php @@ -6,7 +6,7 @@ use MobilePayPhp\Api\IsPostTrait; use MobilePayPhp\Api\RequestInterface; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; /** * @see \MobilePayPhp\MobilePay\AppPayment\Payments\ReservePaymentRequestTest diff --git a/src/MobilePay/AppPayment/Payments/ReservePaymentRequestTest.php b/src/MobilePay/AppPayment/Payments/ReservePaymentRequestTest.php index e37355f..949c9e0 100644 --- a/src/MobilePay/AppPayment/Payments/ReservePaymentRequestTest.php +++ b/src/MobilePay/AppPayment/Payments/ReservePaymentRequestTest.php @@ -4,14 +4,14 @@ namespace MobilePayPhp\MobilePay\AppPayment\Payments; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; use PHPUnit\Framework\TestCase; /** * @covers \MobilePayPhp\MobilePay\AppPayment\Payments\ReservePaymentRequest * * @uses \MobilePayPhp\Api\IsGetTrait - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * * @group unit */ diff --git a/src/MobilePay/AppPayment/Refunds/Client.php b/src/MobilePay/AppPayment/Refunds/Client.php index 1165866..3a7a62e 100644 --- a/src/MobilePay/AppPayment/Refunds/Client.php +++ b/src/MobilePay/AppPayment/Refunds/Client.php @@ -6,7 +6,7 @@ use MobilePayPhp\Api\ClientInterface; use MobilePayPhp\MobilePay\AppPayment\Amount; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; /** * @see \MobilePayPhp\MobilePay\AppPayment\Refunds\ClientTest diff --git a/src/MobilePay/AppPayment/Refunds/ClientTest.php b/src/MobilePay/AppPayment/Refunds/ClientTest.php index de653de..f5e928c 100644 --- a/src/MobilePay/AppPayment/Refunds/ClientTest.php +++ b/src/MobilePay/AppPayment/Refunds/ClientTest.php @@ -6,14 +6,15 @@ use MobilePayPhp\MobilePay\AppPayment\Amount; use MobilePayPhp\MobilePay\AppPayment\ClientTestTrait; -use MobilePayPhp\MobilePay\AppPayment\Id; use MobilePayPhp\MobilePay\AppPayment\Payments\Client as PaymentsClient; use MobilePayPhp\MobilePay\AppPayment\Refunds\Client as RefundsClient; +use MobilePayPhp\MobilePay\Id; use PHPUnit\Framework\TestCase; /** * @covers \MobilePayPhp\MobilePay\AppPayment\Refunds\Client * @covers \MobilePayPhp\Api\Client + * @covers \MobilePayPhp\Api\RetryClient * @covers \MobilePayPhp\Api\IsGetTrait * @covers \MobilePayPhp\Api\IsPostTrait * @covers \MobilePayPhp\Api\Response @@ -21,12 +22,12 @@ * @covers \MobilePayPhp\Api\Validation\ValidationTrait * @covers \MobilePayPhp\MobilePay\AppPayment\Amount * @covers \MobilePayPhp\MobilePay\AppPayment\DateTimeFactory - * @covers \MobilePayPhp\MobilePay\AppPayment\Id + * @covers \MobilePayPhp\MobilePay\Id * @covers \MobilePayPhp\MobilePay\AppPayment\Refunds\CreateRefundRequest * @covers \MobilePayPhp\MobilePay\AppPayment\Refunds\CreateRefundResponse * @covers \MobilePayPhp\MobilePay\AppPayment\Refunds\GetRefundRequest * @covers \MobilePayPhp\MobilePay\AppPayment\Refunds\GetRefundResponse - * @covers \MobilePayPhp\MobilePay\AppPayment\ResponseHandler + * @covers \MobilePayPhp\MobilePay\ResponseHandler * * @uses \MobilePayPhp\MobilePay\AppPayment\Payments\Client * @uses \MobilePayPhp\MobilePay\AppPayment\Payments\CapturePaymentRequest @@ -52,8 +53,8 @@ public function setUp(): void static::fail('No paymentPointId is set, check your MOBILEPAY_PAYMENTPOINT_ID env var.'); } - $this->paymentsClient = new PaymentsClient($this->getMobilePayClient(), $paymentPointId); - $this->refundsClient = new RefundsClient($this->getMobilePayClient()); + $this->paymentsClient = new PaymentsClient($this->getMobilePayClient(true), $paymentPointId); + $this->refundsClient = new RefundsClient($this->getMobilePayClient(true)); } public function test_refunds(): void diff --git a/src/MobilePay/AppPayment/Refunds/CreateRefundRequest.php b/src/MobilePay/AppPayment/Refunds/CreateRefundRequest.php index 8f6ee6e..96d29f4 100644 --- a/src/MobilePay/AppPayment/Refunds/CreateRefundRequest.php +++ b/src/MobilePay/AppPayment/Refunds/CreateRefundRequest.php @@ -7,7 +7,7 @@ use MobilePayPhp\Api\IsPostTrait; use MobilePayPhp\Api\RequestInterface; use MobilePayPhp\MobilePay\AppPayment\Amount; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; /** * @see \MobilePayPhp\MobilePay\AppPayment\Refunds\CreateRefundRequestTest @@ -35,7 +35,7 @@ public function getUri(): string } /** - * @return array{paymentId: string, amount: int, idempotencyKey: string, reference: string, description: string} + * @return array{paymentId: string, amount: int, idempotencyKey: string, reference: string, description: string}|null */ public function getBody(): ?array { diff --git a/src/MobilePay/AppPayment/Refunds/CreateRefundRequestTest.php b/src/MobilePay/AppPayment/Refunds/CreateRefundRequestTest.php index 0aee8d5..7bed263 100644 --- a/src/MobilePay/AppPayment/Refunds/CreateRefundRequestTest.php +++ b/src/MobilePay/AppPayment/Refunds/CreateRefundRequestTest.php @@ -5,7 +5,7 @@ namespace MobilePayPhp\MobilePay\AppPayment\Refunds; use MobilePayPhp\MobilePay\AppPayment\Amount; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; use PHPUnit\Framework\TestCase; /** @@ -13,7 +13,7 @@ * @covers \MobilePayPhp\Api\IsPostTrait * * @uses \MobilePayPhp\MobilePay\AppPayment\Amount - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * * @group unit */ diff --git a/src/MobilePay/AppPayment/Refunds/CreateRefundResponse.php b/src/MobilePay/AppPayment/Refunds/CreateRefundResponse.php index db485d6..567c5d6 100644 --- a/src/MobilePay/AppPayment/Refunds/CreateRefundResponse.php +++ b/src/MobilePay/AppPayment/Refunds/CreateRefundResponse.php @@ -8,7 +8,7 @@ use MobilePayPhp\Api\Validation\ValidationRule; use MobilePayPhp\Api\Validation\ValidationTrait; use MobilePayPhp\MobilePay\AppPayment\DateTimeFactory; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; /** * @see \MobilePayPhp\MobilePay\AppPayment\Refunds\CreateRefundResponseTest diff --git a/src/MobilePay/AppPayment/Refunds/CreateRefundResponseTest.php b/src/MobilePay/AppPayment/Refunds/CreateRefundResponseTest.php index b9ee3dc..6a2fafe 100644 --- a/src/MobilePay/AppPayment/Refunds/CreateRefundResponseTest.php +++ b/src/MobilePay/AppPayment/Refunds/CreateRefundResponseTest.php @@ -12,7 +12,7 @@ * * @uses \MobilePayPhp\Api\Validation\ValidationRule * @uses \MobilePayPhp\MobilePay\AppPayment\DateTimeFactory - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * * @group unit */ @@ -41,6 +41,8 @@ public function test_can_be_constructed(): void /** * @dataProvider invalidPayloadProvider + * + * @param mixed[]|array|array|array $payload */ public function test_it_should_throw_invalid_argument_exception_on_invalid_payload(array $payload, string $expectedExceptionMessage): void { diff --git a/src/MobilePay/AppPayment/Refunds/GetRefundRequest.php b/src/MobilePay/AppPayment/Refunds/GetRefundRequest.php index df58506..9d866d0 100644 --- a/src/MobilePay/AppPayment/Refunds/GetRefundRequest.php +++ b/src/MobilePay/AppPayment/Refunds/GetRefundRequest.php @@ -6,7 +6,7 @@ use MobilePayPhp\Api\IsGetTrait; use MobilePayPhp\Api\RequestInterface; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; /** * @see \MobilePayPhp\MobilePay\AppPayment\Refunds\GetRefundRequestTest diff --git a/src/MobilePay/AppPayment/Refunds/GetRefundRequestTest.php b/src/MobilePay/AppPayment/Refunds/GetRefundRequestTest.php index 1b77358..41a9ca0 100644 --- a/src/MobilePay/AppPayment/Refunds/GetRefundRequestTest.php +++ b/src/MobilePay/AppPayment/Refunds/GetRefundRequestTest.php @@ -4,14 +4,14 @@ namespace MobilePayPhp\MobilePay\AppPayment\Refunds; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; use PHPUnit\Framework\TestCase; /** * @covers \MobilePayPhp\MobilePay\AppPayment\Refunds\GetRefundRequest * @covers \MobilePayPhp\Api\IsGetTrait * - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * * @group unit */ diff --git a/src/MobilePay/AppPayment/Refunds/GetRefundResponse.php b/src/MobilePay/AppPayment/Refunds/GetRefundResponse.php index bf9cdcc..bca30df 100644 --- a/src/MobilePay/AppPayment/Refunds/GetRefundResponse.php +++ b/src/MobilePay/AppPayment/Refunds/GetRefundResponse.php @@ -8,7 +8,7 @@ use MobilePayPhp\Api\Validation\ValidationRule; use MobilePayPhp\Api\Validation\ValidationTrait; use MobilePayPhp\MobilePay\AppPayment\DateTimeFactory; -use MobilePayPhp\MobilePay\AppPayment\Id; +use MobilePayPhp\MobilePay\Id; /** * @see \MobilePayPhp\MobilePay\AppPayment\Refunds\GetRefundResponseTest diff --git a/src/MobilePay/AppPayment/Refunds/GetRefundResponseTest.php b/src/MobilePay/AppPayment/Refunds/GetRefundResponseTest.php index 8091f6b..bfaf642 100644 --- a/src/MobilePay/AppPayment/Refunds/GetRefundResponseTest.php +++ b/src/MobilePay/AppPayment/Refunds/GetRefundResponseTest.php @@ -12,7 +12,7 @@ * * @uses \MobilePayPhp\Api\Validation\ValidationRule * @uses \MobilePayPhp\MobilePay\AppPayment\DateTimeFactory - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * * @group unit */ @@ -45,6 +45,8 @@ public function test_can_be_constructed(): void /** * @dataProvider invalidPayloadProvider + * + * @param mixed[]|array|array|array $payload */ public function test_it_should_throw_invalid_argument_exception_on_invalid_payload(array $payload, string $expectedExceptionMessage): void { diff --git a/src/MobilePay/AppPayment/Refunds/GetRefundsResponse.php b/src/MobilePay/AppPayment/Refunds/GetRefundsResponse.php index 5049115..cd92115 100644 --- a/src/MobilePay/AppPayment/Refunds/GetRefundsResponse.php +++ b/src/MobilePay/AppPayment/Refunds/GetRefundsResponse.php @@ -38,7 +38,7 @@ public function __construct(array $payload) $this->pageSize = (int) $payload['pageSize']; $this->nextPageNumber = isset($payload['nextPageNumber']) ? (int) $payload['nextPageNumber'] : null; - $this->refunds = array_map(fn (array $refund) => new GetRefundResponse($refund), $payload['refunds'] ?? []); + $this->refunds = array_map(fn (array $refund): \MobilePayPhp\MobilePay\AppPayment\Refunds\GetRefundResponse => new GetRefundResponse($refund), $payload['refunds'] ?? []); } public static function fromResponse(ResponseInterface $response): self diff --git a/src/MobilePay/AppPayment/Refunds/GetRefundsResponseTest.php b/src/MobilePay/AppPayment/Refunds/GetRefundsResponseTest.php index 0fe45a0..6999453 100644 --- a/src/MobilePay/AppPayment/Refunds/GetRefundsResponseTest.php +++ b/src/MobilePay/AppPayment/Refunds/GetRefundsResponseTest.php @@ -10,7 +10,7 @@ * @covers \MobilePayPhp\MobilePay\AppPayment\Refunds\GetRefundsResponse * @covers \MobilePayPhp\MobilePay\AppPayment\Refunds\GetRefundResponse * - * @uses \MobilePayPhp\MobilePay\AppPayment\Id + * @uses \MobilePayPhp\MobilePay\Id * @uses \MobilePayPhp\MobilePay\AppPayment\DateTimeFactory * @uses \MobilePayPhp\Api\Validation\ValidationRule * diff --git a/src/MobilePay/AppPayment/Id.php b/src/MobilePay/Id.php similarity index 62% rename from src/MobilePay/AppPayment/Id.php rename to src/MobilePay/Id.php index 97d1dd3..728d149 100644 --- a/src/MobilePay/AppPayment/Id.php +++ b/src/MobilePay/Id.php @@ -2,18 +2,19 @@ declare(strict_types=1); -namespace MobilePayPhp\MobilePay\AppPayment; +namespace MobilePayPhp\MobilePay; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidInterface; /** - * @see \MobilePayPhp\MobilePay\AppPayment\IdTest + * @see \MobilePayPhp\MobilePay\IdTest */ final class Id { - private function __construct(private readonly UuidInterface $id) - { + private function __construct( + private readonly UuidInterface $id + ) { } public static function create(): self @@ -30,9 +31,4 @@ public function toString(): string { return $this->id->toString(); } - - public function getId(): string - { - return $this->id->toString(); - } } diff --git a/src/MobilePay/AppPayment/IdTest.php b/src/MobilePay/IdTest.php similarity index 69% rename from src/MobilePay/AppPayment/IdTest.php rename to src/MobilePay/IdTest.php index 049caa0..a85cd02 100644 --- a/src/MobilePay/AppPayment/IdTest.php +++ b/src/MobilePay/IdTest.php @@ -2,13 +2,13 @@ declare(strict_types=1); -namespace MobilePayPhp\MobilePay\AppPayment; +namespace MobilePayPhp\MobilePay; use PHPUnit\Framework\TestCase; use Ramsey\Uuid\Uuid; /** - * @covers \MobilePayPhp\MobilePay\AppPayment\Id + * @covers \MobilePayPhp\MobilePay\Id * * @group unit */ @@ -19,7 +19,6 @@ public function test_it_can_create_new__id(): void $id = Id::create(); static::assertTrue(Uuid::isValid($id->toString())); - static::assertTrue(Uuid::isValid($id->getId())); } public function test_it_can_create__id_from_string(): void @@ -27,6 +26,5 @@ public function test_it_can_create__id_from_string(): void $id = Id::fromString('8a4d5be1-cdf9-45a3-b1aa-47a1e976c5b9'); static::assertSame('8a4d5be1-cdf9-45a3-b1aa-47a1e976c5b9', $id->toString()); - static::assertSame('8a4d5be1-cdf9-45a3-b1aa-47a1e976c5b9', $id->getId()); } } diff --git a/src/MobilePay/AppPayment/ResponseHandler.php b/src/MobilePay/ResponseHandler.php similarity index 89% rename from src/MobilePay/AppPayment/ResponseHandler.php rename to src/MobilePay/ResponseHandler.php index 418e422..4ce92dc 100644 --- a/src/MobilePay/AppPayment/ResponseHandler.php +++ b/src/MobilePay/ResponseHandler.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace MobilePayPhp\MobilePay\AppPayment; +namespace MobilePayPhp\MobilePay; use MobilePayPhp\Api\Exception\ClientException; use MobilePayPhp\Api\Exception\ServerException; @@ -12,13 +12,10 @@ use MobilePayPhp\MobilePay\AppPayment\Exception\MobilePayClientException; /** - * @see \MobilePayPhp\MobilePay\AppPayment\ResponseHandlerTest + * @see \MobilePayPhp\MobilePay\ResponseHandlerTest */ final class ResponseHandler implements ResponseHandlerInterface { - /** - * {@inheritDoc} - */ public function handle(int $statusCode, array $body): Response { switch ($statusCode) { diff --git a/src/MobilePay/AppPayment/ResponseHandlerTest.php b/src/MobilePay/ResponseHandlerTest.php similarity index 96% rename from src/MobilePay/AppPayment/ResponseHandlerTest.php rename to src/MobilePay/ResponseHandlerTest.php index 2a47925..59eb6e9 100644 --- a/src/MobilePay/AppPayment/ResponseHandlerTest.php +++ b/src/MobilePay/ResponseHandlerTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace MobilePayPhp\MobilePay\AppPayment; +namespace MobilePayPhp\MobilePay; use MobilePayPhp\Api\Exception\ClientException; use MobilePayPhp\Api\Exception\ServerException; @@ -11,7 +11,7 @@ use PHPUnit\Framework\TestCase; /** - * @covers \MobilePayPhp\MobilePay\AppPayment\ResponseHandler + * @covers \MobilePayPhp\MobilePay\ResponseHandler * @covers \MobilePayPhp\Api\Response * @covers \MobilePayPhp\Api\Exception\UnexpectedResponseException * @covers \MobilePayPhp\Api\Exception\ClientException diff --git a/src/MobilePay/Webhook/.gitkeep b/src/MobilePay/Webhook/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/MobilePay/Webhook/Client.php b/src/MobilePay/Webhook/Client.php new file mode 100644 index 0000000..a69a431 --- /dev/null +++ b/src/MobilePay/Webhook/Client.php @@ -0,0 +1,88 @@ +client->request( + new GetWebhookRequest($webhookId) + ); + + $this->validator->validateSingleWebhookResponse($response); + + return SingleWebhook::fromResponse($response); + } + + public function getWebhooks(): WebhookCollection + { + $response = $this->client->request( + new GetWebhooksRequest() + ); + + $this->validator->validateWebhookCollectionResponse($response); + + return WebhookCollection::fromResponse($response); + } + + public function deleteWebhook(Id $webhookId): void + { + $this->client->request( + new DeleteWebhookRequest($webhookId) + ); + } + + public function createWebhook(array $events, string $url, Id $paymentPointId = null): SingleWebhook + { + $request = new CreateWebhookRequest($events, $url, $paymentPointId); + + $this->validator->validateCreateWebhookRequest($request); + + $response = $this->client->request($request); + + $this->validator->validateSingleWebhookResponse($response); + + return SingleWebhook::fromResponse($response); + } + + public function updateWebhook(Id $webhookId, array $events, string $url, Id $paymentPointId = null): SingleWebhook + { + $request = new UpdateWebhookRequest($webhookId, $events, $url, $paymentPointId); + + $this->validator->validateUpdateWebhookRequest($request); + + $response = $this->client->request($request); + + $this->validator->validateSingleWebhookResponse($response); + + return SingleWebhook::fromResponse($response); + } + + public function publishTestNotification(Id $webhookId): void + { + $this->client->request( + new PublishTestNotificationRequest($webhookId) + ); + } +} diff --git a/src/MobilePay/Webhook/Event.php b/src/MobilePay/Webhook/Event.php new file mode 100644 index 0000000..d272d5d --- /dev/null +++ b/src/MobilePay/Webhook/Event.php @@ -0,0 +1,90 @@ +event = $event; + } + + public static function paymentReservedEvent(): self + { + return new self(self::PAYMENT_RESERVED); + } + + public static function paymentExpiredEvent(): self + { + return new self(self::PAYMENT_EXPIRED); + } + + public static function paymentCancelledByUserEvent(): self + { + return new self(self::PAYMENT_CANCELLED_BY_USER); + } + + public static function paymentPointActivatedEvent(): self + { + return new self(self::PAYMENT_POINT_ACTIVATED); + } + + public static function transferSucceededEvent(): self + { + return new self(self::TRANSFER_SUCCEEDED); + } + + public function getEvent(): string + { + return $this->event; + } + + public function isPaymentReserved(): bool + { + return self::PAYMENT_RESERVED === $this->event; + } + + public function isPaymentExpired(): bool + { + return self::PAYMENT_EXPIRED === $this->event; + } + + public function isPaymentCancelledByUser(): bool + { + return self::PAYMENT_CANCELLED_BY_USER === $this->event; + } + + public function isPaymentPaymentPointActivated(): bool + { + return self::PAYMENT_POINT_ACTIVATED === $this->event; + } + + public function isTransferSucceeded(): bool + { + return self::TRANSFER_SUCCEEDED === $this->event; + } +} diff --git a/src/MobilePay/Webhook/EventTest.php b/src/MobilePay/Webhook/EventTest.php new file mode 100644 index 0000000..b1e0099 --- /dev/null +++ b/src/MobilePay/Webhook/EventTest.php @@ -0,0 +1,66 @@ +expectException(InvalidArgumentException::class); + + new Event('test'); + } + + public function test_event(): void + { + $paymentReservedEvent = Event::paymentReservedEvent(); + static::assertSame('payment.reserved', $paymentReservedEvent->getEvent()); + static::assertTrue($paymentReservedEvent->isPaymentReserved()); + static::assertFalse($paymentReservedEvent->isPaymentExpired()); + static::assertFalse($paymentReservedEvent->isPaymentCancelledByUser()); + static::assertFalse($paymentReservedEvent->isPaymentPaymentPointActivated()); + static::assertFalse($paymentReservedEvent->isTransferSucceeded()); + + $paymentExpiredEvent = Event::paymentExpiredEvent(); + static::assertSame('payment.expired', $paymentExpiredEvent->getEvent()); + static::assertTrue($paymentExpiredEvent->isPaymentExpired()); + static::assertFalse($paymentExpiredEvent->isPaymentReserved()); + static::assertFalse($paymentExpiredEvent->isPaymentCancelledByUser()); + static::assertFalse($paymentExpiredEvent->isPaymentPaymentPointActivated()); + static::assertFalse($paymentExpiredEvent->isTransferSucceeded()); + + $paymentCancelledByUserEvent = Event::paymentCancelledByUserEvent(); + static::assertSame('payment.cancelled_by_user', $paymentCancelledByUserEvent->getEvent()); + static::assertTrue($paymentCancelledByUserEvent->isPaymentCancelledByUser()); + static::assertFalse($paymentCancelledByUserEvent->isPaymentExpired()); + static::assertFalse($paymentCancelledByUserEvent->isPaymentReserved()); + static::assertFalse($paymentCancelledByUserEvent->isPaymentPaymentPointActivated()); + static::assertFalse($paymentCancelledByUserEvent->isTransferSucceeded()); + + $paymentPointActivatedEvent = Event::paymentPointActivatedEvent(); + static::assertSame('paymentpoint.activated', $paymentPointActivatedEvent->getEvent()); + static::assertTrue($paymentPointActivatedEvent->isPaymentPaymentPointActivated()); + static::assertFalse($paymentPointActivatedEvent->isPaymentCancelledByUser()); + static::assertFalse($paymentPointActivatedEvent->isPaymentExpired()); + static::assertFalse($paymentPointActivatedEvent->isPaymentReserved()); + static::assertFalse($paymentPointActivatedEvent->isTransferSucceeded()); + + $transferSucceededEvent = Event::transferSucceededEvent(); + static::assertSame('transfer.succeeded', $transferSucceededEvent->getEvent()); + static::assertTrue($transferSucceededEvent->isTransferSucceeded()); + static::assertFalse($transferSucceededEvent->isPaymentPaymentPointActivated()); + static::assertFalse($transferSucceededEvent->isPaymentCancelledByUser()); + static::assertFalse($transferSucceededEvent->isPaymentExpired()); + static::assertFalse($transferSucceededEvent->isPaymentReserved()); + } +} diff --git a/src/MobilePay/Webhook/Exception/InvalidArgumentException.php b/src/MobilePay/Webhook/Exception/InvalidArgumentException.php new file mode 100644 index 0000000..fb35bb1 --- /dev/null +++ b/src/MobilePay/Webhook/Exception/InvalidArgumentException.php @@ -0,0 +1,9 @@ +getMethod()); + assertSame($expectedUri, $request->getUri()); + assertSame($expectedBody, $request->getBody()); + } +} diff --git a/src/MobilePay/Webhook/Request/CreateWebhookRequest.php b/src/MobilePay/Webhook/Request/CreateWebhookRequest.php new file mode 100644 index 0000000..1e76081 --- /dev/null +++ b/src/MobilePay/Webhook/Request/CreateWebhookRequest.php @@ -0,0 +1,47 @@ + $events + */ + public function __construct( + private readonly array $events, + private readonly string $url, + private readonly ?Id $paymentPointId = null + ) { + } + + public function getUri(): string + { + return '/v1/webhooks'; + } + + public function getBody(): array + { + $body = [ + 'events' => array_map(static fn (Event $event): string => $event->getEvent(), $this->events), + 'url' => $this->url, + ]; + + if (null !== $this->paymentPointId) { + $body['paymentPointId'] = $this->paymentPointId->toString(); + } + + return $body; + } +} diff --git a/src/MobilePay/Webhook/Request/CreateWebhookRequestTest.php b/src/MobilePay/Webhook/Request/CreateWebhookRequestTest.php new file mode 100644 index 0000000..f6e887a --- /dev/null +++ b/src/MobilePay/Webhook/Request/CreateWebhookRequestTest.php @@ -0,0 +1,42 @@ + [ + 'payment.reserved', + ], + 'url' => 'webhook_uri', + ]; + yield 'construct CreateWebhookRequest without PaymentPointId' => [$expectedMethod, $expectedUri, $expectedBody, new CreateWebhookRequest([Event::paymentReservedEvent()], 'webhook_uri')]; + + $expectedBody = [ + 'events' => [ + 'payment.reserved', + ], + 'url' => 'webhook_uri', + 'paymentPointId' => '57f43518-c9bf-40ff-b196-b10ef8a5b074', + ]; + yield 'construct CreateWebhookRequest with PaymentPointId' => [$expectedMethod, $expectedUri, $expectedBody, new CreateWebhookRequest([Event::paymentReservedEvent()], 'webhook_uri', Id::fromString('57f43518-c9bf-40ff-b196-b10ef8a5b074'))]; + } +} diff --git a/src/MobilePay/Webhook/Request/DeleteWebhookRequest.php b/src/MobilePay/Webhook/Request/DeleteWebhookRequest.php new file mode 100644 index 0000000..8aa38f3 --- /dev/null +++ b/src/MobilePay/Webhook/Request/DeleteWebhookRequest.php @@ -0,0 +1,27 @@ +webhookId->toString()); + } +} diff --git a/src/MobilePay/Webhook/Request/DeleteWebhookRequestTest.php b/src/MobilePay/Webhook/Request/DeleteWebhookRequestTest.php new file mode 100644 index 0000000..2637c39 --- /dev/null +++ b/src/MobilePay/Webhook/Request/DeleteWebhookRequestTest.php @@ -0,0 +1,26 @@ + [$expectedMethod, $expectedUri, $expectedBody, new DeleteWebhookRequest(Id::fromString('c7dc46c1-8251-4579-9997-cba6725826bf'))]; + } +} diff --git a/src/MobilePay/Webhook/Request/GetWebhookRequest.php b/src/MobilePay/Webhook/Request/GetWebhookRequest.php new file mode 100644 index 0000000..b248e88 --- /dev/null +++ b/src/MobilePay/Webhook/Request/GetWebhookRequest.php @@ -0,0 +1,29 @@ +webhookId->toString()); + } +} diff --git a/src/MobilePay/Webhook/Request/GetWebhookRequestTest.php b/src/MobilePay/Webhook/Request/GetWebhookRequestTest.php new file mode 100644 index 0000000..864d2d9 --- /dev/null +++ b/src/MobilePay/Webhook/Request/GetWebhookRequestTest.php @@ -0,0 +1,26 @@ + [$expectedMethod, $expectedUri, $expectedBody, new GetWebhookRequest(Id::fromString('c7dc46c1-8251-4579-9997-cba6725826bf'))]; + } +} diff --git a/src/MobilePay/Webhook/Request/GetWebhooksRequest.php b/src/MobilePay/Webhook/Request/GetWebhooksRequest.php new file mode 100644 index 0000000..6732dbd --- /dev/null +++ b/src/MobilePay/Webhook/Request/GetWebhooksRequest.php @@ -0,0 +1,21 @@ + [$expectedMethod, $expectedUri, $expectedBody, new GetWebhooksRequest()]; + } +} diff --git a/src/MobilePay/Webhook/Request/PublishTestNotificationRequest.php b/src/MobilePay/Webhook/Request/PublishTestNotificationRequest.php new file mode 100644 index 0000000..291d39f --- /dev/null +++ b/src/MobilePay/Webhook/Request/PublishTestNotificationRequest.php @@ -0,0 +1,31 @@ +webhookId->toString()); + } + + public function getBody(): ?array + { + return null; + } +} diff --git a/src/MobilePay/Webhook/Request/UpdateWebhookRequest.php b/src/MobilePay/Webhook/Request/UpdateWebhookRequest.php new file mode 100644 index 0000000..95ca1f2 --- /dev/null +++ b/src/MobilePay/Webhook/Request/UpdateWebhookRequest.php @@ -0,0 +1,48 @@ +webhookId->toString()); + } + + public function getBody(): ?array + { + $body = [ + 'events' => array_map(static fn (Event $event): string => $event->getEvent(), $this->events), + 'url' => $this->url, + ]; + + if (null !== $this->paymentPointId) { + $body['paymentPointId'] = $this->paymentPointId->toString(); + } + + return $body; + } +} diff --git a/src/MobilePay/Webhook/Request/UpdateWebhookRequestTest.php b/src/MobilePay/Webhook/Request/UpdateWebhookRequestTest.php new file mode 100644 index 0000000..8f7fad9 --- /dev/null +++ b/src/MobilePay/Webhook/Request/UpdateWebhookRequestTest.php @@ -0,0 +1,44 @@ + [ + 'payment.reserved', + ], + 'url' => 'webhook_uri', + ]; + yield 'construct UpdateWebhookRequest without PaymentPointId' => [$expectedMethod, $expectedUri, $expectedBody, new UpdateWebhookRequest(Id::fromString('fce10004-3ef0-4b11-b192-337b70af5c72'), [Event::paymentReservedEvent()], 'webhook_uri')]; + + $expectedUri = '/v1/webhooks/0a8e5c4f-1fac-444f-9245-441082d65916'; + $expectedBody = [ + 'events' => [ + 'payment.reserved', + ], + 'url' => 'webhook_uri', + 'paymentPointId' => '57f43518-c9bf-40ff-b196-b10ef8a5b074', + ]; + yield 'construct UpdateWebhookRequest with PaymentPointId' => [$expectedMethod, $expectedUri, $expectedBody, new UpdateWebhookRequest(Id::fromString('0a8e5c4f-1fac-444f-9245-441082d65916'), [Event::paymentReservedEvent()], 'webhook_uri', Id::fromString('57f43518-c9bf-40ff-b196-b10ef8a5b074'))]; + } +} diff --git a/src/MobilePay/Webhook/Response/SingleWebhook.php b/src/MobilePay/Webhook/Response/SingleWebhook.php new file mode 100644 index 0000000..3bdbf12 --- /dev/null +++ b/src/MobilePay/Webhook/Response/SingleWebhook.php @@ -0,0 +1,69 @@ +, signatureKey: string, url: string, webhookId: string, paymentPointId?: string } $payload + */ + public function __construct(array $payload) + { + $this->events = array_map(static fn (string $event): Event => new Event($event), $payload['events']); + + $this->signatureKey = $payload['signatureKey']; + $this->url = $payload['url']; + $this->webhookId = Id::fromString($payload['webhookId']); + + $this->paymentPointId = isset($payload['paymentPointId']) ? Id::fromString($payload['paymentPointId']) : null; + } + + public static function fromResponse(ResponseInterface $response): self + { + /* @phpstan-ignore-next-line */ + return new self($response->getBody()); + } + + /** + * @return Event[] + */ + public function getEvents(): array + { + return $this->events; + } + + public function getSignatureKey(): string + { + return $this->signatureKey; + } + + public function getUrl(): string + { + return $this->url; + } + + public function getWebhookId(): Id + { + return $this->webhookId; + } + + public function getPaymentPointId(): ?Id + { + return $this->paymentPointId; + } +} diff --git a/src/MobilePay/Webhook/Response/WebhookCollection.php b/src/MobilePay/Webhook/Response/WebhookCollection.php new file mode 100644 index 0000000..adcc9ee --- /dev/null +++ b/src/MobilePay/Webhook/Response/WebhookCollection.php @@ -0,0 +1,36 @@ +webhooks = array_map(fn (array $singleWebhook): SingleWebhook => new SingleWebhook($singleWebhook), $payload['webhooks']); + } + + public static function fromResponse(ResponseInterface $response): self + { + return new self($response->getBody()); + } + + /** + * @return SingleWebhook[] + */ + public function getWebhooks(): array + { + return $this->webhooks; + } +} diff --git a/src/MobilePay/Webhook/Schema/CreateWebhookRequest.schema.json b/src/MobilePay/Webhook/Schema/CreateWebhookRequest.schema.json new file mode 100644 index 0000000..84c5b63 --- /dev/null +++ b/src/MobilePay/Webhook/Schema/CreateWebhookRequest.schema.json @@ -0,0 +1,25 @@ +{ + "required": [ + "events", + "url" + ], + "type": "object", + "properties": { + "url": { + "description": "URL to where webhook requests will be sent. Must be HTTPS. Scheme and host will be converted to lower case. Result can be seen in the response.", + "type": "string" + }, + "events": { + "description": "List of subscribed events.\n\n Allowed event types:\n* `payment.reserved`\n* `payment.expired`\n* `paymentpoint.activated`\n* `payment.cancelled_by_user`\n", + "type": "array", + "items": { + "description": "", + "type": "string" + } + }, + "paymentPointId": { + "format": "uuid", + "description": "Optional. Webhook will receive notifications only coming from specified payment point.\ntype: string\n" + } + } +} \ No newline at end of file diff --git a/src/MobilePay/Webhook/Schema/MultipleWebhookResponse.schema.json b/src/MobilePay/Webhook/Schema/MultipleWebhookResponse.schema.json new file mode 100644 index 0000000..b6398fc --- /dev/null +++ b/src/MobilePay/Webhook/Schema/MultipleWebhookResponse.schema.json @@ -0,0 +1,61 @@ +{ + "required": [ + "webhooks" + ], + "type": "object", + "properties": { + "webhooks": { + "description": "The requested list of webhooks.\n\n", + "type": "array", + "items": { + "$ref": "#/definitions/SingleWebhookResponse" + } + } + }, + "definitions": { + "SingleWebhookResponse" : { + "required": [ + "events", + "signatureKey", + "url", + "webhookId" + ], + "type": "object", + "properties": { + "webhookId": { + "format": "uuid", + "description": "Unique identifier for the object.\n\n", + "type": "string", + "example": "e4a2e195-74f6-42e1-a172-83291c9d2a41" + }, + "url": { + "description": "The URL of the webhook endpoint.\n\n", + "type": "string", + "example": "https://www.my-site.com/webhooks" + }, + "events": { + "description": "List of subscribed events.\n\n", + "type": "array", + "items": { + "description": "", + "type": "string" + }, + "example": [ + "payment.reserved" + ] + }, + "signatureKey": { + "description": "The webhook's secret is used to generate webhook signatures.\n\n", + "type": "string", + "example": "my-secure-signature-key" + }, + "paymentPointId": { + "format": "uuid", + "description": "Payment point for which the webhook received notifications.\n\n", + "type": "string", + "example": null + } + } + } + } +} \ No newline at end of file diff --git a/src/MobilePay/Webhook/Schema/SingleWebhookResponse.schema.json b/src/MobilePay/Webhook/Schema/SingleWebhookResponse.schema.json new file mode 100644 index 0000000..bbb370a --- /dev/null +++ b/src/MobilePay/Webhook/Schema/SingleWebhookResponse.schema.json @@ -0,0 +1,44 @@ +{ + "required": [ + "events", + "signatureKey", + "url", + "webhookId" + ], + "type": "object", + "properties": { + "webhookId": { + "format": "uuid", + "description": "Unique identifier for the object.\n\n", + "type": "string", + "example": "e4a2e195-74f6-42e1-a172-83291c9d2a41" + }, + "url": { + "description": "The URL of the webhook endpoint.\n\n", + "type": "string", + "example": "https://www.my-site.com/webhooks" + }, + "events": { + "description": "List of subscribed events.\n\n", + "type": "array", + "items": { + "description": "", + "type": "string" + }, + "example": [ + "payment.reserved" + ] + }, + "signatureKey": { + "description": "The webhook's secret is used to generate webhook signatures.\n\n", + "type": "string", + "example": "my-secure-signature-key" + }, + "paymentPointId": { + "format": "uuid", + "description": "Payment point for which the webhook received notifications.\n\n", + "type": "string", + "example": null + } + } +} diff --git a/src/MobilePay/Webhook/Schema/UpdateWebhookRequest.schema.json b/src/MobilePay/Webhook/Schema/UpdateWebhookRequest.schema.json new file mode 100644 index 0000000..9480bfb --- /dev/null +++ b/src/MobilePay/Webhook/Schema/UpdateWebhookRequest.schema.json @@ -0,0 +1,26 @@ +{ + "required": [ + "events", + "url" + ], + "type": "object", + "properties": { + "url": { + "description": "URL to where webhook requests will be sent. Must be HTTPS. Scheme and host will be converted to lower case. Result can be seen in the response.", + "type": "string" + }, + "events": { + "description": "List of subscribed events.\n\n Allowed event types:\n* `payment.reserved`\n* `payment.expired`\n* `paymentpoint.activated`\n* `payment.cancelled_by_user`\n", + "type": "array", + "items": { + "description": "", + "type": "string" + } + }, + "paymentPointId": { + "format": "uuid", + "description": "Optional. Webhook will receive notifications only coming from specified payment point.\n", + "type": "string" + } + } +} \ No newline at end of file diff --git a/src/MobilePay/Webhook/Schema/Validator.php b/src/MobilePay/Webhook/Schema/Validator.php new file mode 100644 index 0000000..168dea1 --- /dev/null +++ b/src/MobilePay/Webhook/Schema/Validator.php @@ -0,0 +1,40 @@ +validate($response->getBody(), __DIR__.'/SingleWebhookResponse.schema.json'); + } + + public function validateWebhookCollectionResponse(ResponseInterface $response): void + { + $this->validate($response->getBody(), __DIR__.'/MultipleWebhookResponse.schema.json'); + } + + public function validateCreateWebhookRequest(CreateWebhookRequest $request): void + { + $this->validate($request->getBody() ?? [], __DIR__.'/CreateWebhookRequest.schema.json'); + } + + public function validateUpdateWebhookRequest(UpdateWebhookRequest $request): void + { + $this->validate($request->getBody() ?? [], __DIR__.'/UpdateWebhookRequest.schema.json'); + } +} diff --git a/src/MobilePay/Webhook/Schema/ValidatorTest.php b/src/MobilePay/Webhook/Schema/ValidatorTest.php new file mode 100644 index 0000000..93b0831 --- /dev/null +++ b/src/MobilePay/Webhook/Schema/ValidatorTest.php @@ -0,0 +1,31 @@ +validateSingleWebhookResponse($response); + } +}