Skip to content

Commit

Permalink
Merge pull request #15 from skoro/14-bug-error-on-payment-status
Browse files Browse the repository at this point in the history
Fix LiqPay payment status error
  • Loading branch information
skoro authored Sep 10, 2024
2 parents 7c1b0a0 + ddd95f9 commit 8470e73
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/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/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/Payment/LiqPay/Response/AbstractResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Payment\Common\Message\MessageInterface;
use App\Payment\Common\Message\NullRequest;
use App\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
2 changes: 1 addition & 1 deletion src/Payment/LiqPay/Response/PaymentStatusResponse.php
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 8470e73

Please sign in to comment.