Skip to content

Commit

Permalink
Add order cancelation request
Browse files Browse the repository at this point in the history
  • Loading branch information
ropczan committed Mar 8, 2024
1 parent 8994f21 commit 5866200
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"require": {
"php": ">=7.4|^8.0",
"ext-json": "*",
"marc-mabe/php-enum": "^4.3",
"guzzlehttp/guzzle": "^6.0|^7.0",
"marc-mabe/php-enum": "^4.3",
"symfony/property-access": "^5.4|^6.0|^6.4",
"symfony/serializer": "^5.4|^6.0"
},
"require-dev": {
Expand Down
32 changes: 32 additions & 0 deletions src/PayPo/Request/Transaction/CancelRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Answear\PayPo\Request\Transaction;

use Answear\PayPo\Enum\OrderStatusEnum;

class CancelRequest implements RequestInterface
{
private const HTTP_METHOD = 'PATCH';
private const ENDPOINT = '/transactions';

public string $status;
private string $transactionUuid;

public function __construct(string $transactionUuid)
{
$this->status = OrderStatusEnum::CANCELED;
$this->transactionUuid = $transactionUuid;
}

public function getHttpMethod(): string
{
return self::HTTP_METHOD;
}

public function getUrl(): string
{
return self::ENDPOINT . '/' . $this->transactionUuid;
}
}
13 changes: 13 additions & 0 deletions src/PayPo/Service/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Answear\PayPo\Exception\BadResponseException;
use Answear\PayPo\Exception\PrepareRequestException;
use Answear\PayPo\Exception\ServiceUnavailable;
use Answear\PayPo\Request\Transaction\CancelRequest;
use Answear\PayPo\Request\Transaction\ConfirmRequest;
use Answear\PayPo\Request\Transaction\CreateRequest;
use Answear\PayPo\Request\Transaction\RefundRequest;
Expand Down Expand Up @@ -89,6 +90,18 @@ public function refund(string $transactionUuid, int $amount): Response
return $this->handleRequest($request, Response::class);
}

/**
* @throws ServiceUnavailable
* @throws PrepareRequestException
* @throws BadResponseException
*/
public function cancel(string $transactionUuid): ConfirmResponse
{
$request = new CancelRequest($transactionUuid);

return $this->handleRequest($request, ConfirmResponse::class);
}

/**
* @return object|mixed
*
Expand Down
54 changes: 54 additions & 0 deletions tests/Integration/Request/Transaction/CancelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Answear\PayPo\Tests\Integration\Request\Transaction;

use Answear\PayPo\Configuration\PayPoConfiguration;
use Answear\PayPo\Exception\ConfigurationException;
use Answear\PayPo\Service\PayPoClient;
use GuzzleHttp\Client;

class CancelTest extends AbstractOrderTest
{
private const TRANSACTION_UUID = 'transaction-uuid';

/**
* @test
*
* @dataProvider provideDataForRequest
*/
public function configurationNotSetException(string $transactionUuid): void
{
PayPoConfiguration::reset();

$this->expectException(ConfigurationException::class);

$client = $this->createMock(Client::class);
$this->getOrderService(new PayPoClient($client))->cancel($transactionUuid);
}

public function provideDataForRequest(): iterable
{
$this->setUpConfiguration();

yield [
self::TRANSACTION_UUID,
'{"status":"CANCELED"}',
[
'code' => '200',
'statusDescription' => 'Transaction updated successfully',
],
];
}

protected function sendAndAssert(PayPoClient $client, $request, array $apiResponse): void
{
self::assertSame(self::TRANSACTION_UUID, $request);

$response = $this->getOrderService($client)->cancel($request);

self::assertSame($apiResponse['code'], $response->code);
self::assertSame($apiResponse['statusDescription'], $response->statusDescription);
}
}

0 comments on commit 5866200

Please sign in to comment.