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
14 changes: 6 additions & 8 deletions examples/asaas/clients.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
* @param array $client <name, cpf_cnpj>
* @return string cliente id asaas
*/
$phpay
->client($client)
->store();
$phpay->client($client);

/**
* list all clients with filters
Expand All @@ -39,7 +37,7 @@
->with(['cpfCnpj' => '09102295466', ])
->all();

// print_r($response);
print_r($response);

/**
* get client by cpf_cnpj
Expand All @@ -52,7 +50,7 @@
->with(['cpfCnpj' => '09102295466'])
->get();

// print_r($response);
print_r($response);

/**
* delete cliente no asaas
Expand All @@ -61,7 +59,7 @@
* @return bool
*/
$phpay
->client($client)
->client($client, false)
->delete();

/**
Expand All @@ -71,7 +69,7 @@
->client()
->restore('cus_000006376400');

// print_r($response);
print_r($response);

/**
* notifications cliente no asaas
Expand All @@ -80,4 +78,4 @@
->client()
->notifications('cus_000006376400');

// print_r($response);
print_r($response);
25 changes: 25 additions & 0 deletions examples/asaas/invoices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use PHPay\PHPay;

require_once __DIR__ . '/../../vendor/autoload.php';

require_once __DIR__ . '/credentials.php';

$client = [
'name' => NAME,
'cpf_cnpj' => CPF_CNPJ,
];

$invoice = [
'billingType' => 'BOLETO',
'value' => 100.00,
'description' => 'Teste de fatura',
'dueDate' => date('Y-m-d', strtotime('+1 day')),
];

$data = PHPay::asaas(TOKEN_ASAAS_SANDBOX)
->client($client)
->invoice($invoice);

print_r($data);
5 changes: 5 additions & 0 deletions src/.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ ASSAS_CLIENT=/customers/{id}
ASSAS_CLIENT_RESTORE=/customers/{id}/restore
ASAAS_CLIENT_NOTIFICATIONS=/customers/{id}/notifications

# ASAAS PAYMENTS ENDPOINTS
ASSAS_PAYMENTS=/payments
ASAAS_PAYMENT=/payments/{id}
ASAAS_PAYMENT_INFO=/payments/{id}/billingInfo
ASAAS_PAYMENT_VIEW_INFO=/payments/{id}/viewingInfo
1 change: 1 addition & 0 deletions src/Contracts/GatewayInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ interface GatewayInterface
{
public function __construct(string $token, bool $sandbox = true);
public function client(array $client): object;
public function invoice(array $invoice): object;
}
2 changes: 1 addition & 1 deletion src/Exceptions/AsaasExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ final class AsaasExceptions
*/
public function __invoke(string $message): Exception
{
return throw new Exception('Gateway Asaas: ' . $message);
return throw new Exception('ASAAS EXCEPTION: ' . $message);
}
}
63 changes: 55 additions & 8 deletions src/Gateways/Asaas/AsaasGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,31 @@
use PHPay\Exceptions\AsaasExceptions;
use PHPay\Gateways\Asaas\Enums\{BillingType};
use PHPay\Gateways\Asaas\Requests\AsaasPixRequest;
use PHPay\Gateways\Asaas\Resources\{Auth, Client};
use PHPay\Gateways\Asaas\Resources\{Auth, Client, Invoice};
use PHPay\Gateways\Gateway;

class AsaasGateway extends Gateway implements GatewayInterface
{
/**
* @var string
* @var array
*/
private string $customerId;
public array $client;

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

/**
* @var array
*/
private array $payment;

/**
* @var object
*/
public object $resource;

/**
* construct
*
Expand All @@ -37,11 +47,48 @@ public function __construct(string $token, bool $sandbox = true)
* set client
*
* @param array $client
* @return Client
* @return Client|self
*/
public function client(array $client = []): Client
{
return new Client($client, $this);
public function client(
array $client = [],
bool $createOnly = true
): Client|self {
$clientInstance = new Client($client, $this);

if ($createOnly && !empty($client)) {
$this->client = $clientInstance->create()->client;

return $this;
}

return $clientInstance;
}

/**
* set invoice
*
* @param array $invoice
* @return Invoice|self
*/
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)) {
$this->invoice = $invoiceInstance->create()->invoice;

$this;
}

