From 9a87bc667b341aa5947700579864828cd7dd57ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Sun, 8 Dec 2024 09:43:35 -0300 Subject: [PATCH] PHPAY-23: feat: list invoices --- .vscode/settings.json | 59 +++++++++++++- examples/asaas/credentials.example.php | 1 - examples/asaas/invoices.php | 11 ++- src/Contracts/GatewayInterface.php | 4 +- src/Gateways/Asaas/AsaasGateway.php | 65 +++++++--------- .../{BillingType.php => BillingTypeEnum.php} | 2 +- .../Asaas/Enums/InvoiceFiltersEnum.php | 30 +++++++ .../Asaas/Enums/InvoiceStatusEnum.php | 20 +++++ .../Enums/InvoiceStatusFiscalDocumentEnum.php | 13 ++++ .../Asaas/Requests/AsaasInvoiceRequest.php | 4 +- .../Resources/Interfaces/InvoiceInterface.php | 7 +- src/Gateways/Asaas/Resources/Invoice.php | 78 ++++++++++++------- src/Gateways/Gateway.php | 6 +- src/PHPay.php | 31 +++----- 14 files changed, 229 insertions(+), 102 deletions(-) rename src/Gateways/Asaas/Enums/{BillingType.php => BillingTypeEnum.php} (86%) create mode 100644 src/Gateways/Asaas/Enums/InvoiceFiltersEnum.php create mode 100644 src/Gateways/Asaas/Enums/InvoiceStatusEnum.php create mode 100644 src/Gateways/Asaas/Enums/InvoiceStatusFiscalDocumentEnum.php diff --git a/.vscode/settings.json b/.vscode/settings.json index 4ec39be..a482329 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,60 @@ { - "cSpell.enabled": false + "cSpell.enabled": false, + "editor.lineHeight": 1.9, + "editor.fontFamily": "JetBrains Mono", + // "editor.fontFamily": "Dank Mono", + // "editor.codeLensFontFamily": "Operator Mono", + "editor.fontLigatures": true, + "editor.formatOnSave": true, + "php-cs-fixer.onsave": true, + "php-cs-fixer.executablePath": "${extensionPath}/php-cs-fixer.phar", + "cSpell.userWords": [ + "Assertable", + "autoload", + "Bootstrapper", + "bootstrappers", + "Codeigniter", + "Dispatchable", + "flashdata", + "intelephense", + "junstyle", + "Laravel", + "Livewire", + "onsave", + "phar", + "phpstan", + "Queueable", + "Stancl", + "timesheet", + "userdata" + ], + "php-cs-fixer.lastDownload": 1693233566761, + "terminal.integrated.cursorStyle": "underline", + "terminal.integrated.defaultProfile.windows": "Git Bash", + "terminal.integrated.fontFamily": "Hack Nerd Font Mono", + "terminal.integrated.lineHeight": 1.2, + "editor.accessibilitySupport": "off", + "terminal.integrated.copyOnSelection": true, + "terminal.integrated.sendKeybindingsToShell": true, + "redhat.telemetry.enabled": true, + "editor.minimap.enabled": false, + "editor.tabSize": 4, + "editor.insertSpaces": true, + "editor.detectIndentation": false, + "github.copilot.enable": { + "*": true, + "plaintext": true, + "markdown": false, + "scminput": false + }, + "editor.inlineSuggest.enabled": true, + "explorer.confirmDragAndDrop": false, + "[javascript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "files.associations": { + "*.js": "javascriptreact" + }, + "window.zoomLevel": 1, + "editor.fontVariations": false, } \ No newline at end of file diff --git a/examples/asaas/credentials.example.php b/examples/asaas/credentials.example.php index db07ccd..84c4c8f 100644 --- a/examples/asaas/credentials.example.php +++ b/examples/asaas/credentials.example.php @@ -2,6 +2,5 @@ const TOKEN_ASAAS_SANDBOX = ''; -/* client to testing */ const NAME = ''; const CPF_CNPJ = ''; diff --git a/examples/asaas/invoices.php b/examples/asaas/invoices.php index 7d04f5e..4e180e6 100644 --- a/examples/asaas/invoices.php +++ b/examples/asaas/invoices.php @@ -18,7 +18,16 @@ 'dueDate' => date('Y-m-d', strtotime('+1 day')), ]; -$qrcode = PHPay::asaas(TOKEN_ASAAS_SANDBOX) +$assas = PHPay::asaas(TOKEN_ASAAS_SANDBOX); + +$invoices = (array) $assas + ->invoice(createOnly: false) + ->with([]) + ->all(); + +print_r($invoices); + +$qrcode = $assas ->client($client) ->invoice($invoice, false) ->qrCodePix(); diff --git a/src/Contracts/GatewayInterface.php b/src/Contracts/GatewayInterface.php index f13dcd4..529e1a7 100644 --- a/src/Contracts/GatewayInterface.php +++ b/src/Contracts/GatewayInterface.php @@ -5,6 +5,6 @@ interface GatewayInterface { public function __construct(string $token, bool $sandbox = true); - public function client(array $client): object; - public function invoice(array $invoice): object; + public function client(array $client, bool $createOnly = true): object; + public function invoice(array $invoice, bool $createOnly = true): object; } diff --git a/src/Gateways/Asaas/AsaasGateway.php b/src/Gateways/Asaas/AsaasGateway.php index 953505c..e0bfa2e 100644 --- a/src/Gateways/Asaas/AsaasGateway.php +++ b/src/Gateways/Asaas/AsaasGateway.php @@ -5,41 +5,26 @@ use Illuminate\Support\Facades\Http; use PHPay\Contracts\GatewayInterface; use PHPay\Exceptions\AsaasExceptions; -use PHPay\Gateways\Asaas\Enums\{BillingType}; +use PHPay\Gateways\Asaas\Enums\BillingTypeEnum; use PHPay\Gateways\Asaas\Requests\AsaasPixRequest; use PHPay\Gateways\Asaas\Resources\{Auth, Client, Invoice}; use PHPay\Gateways\Gateway; class AsaasGateway extends Gateway implements GatewayInterface { - /** - * @var array - */ - public array $client; - - /** - * @var array - */ - public array $invoice; - - /** - * @var array - */ - private array $payment; - - /** - * @var object - */ - public object $resource; - /** * construct * * @param string $token * @param bool $sandbox */ - public function __construct(string $token, bool $sandbox = true) - { + public function __construct( + private string $token, + private bool $sandbox = true, + public array $client = [], + public array $invoice = [], + public array $payment = [], + ) { new Auth($token, $sandbox); } @@ -47,16 +32,17 @@ public function __construct(string $token, bool $sandbox = true) * set client * * @param array $client + * @param bool $createOnly * @return Client|self */ - public function client( - array $client = [], - bool $createOnly = true - ): Client|self { + public function client(array $client = [], bool $createOnly = true): Client|self + { $clientInstance = new Client($client, $this); if ($createOnly && !empty($client)) { - $this->client = $clientInstance->create()->client; + $clientInstance->create(); + + $this->client = $clientInstance->client; return $this; } @@ -68,21 +54,26 @@ public function client( * set invoice * * @param array $invoice + * @param bool $createOnly * @return Invoice|self */ - public function invoice( - array $invoice = [], - bool $createOnly = true - ): Invoice|self { + public function invoice(array $invoice = [], bool $createOnly = true): Invoice|self + { $invoiceInstance = new Invoice($invoice, $this); if (!empty($invoice)) { - $this->invoice = $invoiceInstance->create()->invoice; + $invoiceInstance->create(); + + $this->invoice = $invoiceInstance->invoice; + + if ($createOnly) { + return $this; + } + + return $invoiceInstance; } - return $createOnly ? - $this : - $invoiceInstance; + return $invoiceInstance; } /** @@ -100,7 +91,7 @@ public function pix(array $pix): self|AsaasExceptions $payment = Http::asaas() ->post('/payments', [ 'customer' => $this->client['id'], - 'billingType' => BillingType::PIX->value, + 'billingType' => BillingTypeEnum::PIX->value, 'value' => $amount, 'dueDate' => $due_date, 'description' => $description, diff --git a/src/Gateways/Asaas/Enums/BillingType.php b/src/Gateways/Asaas/Enums/BillingTypeEnum.php similarity index 86% rename from src/Gateways/Asaas/Enums/BillingType.php rename to src/Gateways/Asaas/Enums/BillingTypeEnum.php index dfbf09c..803dcf0 100644 --- a/src/Gateways/Asaas/Enums/BillingType.php +++ b/src/Gateways/Asaas/Enums/BillingTypeEnum.php @@ -2,7 +2,7 @@ namespace PHPay\Gateways\Asaas\Enums; -enum BillingType: string +enum BillingTypeEnum: string { case UNDEFINED = 'UNDEFINED'; case BOLETO = 'BOLETO'; diff --git a/src/Gateways/Asaas/Enums/InvoiceFiltersEnum.php b/src/Gateways/Asaas/Enums/InvoiceFiltersEnum.php new file mode 100644 index 0000000..71e8947 --- /dev/null +++ b/src/Gateways/Asaas/Enums/InvoiceFiltersEnum.php @@ -0,0 +1,30 @@ +invoice->billingType); } - if (!BillingType::tryFrom($invoice['billingType'])) { + if (!BillingTypeEnum::tryFrom($invoice['billingType'])) { throw new \InvalidArgumentException(self::messages()->invoice->billingType); } diff --git a/src/Gateways/Asaas/Resources/Interfaces/InvoiceInterface.php b/src/Gateways/Asaas/Resources/Interfaces/InvoiceInterface.php index 4a09089..562bd04 100644 --- a/src/Gateways/Asaas/Resources/Interfaces/InvoiceInterface.php +++ b/src/Gateways/Asaas/Resources/Interfaces/InvoiceInterface.php @@ -6,11 +6,8 @@ interface InvoiceInterface { - // public function all(): self; - // public function get(): array; - // public function with(array $filters): self; - // public function find(string $invoiceId): array; + public function all(): array; public function create(): self|AsaasExceptions; - // public function delete(): bool; + public function with(array $filters): self; public function qrCodePix(): array; } diff --git a/src/Gateways/Asaas/Resources/Invoice.php b/src/Gateways/Asaas/Resources/Invoice.php index 93c2001..20f83d8 100644 --- a/src/Gateways/Asaas/Resources/Invoice.php +++ b/src/Gateways/Asaas/Resources/Invoice.php @@ -4,50 +4,48 @@ use Illuminate\Support\Facades\Http; use PHPay\Exceptions\AsaasExceptions; +use PHPay\Gateways\Asaas\Enums\InvoiceFiltersEnum; use PHPay\Gateways\Asaas\Requests\AsaasInvoiceRequest; use PHPay\Gateways\Asaas\Resources\Interfaces\InvoiceInterface; use PHPay\Gateways\Gateway; final class Invoice implements InvoiceInterface { - /** - * @var array - */ - public array $client; - - /** - * @var array - */ - public array $invoice; - - /** - * @var array - */ - public array $qrcode; - - /** - * @var array - */ - protected array $filters = []; - - /** - * @var Gateway - */ - protected Gateway $gateway; - /** * construct * * @param array $invoice * @param Gateway $gateway */ - public function __construct(array $invoice, Gateway $gateway) - { + public function __construct( + public array $invoice, + protected Gateway $gateway, + public array $client = [], + public array $qrcode = [], + public array $filters = [], + public array $invoices = [], + ) { $this->invoice = $invoice; $this->gateway = $gateway; $this->client = $gateway->client; } + /** + * get all invoices + * + * @return array + */ + public function all(): array + { + $invoices = Http::asaas() + ->get(env('ASSAS_PAYMENTS') . '/?' . http_build_query($this->filters)) + ->json(); + + $this->invoices = $invoices['data']; + + return $this->invoices; + } + /** * create invoice * @@ -62,10 +60,34 @@ public function create(): self|AsaasExceptions $this->invoice ); - $this->invoice = Http::asaas() + $invoice = Http::asaas() ->post(env('ASSAS_PAYMENTS'), $this->invoice) ->json(); + $this->invoice = $invoice; + + return $this; + } + + /** + * set filters + * + * + * @param array $filters + * @return self + * @throws AsaasExceptions + * @see PHPay\Gateways\Asaas\Enums\InvoiceFiltersEnum + */ + public function with(array $filters): self + { + foreach ($filters as $key => $value) { + if (!in_array($key, InvoiceFiltersEnum::cases())) { + throw new AsaasExceptions('filter not found' . $key . ' not found'); + } + } + + $this->filters = $filters; + return $this; } diff --git a/src/Gateways/Gateway.php b/src/Gateways/Gateway.php index 55770b9..93481dd 100644 --- a/src/Gateways/Gateway.php +++ b/src/Gateways/Gateway.php @@ -5,6 +5,8 @@ abstract class Gateway { public array $client; - abstract public function client(array $client = []): object; - abstract public function invoice(array $invoice): object; + + public array $invoice; + abstract public function client(array $client, bool $createOnly = true): object; + abstract public function invoice(array $invoice, bool $createOnly = true): object; } diff --git a/src/PHPay.php b/src/PHPay.php index 9bc811a..b79a1fc 100644 --- a/src/PHPay.php +++ b/src/PHPay.php @@ -11,29 +11,16 @@ class PHPay { - /** - * @var Gateway - */ - protected Gateway $gateway; - - /** - * @var array - */ - protected array $client; - - /** - * @var array - */ - protected array $invoice; - /** * construct * * @param Gateway $gateway */ - public function __construct(Gateway $gateway) - { - $this->gateway = $gateway; + public function __construct( + protected Gateway $gateway, + protected array $client = [], + protected array $invoice = [] + ) { } /** @@ -75,9 +62,9 @@ public static function asaas(string $token, $sandbox = true): self * @param array $client * @return object */ - public function client(array $client = []): object + public function client(array $client = [], $createOnly = true): object { - return $this->gateway->client($client); + return $this->gateway->client($client, $createOnly); } /** @@ -86,8 +73,8 @@ public function client(array $client = []): object * @param array $invoice * @return object */ - public function invoice(array $invoice = []): object + public function invoice(array $invoice = [], $createOnly = true): object { - return $this->gateway->invoice($invoice); + return $this->gateway->invoice($invoice, $createOnly); } }