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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor/
node_modules/
node_modules/
examples/asaas/credentials.php
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
}
},
"require": {
"laravel/framework": "^11.32"
"laravel/framework": "^11.32",
"vlucas/phpdotenv": "^5.6"
},
"scripts": {
"test": "vendor/bin/pest"
}
}
}
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 0 additions & 33 deletions examples/asaas.php

This file was deleted.

78 changes: 78 additions & 0 deletions examples/asaas/clients.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

use Payhub\Payhub;
use Payhub\Gateways\Asaas\AsaasGateway;

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

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

/**
* initialize payhub
*
* @param array $credentials <token>
* @param bool $sandbox
*
* @return AsaasGateway
*/
$payhub = Payhub::asaas(TOKEN_ASAAS_SANDBOX);

/**
* store asaas cliente
*
* @param array $client <name, cpf_cnpj>
* @return string cliente id asaas
*/
$payhub
->client($client)
->store();

/**
* list all clients with filters
*
* @return array clients
*/
$response = $payhub
->client()
->with(['cpfCnpj' => '09102295466',])
->all();

// print_r($response);

/**
* get client by cpf_cnpj
*
* @param array $client <cpf_cnpj>
* @return array client
*/
$response = $payhub
->client()
->with(['cpfCnpj' => '09102295466'])
->get();

// print_r($response);

/**
* delete cliente no asaas
*
* @param array $client <cpf_cnpj>
* @return bool
*/
$payhub->client($client)
->delete();

/**
* restore cliente no asaas
*
* @param array $client <cpf_cnpj>
* @return bool
*/
$response = $payhub
->client()
->restore('cus_000006376400');

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

const TOKEN_ASAAS_SANDBOX = '';

/* client to testing */
const NAME = '';
const CPF_CNPJ = '';
10 changes: 10 additions & 0 deletions src/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ASAAS API BASE
ASSAS_URL=https://api.asaas.com/v3
ASAAS_URL_SANDBOX=https://sandbox.asaas.com/api/v3

# ASAAS CLIENTS ENDPOINTS
ASSAS_CLIENTS=/customers
ASSAS_CLIENT=/customers/{id}
ASSAS_CLIENT_RESTORE=/customers/{id}/restore
ASAAS_CLIENT_NOTIFICATIONS=/customers/{id}/notifications

9 changes: 2 additions & 7 deletions src/Contracts/GatewayInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

namespace Payhub\Contracts;

use Exception;

interface GatewayInterface
{
public function authorize(array $credentials, bool $sandbox = true): self|Exception;

public function client(array $client): self|Exception;

public function pix(array $pix): self|Exception;
public function __construct(string $token, bool $sandbox = true);
public function client(array $client): object;
}
8 changes: 0 additions & 8 deletions src/Enums/Gateways.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Exceptions/AsaasExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Exception;

class AsaasExceptions
final class AsaasExceptions
{
/**
* AsaasExceptions invokable.
Expand Down
112 changes: 52 additions & 60 deletions src/Gateways/Asaas/AsaasGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,100 +5,92 @@
use Illuminate\Support\Facades\Http;
use Payhub\Contracts\GatewayInterface;
use Payhub\Exceptions\AsaasExceptions;
use Payhub\Gateways\Asaas\Requests\AsaasClientRequest;
use Payhub\Gateways\Asaas\Enums\{BillingType, ClientMethods};
use Payhub\Gateways\Asaas\Requests\AsaasPixRequest;
use Payhub\Gateways\Asaas\Resources\{Auth, Client};
use Payhub\Gateways\Gateway;

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

public function authorize(array $credentials, bool $sandbox = true): self
{
$baseUrl = $sandbox ?
'https://sandbox.asaas.com/api/v3' :
'https://api.asaas.com/v3';

extract($credentials);

if (! isset($token)) {
return (new AsaasExceptions())('As credenciais do asaas precisam de um parâmetro token.');
}

Http::macro('asaas', function () use ($token, $baseUrl) {
return Http::acceptJson()
->baseUrl($baseUrl)
->withHeaders([
'content-type' => 'application/json',
'user-agent' => 'payhub',
'access_token' => $token,
]);
});
/**
* @var array
*/
private array $payment;

return $this;
/**
* construct
*
* @param string $token
* @param bool $sandbox
*/
public function __construct(string $token, bool $sandbox = true)
{
new Auth($token, $sandbox);
}

/**
* set client
*
* @param array $client
* @return Client
*/
public function client(array $client): self
public function client(array $client = []): Client
{
AsaasClientRequest::validate($client);

try {
$clientExists = (object) Http::asaas()
->get('/customers', [
'cpfCnpj' => $client['cpf_cnpj'],
])->json();

if (empty($clientExists->data)) {
$client = Http::asaas()
->post('/customers', [
'name' => $client['name'],
'cpfCnpj' => $client['cpf_cnpj'],
])->json();
return new Client($client, $this);
}
// {
// if (! ClientMethods::tryFrom($method)) {
// return (new AsaasExceptions())('Method not found');
// }

$this->client = $client['id'];
// if (! method_exists(Client::class, $method)) {
// return (new AsaasExceptions())('Method not found');
// }

return $this;
}
// try {
// $this->customerId = (new Client())::$method($client);
// } catch (\Exception $e) {
// return (new AsaasExceptions())($e->getMessage());
// }

$this->client = $clientExists->data[0]['id'];

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

/**
* set pix
* generate pix
*
* @param array $pix
*/
public function pix(array $pix): self
public function pix(array $pix): self|AsaasExceptions
{
try {
AsaasPixRequest::validate($pix);

$pix = Http::asaas()
extract($pix);

$payment = Http::asaas()
->post('/payments', [
'customer' => $this->client,
'billingType' => 'BOLETO',
'value' => $pix['amount'],
'dueDate' => $pix['due_date'],
'description' => $pix['description'],
'customer' => $this->customerId,
'billingType' => BillingType::PIX->value,
'value' => $amount,
'dueDate' => $due_date,
'description' => $description,
])->json();

print_r($pix);
$this->payment = $payment;

echo 'Pix gerado com sucesso' . PHP_EOL;
echo 'Pix: ' . $this->payment['id'] . PHP_EOL;

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

return $this;
}
}
11 changes: 11 additions & 0 deletions src/Gateways/Asaas/Enums/BillingType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Payhub\Gateways\Asaas\Enums;

enum BillingType: string
{
case UNDEFINED = 'UNDEFINED';
case BOLETO = 'BOLETO';
case CREDIT_CARD = 'CREDIT_CARD';
case PIX = 'PIX';
}
12 changes: 12 additions & 0 deletions src/Gateways/Asaas/Enums/ClientMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Payhub\Gateways\Asaas\Enums;

enum ClientMethods: string
{
case ALL = 'all';
case FIND = 'find';
case STORE = 'store';
case UPDATE = 'update';
case DELETE = 'delete';
}
Loading