Skip to content

Commit

Permalink
Merge branch 'master' into 5-reorganize-the-app-namespace-and-folders
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Mirspay/Payment/LiqPay/Response/AbstractResponse.php
  • Loading branch information
skoro committed Sep 10, 2024
2 parents bed6f39 + 8470e73 commit 6bbb7d5
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Mirspay/Controller/Api/V1/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ public function getPaymentStatus(
$event = $this->eventDispatcher->dispatch(new PaymentStatusEvent($order, $statusResponse));
$statusResponse = $event->paymentStatusResponse;

return $this->json(PaymentStatusDto::makeFromResponse($paymentGateway->getId(), $statusResponse));
return $this->json(PaymentStatusDto::makeFromPaymentGatewayResponse($paymentGateway->getId(), $statusResponse));
}
}
8 changes: 4 additions & 4 deletions src/Mirspay/Dto/PaymentStatusDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ public function __construct(
) {
}

public static function makeFromResponse(
public static function makeFromPaymentGatewayResponse(
string $paymentGatewayId,
ResponseInterface $response,
): self {
return new self(
paymentGatewayId: $paymentGatewayId,
isSuccess: $response->isSuccessful(),
transactionId: $response->getTransactionId(),
message: $response->getMessage(),
code: $response->getCode(),
data: $response->getRawData(),
message: (string) $response->getMessage(),
code: (string) $response->getCode(),
data: (array) $response->getRawData(),
);
}
}
12 changes: 12 additions & 0 deletions src/Mirspay/Payment/LiqPay/Response/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Mirspay\Payment\Common\Message\MessageInterface;
use Mirspay\Payment\Common\Message\NullRequest;
use Mirspay\Payment\Common\Message\RequestInterface;
use Override;

abstract class AbstractResponse extends CommonAbstractResponse
{
Expand All @@ -18,6 +19,17 @@ public function __construct(
parent::__construct($request);
}

#[Override]
public function getCode(): string
{
return (string) ($this->data['code'] ?? '');
}

public function getStatus(): string
{
return (string) ($this->data['status'] ?? '');
}

public static function makeOfMessage(MessageInterface $message, RequestInterface | null $request = null): static
{
return new static($message->getRawData(), $request ?? new NullRequest());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function isRedirect(): false

public function isSuccessful(): bool
{
$status = $this->data['status'] ?? '';
$status = $this->getStatus();

return $status === self::SUCCESS;
}
Expand Down
56 changes: 56 additions & 0 deletions tests/Unit/Dto/PaymentStatusDtoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace App\Tests\Unit\Dto;

use App\Dto\PaymentStatusDto;
use App\Payment\Common\Message\ResponseInterface;
use PHPUnit\Framework\TestCase;

final class PaymentStatusDtoTest extends TestCase
{
public function testCastToStringPaymentResponseValues(): void
{
$response = $this->createStub(ResponseInterface::class);
$response->method('isSuccessful')->willReturn(true);
$response->method('getTransactionId')->willReturn('');
$response->method('getMessage')->willReturn(null);
$response->method('getCode')->willReturn(null);
$response->method('getRawData')->willReturn(false);

$dto = PaymentStatusDto::makeFromPaymentGatewayResponse('123', $response);

$this->assertEquals('123', $dto->paymentGatewayId);
$this->assertEquals('', $dto->message);
$this->assertEquals('', $dto->code);
$this->assertTrue($dto->isSuccess);
$this->assertEquals('', $dto->transactionId);
}

/**
* @dataProvider paymentStatusRawDataProvider
*/
public function testPaymentStatusDataAlwaysCastToArray(mixed $data, $expected): void
{
$response = $this->createStub(ResponseInterface::class);
$response->method('isSuccessful')->willReturn(true);
$response->method('getTransactionId')->willReturn('');
$response->method('getMessage')->willReturn(null);
$response->method('getCode')->willReturn(null);
$response->method('getRawData')->willReturn($data);

$dto = PaymentStatusDto::makeFromPaymentGatewayResponse('123', $response);

$this->assertEquals($expected, $dto->data);
}

public function paymentStatusRawDataProvider(): array
{
return [
[false, [false]],
['', ['']],
[null, []],
];
}
}
18 changes: 18 additions & 0 deletions tests/Unit/Payment/LiqPay/Response/PaymentStatusResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,22 @@ public function testLiqPayPaymentStatusSuccessResponse(): void
$this->assertEquals('123456789', $response->getTransactionId(), 'Payment transaction is not matched');
$this->assertEquals(123456789, $response->getPaymentId(), 'Payment id is not matched');
}

public function testLiqPayPaymentNotFoundResponse(): void
{
$data = $this->loadJsonFixture('liqpay/payment-not-found-status.json');

$message = $this->createStub(MessageInterface::class);
$message
->method('getRawData')
->willReturn($data);

$response = PaymentStatusResponse::makeOfMessage($message);

$this->assertFalse($response->isSuccessful());
$this->assertEquals('Платіж не знайдено', $response->getMessage());
$this->assertEquals('', $response->getTransactionId(), 'Transaction id must be empty');
$this->assertEquals('', $response->getPaymentId());
$this->assertEquals('payment_not_found', $response->getCode(), 'Payment code is not matched');
}
}
7 changes: 7 additions & 0 deletions tests/fixtures/liqpay/payment-not-found-status.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"code": "payment_not_found",
"err_code": "payment_not_found",
"err_description": "Платіж не знайдено",
"result": "error",
"status": "error"
}

0 comments on commit 6bbb7d5

Please sign in to comment.