Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #10 from mateusjunges/feature/make-endpoints-confi…
Browse files Browse the repository at this point in the history
…gurable

Allow to change endpoitns resolver for each configured psp
  • Loading branch information
mateusjunges authored Sep 25, 2021
2 parents ddfae63 + 1f86515 commit e393a9c
Show file tree
Hide file tree
Showing 17 changed files with 282 additions and 63 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

All relevant changes to `mateusjunges/laravel-pix` will be documented here.

## 2021-06-02
## v1.1.0 2021-09-25
- Allow to change endpoints for each configured PSP (#10)

## v1.0.0 2021-06-02
- Initial Release
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- [Exibir informações sobre o webhook pix](#exibir-informaes-sobre-o-webhook-pix)
- [Cancelar o webhook pix](#cancelar-o-webhook-pix)
- [Consutlar webhooks cadastrados](#consultar-webhooks-cadastrados)
- [Configurando endpoints](#configurando-endpoints)


Este pacote oferece integração completa com a API PIX do banco central do Brasil.
Expand Down Expand Up @@ -691,3 +692,57 @@ fim | `endingAt()`
paginacao.paginaAtual | `currentPage()`
paginacao.itensPorPagina | `itemsPerPage()`
---

# Configurando Endpoints
Para configurar endpoints específicos para cada PSP, você precisa criar um `EndpointResolver` para cada PSP que não segue a convenção do BACEN.
Para criar um endpoint Resolver, crie um classe com o nome desejado e implemente a interface `CanResolveEndpoints`, ou, simplesmente extenda a class `Endpoints`,
disponível neste pacote. Ela fornece os endpoints necessários, e você pode copiar o array de endpoints e alterar o necessário para o seu PSP.

```php
public array $endpoints = [
self::OAUTH_TOKEN => '/oauth/token',
self::CREATE_COB => '/cob/',
self::GET_COB => '/cob/',
self::UPDATE_COB => '/cob/',
self::GET_ALL_COBS => '/cob/',

self::CREATE_COBV => '/cobv/',
self::GET_COBV => '/cobv/',
self::GET_ALL_COBV => '/cobv/',

self::CREATE_LOTE_COBV => '/lotecobv/',
self::UPDATE_LOTE_COBV => '/lotecobv/',
self::GET_LOTE_COBV => '/lotecobv/',
self::GET_ALL_LOTE_COBV => '/lotecobv/',

self::CREATE_WEBHOOK => '/webhook/',
self::GET_WEBHOOK => '/webhook/',
self::DELETE_WEBHOOK => '/webhook/',
self::GET_WEBHOOKS => '/webhooks/',

self::RECEIVED_PIX => '/pix/',
self::RECEIVED_PIX_REFUND => '/devolucao/',

self::CREATE_PAYLOAD_LOCATION => '/loc/',
self::GET_PAYLOAD_LOCATION => '/loc/',
self::DETACH_CHARGE_FROM_LOCATION => '/loc/',
self::PAYLOAD_LOCATION_TXID => '/loc/',
];
```

Após isso, registre o endpoint resolver do seu PSP na chave `resolve_endpoints_using`, do arquivo de configurações deste pacote, dentro das configurações do PSP desejado.

```php
'psp' => [
'default' => [
'base_url' => env('LARAVEL_PIX_PSP_BASE_URL'),
'oauth_token_url' => env('LARAVEL_PIX_PSP_OAUTH_URL', false),
'oauth_bearer_token' => env('LARAVEL_PIX_OAUTH2_BEARER_TOKEN'),
'ssl_certificate' => env('LARAVEL_PIX_PSP_SSL_CERTIFICATE'),
'client_secret' => env('LARAVEL_PIX_PSP_CLIENT_SECRET'),
'client_id' => env('LARAVEL_PIX_PSP_CLIENT_ID'),
'authentication_class' => \Junges\Pix\Api\Contracts\AuthenticatesPSPs::class,
'resolve_endpoints_using' => YourCustomEndpointResolver::class,
],
],
```
15 changes: 8 additions & 7 deletions config/laravel-pix.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
*/
'psp' => [
'default' => [
'base_url' => env('LARAVEL_PIX_PSP_BASE_URL'),
'oauth_token_url' => env('LARAVEL_PIX_PSP_OAUTH_URL', false),
'oauth_bearer_token' => env('LARAVEL_PIX_OAUTH2_BEARER_TOKEN'),
'ssl_certificate' => env('LARAVEL_PIX_PSP_SSL_CERTIFICATE'),
'client_secret' => env('LARAVEL_PIX_PSP_CLIENT_SECRET'),
'client_id' => env('LARAVEL_PIX_PSP_CLIENT_ID'),
'authentication_class' => \Junges\Pix\Api\Contracts\AuthenticatesPSPs::class,
'base_url' => env('LARAVEL_PIX_PSP_BASE_URL'),
'oauth_token_url' => env('LARAVEL_PIX_PSP_OAUTH_URL', false),
'oauth_bearer_token' => env('LARAVEL_PIX_OAUTH2_BEARER_TOKEN'),
'ssl_certificate' => env('LARAVEL_PIX_PSP_SSL_CERTIFICATE'),
'client_secret' => env('LARAVEL_PIX_PSP_CLIENT_SECRET'),
'client_id' => env('LARAVEL_PIX_PSP_CLIENT_ID'),
'authentication_class' => \Junges\Pix\Api\Contracts\AuthenticatesPSPs::class,
'resolve_endpoints_using' => \Junges\Pix\Support\Endpoints::class,
],
],
];
7 changes: 7 additions & 0 deletions src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
use Junges\Pix\Api\Contracts\ConsumesPixApi;
use Junges\Pix\Contracts\CanResolveEndpoints;
use Junges\Pix\Providers\PixServiceProvider;
use Junges\Pix\Psp;

Expand All @@ -19,6 +20,7 @@ class Api implements ConsumesPixApi
protected array $additionalParams = [];
protected array $additionalOptions = [];
protected Psp $psp;
protected CanResolveEndpoints $endpointsResolver;

public function __construct()
{
Expand Down Expand Up @@ -151,6 +153,11 @@ public function withOptions(array $options): Api
return $this;
}

protected function resolveEndpoint(string $endpoint): string
{
return $this->getPsp()->getEndpointsResolver()->getEndpoint($endpoint);
}

protected function getEndpoint(string $endpoint): string
{
return $endpoint.'?'.http_build_query($this->additionalParams);
Expand Down
10 changes: 5 additions & 5 deletions src/Api/Resources/Cob/Cob.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ public function withFilters($filters): Cob

public function create(string $transactionId, array $request): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::CREATE_COB.$transactionId);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::CREATE_COB).$transactionId);

return $this->request()->put($endpoint, $request);
}

public function createWithoutTransactionId(array $request): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::CREATE_COB);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::CREATE_COB));

