Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
./vendor/bin/pint
./vendor/bin/pint

git add .
6 changes: 0 additions & 6 deletions src/Gateways/Asaas/AsaasGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ public function invoice(
array $invoice = [],
bool $createOnly = true
): Invoice|self {
if (!isset($this->client['id'])) {
throw new \Exception('Para criar um fatura é necessário a criação de um cliente');
}

$invoice['customer'] = $this->client['id'];

$invoiceInstance = new Invoice($invoice, $this);

if ($createOnly && empty($invoice)) {
Expand Down
63 changes: 63 additions & 0 deletions src/Gateways/Asaas/Requests/AsaasInvoiceRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace PHPay\Gateways\Asaas\Requests;

use PHPay\Gateways\Asaas\Enums\BillingType;
use stdClass;

class AsaasInvoiceRequest
{
/**
* validate client and invoice data
*
* @param array $client
* @param array $invoice
* @return void
*/
public static function validate(array $client, array $invoice): void
{
if (!isset($client['id'])) {
throw new \InvalidArgumentException(self::messages()->client->id);
}

if (!isset($invoice['customer']) && !is_string($invoice['customer'])) {
throw new \InvalidArgumentException(self::messages()->invoice->customer);
}

if (!isset($invoice['billingType'])) {
throw new \InvalidArgumentException(self::messages()->invoice->billingType);
}

if (!BillingType::tryFrom($invoice['billingType'])) {
throw new \InvalidArgumentException(self::messages()->invoice->billingType);
}

if (!isset($invoice['value']) && !is_numeric($invoice['value'])) {
throw new \InvalidArgumentException(self::messages()->invoice->value);
}

if (!isset($invoice['dueDate']) && !is_string($invoice['dueDate'])) {
throw new \InvalidArgumentException(self::messages()->invoice->dueDate);
}
}

/**
* messages for validation
*
* @return stdClass
*/
public static function messages(): stdClass
{
return (object) [
'client' => (object) [
'id' => 'Asaas: Para gerar uma cobrança é necessário um id de cliente do Asaas.',
],
'invoice' => (object) [
'customer' => 'Asaas: O campo customer é obrigatório e deve ser do tipo string.',
'billingType' => 'Asaas: O campo billingType é obrigatório, e tem como disponível as seguintes opções: UNDEFINED, BOLETO, CREDIT_CARD, PIX',
'value' => 'Asaas: O campo value é obrigatório e deve ser do tipo numérico.',
'dueDate' => 'Asaas: O campo dueDate é obrigatório e deve ser do tipo string.',
],
];
}
}
24 changes: 17 additions & 7 deletions src/Gateways/Asaas/Resources/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Http;
use PHPay\Exceptions\AsaasExceptions;
use PHPay\Gateways\Asaas\Requests\AsaasInvoiceRequest;
use PHPay\Gateways\Asaas\Resources\Interfaces\InvoiceInterface;
use PHPay\Gateways\Gateway;

Expand All @@ -14,6 +15,11 @@ final class Invoice extends Resource implements InvoiceInterface
*/
public array $invoice;

/**
* @var array
*/
public array $client;

/**
* @var array
*/
Expand All @@ -34,6 +40,7 @@ public function __construct(array $invoice, Gateway $gateway)
{
$this->invoice = $invoice;
$this->gateway = $gateway;
$this->client = $gateway->client;
}

/**
Expand All @@ -43,13 +50,16 @@ public function __construct(array $invoice, Gateway $gateway)
*/
public function create(): self|AsaasExceptions
{
try {
$this->invoice = Http::asaas()
->post(env('ASSAS_PAYMENTS'), $this->invoice)
->json();
} catch (\Exception $e) {
return (new AsaasExceptions())($e->getMessage());
}
AsaasInvoiceRequest::validate(
$this->client,
$this->invoice
);

$this->invoice['customer'] = $this->client['id'];

$this->invoice = Http::asaas()
->post(env('ASSAS_PAYMENTS'), $this->invoice)
->json();

return $this;
}
Expand Down
1 change: 1 addition & 0 deletions src/Gateways/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

abstract class Gateway
{
public array $client;
abstract public function client(array $client = []): object;
abstract public function invoice(array $invoice): object;
}