Skip to content

Commit

Permalink
Merge pull request #1 from pedrobruning/feat/add-refund
Browse files Browse the repository at this point in the history
Feat/add refund
  • Loading branch information
pedrobruning authored Mar 18, 2022
2 parents 7d63859 + 0a52019 commit b04999f
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 3 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ $client = $client->withOptions([
'headers' => ['Authorization' => 'YOUR_TOKEN']
]);

$provider = Providers::OpenPix;
$provider = Providers::YourProvider;
$phPixService = PhPixServiceFactory::make($provider, $client);
```

## Cobranças
### Obter Cobrança por Id
```php
$chargeId = 'chargeId';
Expand Down Expand Up @@ -76,3 +77,25 @@ $chargeRequest = new ChargeRequest(
);
$charge = $phPixService->charges()->create($chargeRequest);
```
## Estornos
### Obter Estorno por Id
```php
$refundId = 'refundId';
$refund = $phPixService->refund()->getById($refundId);
```

### Obter todos os Estornos
```php
$refund = $phPixService->refund()->getAll();
```
### Criar Estorno
Todos os campos que podem ser utilizados para criação do estorno podem ser consultados na própria classe de Request chamada.
```php
use PedroBruning\PhPix\Models\OpenPix\RefundRequest;
$refundRequest = new RefundRequest(
value: 100,
transactionEndToEndId: 'validTransactionEndToEndId',
correlationId: 'validCorrelation'
);
$refund = $phPixService->refunds()->create($refundRequest);
```
26 changes: 26 additions & 0 deletions src/Models/OpenPix/RefundRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace PedroBruning\PhPix\Models\OpenPix;

use PedroBruning\PhPix\Models\Contracts\Request;

class RefundRequest implements Request
{

public function __construct(
private int $value,
private string $transactionEndToEndId,
private string $correlationId
)
{
}

public function getPayload(): array
{
return [
'value' => $this->value,
'transactionEndToEndId' => $this->transactionEndToEndId,
'correlationID' => $this->correlationId
];
}
}
1 change: 0 additions & 1 deletion src/Services/Contracts/ChargeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace PedroBruning\PhPix\Services\Contracts;

use PedroBruning\PhPix\Models\Contracts\Request;
use PedroBruning\PhPix\Models\Charge;

interface ChargeService
{
Expand Down
2 changes: 2 additions & 0 deletions src/Services/Contracts/PhPixService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
interface PhPixService
{
public function charges(): ChargeService;

public function refunds(): RefundService;
}
14 changes: 14 additions & 0 deletions src/Services/Contracts/RefundService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace PedroBruning\PhPix\Services\Contracts;

use PedroBruning\PhPix\Models\Contracts\Request;

interface RefundService
{
public function getById(string $id): array;

public function getAll(): array;

public function create(Request $refundRequest): array;
}
52 changes: 52 additions & 0 deletions src/Services/OpenPix/OpenPixRefundService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace PedroBruning\PhPix\Services\OpenPix;

use PedroBruning\PhPix\Models\Contracts\Request;
use PedroBruning\PhPix\Services\Contracts\RefundService;
use PedroBruning\PhPix\Services\Traits\ReturnsError;
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class OpenPixRefundService implements RefundService
{
use ReturnsError;

public function __construct(private HttpClientInterface $client)
{
}

public function getById(string $id): array
{
try {
$response = $this->client->request('GET', "refund/$id");
return $response->toArray();
} catch (ClientException $exception) {
return $this->returnError($exception);
}
}

public function getAll(): array
{
try {
$response = $this->client->request('GET', "refund");
return $response->toArray();
} catch (ClientException $exception) {
return $this->returnError($exception);
}
}

public function create(Request $refundRequest): array
{
try {
$payload = [
'json' => $refundRequest->getPayload()
];
$response = $this->client
->request('POST', 'refund', $payload);
return $response->toArray();
} catch (ClientException $exception) {
return $this->returnError($exception);
}
}
}
8 changes: 7 additions & 1 deletion src/Services/OpenPix/OpenPixService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@

use PedroBruning\PhPix\Services\Contracts\ChargeService;
use PedroBruning\PhPix\Services\Contracts\PhPixService;
use PedroBruning\PhPix\Services\Contracts\RefundService;

class OpenPixService implements PhPixService
{

public function __construct(private ChargeService $chargeService)
public function __construct(private ChargeService $chargeService, private RefundService $refundService)
{}

public function charges(): ChargeService
{
return $this->chargeService;
}

public function refunds(): RefundService
{
return $this->refundService;
}
}
164 changes: 164 additions & 0 deletions tests/Unit/Services/OpenPix/OpenPixRefundServiceTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

namespace Tests\Unit\Services\OpenPix;

use PedroBruning\PhPix\Models\OpenPix\RefundRequest;
use PedroBruning\PhPix\Services\OpenPix\OpenPixRefundService;
use PedroBruning\PhPix\Services\Contracts\RefundService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

class OpenPixRefundServiceTests extends TestCase
{
private function makeSut(array $return): RefundService
{
$responseInterfaceStub = $this->createStub(ResponseInterface::class);
$responseInterfaceStub->method('toArray')->willReturn($return);
$clientStub = $this->createStub(HttpClientInterface::class);
$clientStub->method('request')->willReturn($responseInterfaceStub);
return new OpenPixRefundService($clientStub);
}

private function makeSutWithClientThrowing(): RefundService
{
$clientStub = $this->createStub(HttpClientInterface::class);
$responseInterfaceStub = $this->createMock(ResponseInterface::class);
$responseInterfaceStub
->method('getInfo')
->withConsecutive(['http_code'], ['url'], ['response_headers'])
->willReturnOnConsecutiveCalls('400', 'some_url', []);
$clientStub->method('request')
->willThrowException(new ClientException($responseInterfaceStub));
return new OpenPixRefundService($clientStub);
}

public function test_getById_returns_valid_refund()
{
$return = $this->validGetByIdResponse();
$sut = $this->makeSut($return);
$id = 'validId';

$result = $sut->getById($id);

$this->assertEquals($return, $result);
}

public function test_getById_returns_error_body_if_client_exception_is_thrown()
{
$return = $this->validExceptionResponse();
$sut = $this->makeSutWithClientThrowing();
$id = 'validId';

$result = $sut->getById($id);

$this->assertEquals($return, $result);
}

public function test_getAll_returns_valid_refunds()
{
$return = $this->validGetAllResponse();
$sut = $this->makeSut($return);

$response = $sut->getAll();

$this->assertEquals($return, $response);
}

public function test_getByAll_returns_error_body_if_client_exception_is_thrown()
{
$return = $this->validExceptionResponse();
$sut = $this->makeSutWithClientThrowing();

$result = $sut->getAll();

$this->assertEquals($return, $result);
}

public function test_create_returns_valid_charge()
{
//Arrange
$return = $this->validCreateResponse();
$sut = $this->makeSut($return);
$charge = $this->fakeRefund();
//Act
$response = $sut->create($charge);
$this->assertEquals($return, $response);
}

public function test_create_returns_error_body_if_client_exception_is_thrown()
{
//Arrange
$sut = $this->makeSutWithClientThrowing();
$charge = $this->fakeRefund();
//Act
$result = $sut->create($charge);
//Assert
$this->assertEquals($result, $this->validExceptionResponse());
}


private function validGetByIdResponse(): array
{
return [
'refund' => [
'value' => 100,
'correlationID' => '7777-6f71-427a-bf00-241681624586',
'refundId' => '11bf5b37e0b842e08dcfdc8c4aefc000',
'returnIdentification' => 'D09089356202108032000a543e325902'
]
];
}

private function validExceptionResponse(): array
{
return [
'error' => 'HTTP 400 returned for "some_url".'
];
}

private function validGetAllResponse(): array
{
return [
"pageInfo" => [
"skip" => 0,
"limit" => 10,
"totalCount" => 20,
"hasPreviousPage" => false,
"hasNextPage" => true
],
"refunds" => [
[
"status" => "IN_PROCESSING",
"value" => 100,
"correlationID" => "9134e286-6f71-427a-bf00-241681624586",
"refundId" => "9134e2866f71427abf00241681624586",
"time" => "2021-03-02T17:28:51.882Z"
]
]
];
}

private function validCreateResponse(): array
{
return [
"refund" => [
"status" => "IN_PROCESSING",
"value" => 100,
"correlationID" => "9134e286-6f71-427a-bf00-241681624586",
"refundId" => "9134e2866f71427abf00241681624586",
"time" => "2021-03-02T17:28:51.882Z"
]
];
}

private function fakeRefund(): RefundRequest
{
return new RefundRequest(
value: 100,
correlationId: '9134e286-6f71-427a-bf00-241681624586',
transactionEndToEndId: '9134e286-6f71-427a-bf00-241681624586'
);
}
}
9 changes: 9 additions & 0 deletions tests/Unit/Services/OpenPix/OpenPixServiceTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Unit\Services\OpenPix;

use PedroBruning\PhPix\Services\Contracts\ChargeService;
use PedroBruning\PhPix\Services\Contracts\RefundService;
use PedroBruning\PhPix\Services\OpenPix\OpenPixService;
use PHPUnit\Framework\TestCase;

Expand All @@ -15,4 +16,12 @@ public function test_charges_method_returns_instance_of_charge_service()
$this->assertEquals($chargeServiceMock, $sut->charges());
}

public function test_refunds_method_returns_instance_of_charge_service()
{
$chargeServiceMock = $this->createMock(ChargeService::class);
$refundServiceMock = $this->createMock(RefundService::class);
$sut = new OpenPixService(chargeService: $chargeServiceMock, refundService: $refundServiceMock);
$this->assertEquals($refundServiceMock, $sut->refunds());
}

}

0 comments on commit b04999f

Please sign in to comment.