return $this->request()->post($endpoint, $request);
}

public function getByTransactionId(string $transactionId): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::GET_COB.$transactionId);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::GET_COB).$transactionId);

return $this->request()->get($endpoint);
}

public function updateByTransactionId(string $transactionId, array $request): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::UPDATE_COB.$transactionId);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::UPDATE_COB).$transactionId);

return $this->request()->patch($endpoint, $request);
}
Expand All @@ -66,7 +66,7 @@ public function all(): Response
ValidationException::filtersAreRequired()
);

$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::GET_ALL_COBS);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::GET_ALL_COBS));

return $this->request()->get($endpoint, $this->filters);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Api/Resources/Cobv/Cobv.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ public function withFilters($filters): Cobv

public function createWithTransactionId(string $transactionId, array $request): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::CREATE_COBV.$transactionId);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::CREATE_COBV).$transactionId);

return $this->request()->put($endpoint, $request);
}

public function updateWithTransactionId(string $transactionId, array $request): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::CREATE_COBV.$transactionId);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::CREATE_COBV).$transactionId);

return $this->request()->patch($endpoint, $request);
}

public function getByTransactionId(string $transactionId): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::GET_COBV.$transactionId);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::GET_COBV).$transactionId);

return $this->request()->get($endpoint, $this->filters);
}
Expand All @@ -59,7 +59,7 @@ public function all(): Response
ValidationException::filtersAreRequired()
);

$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::GET_ALL_COBV);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::GET_ALL_COBV));

return $this->request()->get($endpoint, $this->filters);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Api/Resources/LoteCobv/LoteCobv.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ public function withFilters($filters): LoteCobv

public function createBatch(string $batchId, array $request): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::CREATE_LOTE_COBV.$batchId);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::CREATE_LOTE_COBV).$batchId);

return $this->request()->put($endpoint, $request);
}

public function updateBatch(string $batchId, array $request): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::UPDATE_LOTE_COBV.$batchId);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::UPDATE_LOTE_COBV).$batchId);

return $this->request()->patch($endpoint, $request);
}

public function getByBatchId(string $batchId): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::GET_LOTE_COBV.$batchId);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::GET_LOTE_COBV).$batchId);

return $this->request()->get($endpoint);
}
Expand All @@ -59,7 +59,7 @@ public function all(): Response
ValidationException::filtersAreRequired()
);

$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::GET_ALL_LOTE_COBV);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::GET_ALL_LOTE_COBV));