return $invoiceInstance;
}

/**
Expand All @@ -58,7 +105,7 @@ public function pix(array $pix): self|AsaasExceptions

$payment = Http::asaas()
->post('/payments', [
'customer' => $this->customerId,
'customer' => $this->client['id'],
'billingType' => BillingType::PIX->value,
'value' => $amount,
'dueDate' => $due_date,
Expand Down
2 changes: 1 addition & 1 deletion src/Gateways/Asaas/Requests/AsaasClientRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function validate(array $client): void
*
* @return stdClass
*/
public function messages(): stdClass
public static function messages(): stdClass
{
return (object) [
'name' => 'Asaas: Nome do cliente é obrigatório para o Asaas',
Expand Down
27 changes: 19 additions & 8 deletions src/Gateways/Asaas/Resources/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
use PHPay\Gateways\Asaas\Resources\Interfaces\ClientInterface;
use PHPay\Gateways\Gateway;

final class Client implements ClientInterface
final class Client extends Resource implements ClientInterface
{
/**
* @var array
*/
protected array $client;
public array $clients;

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

/**
* @var array
Expand Down Expand Up @@ -42,13 +47,15 @@ public function __construct(array $client, Gateway $gateway)
*
* @return array
*/
public function all(): array
public function all(): self
{
$client = Http::asaas()
->get(env('ASSAS_CLIENTS') . '/?' . http_build_query($this->filters))
->json();

return $client;
$this->clients = $client['data'];

return $this;
}

/**
Expand Down Expand Up @@ -97,11 +104,11 @@ public function with(array $filter): self
}

/**
* store client
* create client
*
* @return $this
*/
public function store(): string|AsaasExceptions
public function create(): self|AsaasExceptions
{
AsaasClientRequest::validate($this->client);

Expand All @@ -111,7 +118,9 @@ public function store(): string|AsaasExceptions
$client = self::find($cpf_cnpj);

if (!empty($client)) {
return $client[0]['id'];
$this->client = $client[0];

return $this;
}

$client = Http::asaas()
Expand All @@ -124,11 +133,13 @@ public function store(): string|AsaasExceptions
return (new AsaasExceptions())($client['errors'][0]['description']);
}

return $client['id'];
$this->client = $client;

} catch (\Exception $e) {
return (new AsaasExceptions())($e->getMessage());
}

return $this;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Gateways/Asaas/Resources/Interfaces/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

interface ClientInterface
{
public function all(): array;
public function all(): self;
public function get(): array;
public function find(string $cpfCnpj): array;
public function with(array $filters): self;
public function store(): string|AsaasExceptions;
public function create(): self|AsaasExceptions;
public function delete(): bool;
public function restore(string $id): bool;
public function notifications(string $id): ?array;
Expand Down
10 changes: 10 additions & 0 deletions src/Gateways/Asaas/Resources/Interfaces/InvoiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace PHPay\Gateways\Asaas\Resources\Interfaces;

use PHPay\Exceptions\AsaasExceptions;

interface InvoiceInterface
{
public function create(): self|AsaasExceptions;
}
56 changes: 56 additions & 0 deletions src/Gateways/Asaas/Resources/Invoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace PHPay\Gateways\Asaas\Resources;

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

final class Invoice extends Resource implements InvoiceInterface
{
/**
* @var array
*/
public array $invoice;

/**
* @var array
*/
protected array $filters = [];

/**
* @var Gateway
*/
protected Gateway $gateway;

/**
* construct
*
* @param array $invoice
* @param Gateway $gateway
*/
public function __construct(array $invoice, Gateway $gateway)
{
$this->invoice = $invoice;
$this->gateway = $gateway;
}

/**
* create invoice
*
* @return self|AsaasExceptions
*/
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());
}

return $this;
}
}
Loading