return $this->request()
->get($endpoint, $this->filters);
Expand Down
20 changes: 16 additions & 4 deletions src/Api/Resources/PayloadLocation/PayloadLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,33 @@ public function withFilters($filters): PayloadLocation

public function create(string $loc): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::CREATE_PAYLOAD_LOCATION);
$endpoint = $this->getEndpoint(
$this->baseUrl
.$this->resolveEndpoint(Endpoints::CREATE_PAYLOAD_LOCATION)
);

return $this->request()->post($endpoint, ['tipoCob' => $loc]);
}

public function getById(string $id): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::GET_PAYLOAD_LOCATION.$id);
$endpoint = $this->getEndpoint(
$this->baseUrl
.$this->resolveEndpoint(Endpoints::GET_PAYLOAD_LOCATION)
.$id
);

return $this->request()->get($endpoint, $this->filters);
}

public function detachChargeFromLocation(string $id): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::DETACH_CHARGE_FROM_LOCATION.$id.Endpoints::PAYLOAD_LOCATION_TXID);
$endpoint = $this->getEndpoint(
$this->baseUrl.
$this->resolveEndpoint(Endpoints::DETACH_CHARGE_FROM_LOCATION)
.$id
.$this->resolveEndpoint(Endpoints::PAYLOAD_LOCATION_TXID)
);

return $this->request()->delete($endpoint);
}
Expand All @@ -59,7 +71,7 @@ public function all(): Response
ValidationException::filtersAreRequired()
);

$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::GET_PAYLOAD_LOCATION);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::GET_PAYLOAD_LOCATION));

return $this->request()->get($endpoint, $this->filters);
}
Expand Down
20 changes: 16 additions & 4 deletions src/Api/Resources/ReceivedPix/ReceivedPix.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@ public function withFilters($filters): ReceivedPix

public function getBye2eid(string $e2eid): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::RECEIVED_PIX.$e2eid);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::RECEIVED_PIX).$e2eid);

return $this->request()->get($endpoint);
}

public function refund(string $e2eid, string $refundId): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::RECEIVED_PIX.$e2eid.Endpoints::RECEIVED_PIX_REFUND.$refundId);
$endpoint = $this->getEndpoint(
$this->baseUrl
.$this->resolveEndpoint(Endpoints::RECEIVED_PIX)
.$e2eid
.$this->resolveEndpoint(Endpoints::RECEIVED_PIX_REFUND)
.$refundId
);

$refund = $this->request()->put($endpoint);

Expand All @@ -49,7 +55,13 @@ public function refund(string $e2eid, string $refundId): Response

public function consultRefund(string $e2eid, string $refundId): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::RECEIVED_PIX.$e2eid.Endpoints::RECEIVED_PIX_REFUND.$refundId);
$endpoint = $this->getEndpoint(
$this->baseUrl
.$this->resolveEndpoint(Endpoints::RECEIVED_PIX)
.$e2eid
.$this->resolveEndpoint(Endpoints::RECEIVED_PIX_REFUND)
.$refundId
);

return $this->request()->get($endpoint);
}
Expand All @@ -64,7 +76,7 @@ public function all(): Response
ValidationException::filtersAreRequired()
);

$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::RECEIVED_PIX);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::RECEIVED_PIX));

return $this->request()->get($endpoint, $this->filters);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Api/Resources/Webhook/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function withFilters($filters): Webhook

public function create(string $pixKey, string $callbackUrl): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::CREATE_WEBHOOK.$pixKey);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::CREATE_WEBHOOK).$pixKey);

$webhook = $this->request()->put($endpoint, ['webhookUrl' => $callbackUrl]);

Expand All @@ -42,14 +42,14 @@ public function create(string $pixKey, string $callbackUrl): Response

public function getByPixKey(string $pixKey): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::GET_WEBHOOK.$pixKey);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::GET_WEBHOOK).$pixKey);

return $this->request()->get($endpoint);
}

public function delete(string $pixKey): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::DELETE_WEBHOOK.$pixKey);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::DELETE_WEBHOOK).$pixKey);

$webhook = $this->request()->delete($endpoint);

Expand All @@ -60,7 +60,7 @@ public function delete(string $pixKey): Response

public function all(): Response
{
$endpoint = $this->getEndpoint($this->baseUrl.Endpoints::GET_WEBHOOKS);
$endpoint = $this->getEndpoint($this->baseUrl.$this->resolveEndpoint(Endpoints::GET_WEBHOOKS));

return $this->request()->get($endpoint, $this->filters);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Contracts/CanResolveEndpoints.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Junges\Pix\Contracts;

interface CanResolveEndpoints
{
public function setEndpoint(string $key, string $value): void;

public function getEndpoint(string $key): string;
}
Loading

0 comments on commit e393a9c

Please sign in to comment.