Skip to content

Commit f2bb7ec

Browse files
committed
PAYH-4: feat: addeed remove client action
1 parent 2dacb95 commit f2bb7ec

File tree

12 files changed

+155
-50
lines changed

12 files changed

+155
-50
lines changed

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
}
2222
},
2323
"require": {
24-
"laravel/framework": "^11.32"
24+
"laravel/framework": "^11.32",
25+
"vlucas/phpdotenv": "^5.6"
2526
},
2627
"scripts": {
2728
"test": "vendor/bin/pest"
2829
}
29-
}
30+
}

composer.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/asaas.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Payhub\Payhub;
44
use Payhub\Enums\Gateways;
5+
use Payhub\Gateways\Asaas\Enums\ClientMethods;
56

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

@@ -23,9 +24,22 @@
2324
/* instance singleton */
2425
$payhub = (new Payhub())
2526
->gateway(Gateways::ASAAS)
26-
->auth($credentials, sandbox: true)
27-
->client()
28-
->store($client);
27+
->auth($credentials, sandbox: true);
28+
29+
/* cadastrar client no asaas */
30+
$payhub
31+
->client($client)
32+
->pix($pix);
33+
34+
/* excluir cliente */
35+
// $payhub
36+
// ->client($client, method: 'delete');
37+
38+
/* localizar cliente no asaas pelo documento */
39+
// $payhub
40+
// ->client()
41+
// ->find('09102295466');
42+
2943

3044
// TODO: create feature to extend resources
3145
// TODO: command with stubs to create resources

src/.env

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# ASAAS API BASE
2+
ASSAS_URL=https://api.asaas.com/v3
3+
ASAAS_URL_SANDBOX=https://sandbox.asaas.com/api/v3
4+
5+
# ASAAS CLIENTS ENDPOINTS
6+
ASSAS_CLIENTS=/customers
7+
ASSAS_CLIENT=/customers/{id}
8+
ASSAS_CLIENT_RESTORE=/customers/{id}/restore
9+
ASAAS_CLIENT_NOTIFICATIONS=/customers/{id}/notifications
10+

src/Contracts/GatewayInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
interface GatewayInterface
88
{
99
public function auth(array $credentials, bool $sandbox = true): object;
10-
public function client(): object;
11-
public function pix(array $pix): self|Exception;
10+
public function client(array $client): object;
11+
public function pix(array $pix): object;
1212
}

src/Exceptions/AsaasExceptions.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Exception;
66

7-
class AsaasExceptions
7+
final class AsaasExceptions
88
{
99
/**
1010
* AsaasExceptions invokable.

src/Gateways/Asaas/AsaasGateway.php

+37-14
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55
use Illuminate\Support\Facades\Http;
66
use Payhub\Contracts\GatewayInterface;
77
use Payhub\Exceptions\AsaasExceptions;
8+
use Payhub\Gateways\Asaas\Enums\{BillingType, ClientMethods};
89
use Payhub\Gateways\Asaas\Requests\AsaasPixRequest;
9-
use Payhub\Gateways\Asaas\Resources\Auth;
10-
use Payhub\Gateways\Asaas\Resources\Client;
10+
use Payhub\Gateways\Asaas\Resources\{Auth, Client};
1111

1212
class AsaasGateway implements GatewayInterface
1313
{
14+
/**
15+
* @var string
16+
*/
17+
private string $client;
18+
1419
/**
1520
* @var array
1621
*/
17-
protected array $client;
22+
private array $payment;
1823

1924
/**
2025
* auth
@@ -34,37 +39,55 @@ public function auth(array $credentials, bool $sandbox = true): self
3439
* set client
3540
*
3641
* @param array $client
37-
* @return Client
42+
* @return self
3843
*/
39-
public function client(): Client
44+
public function client(array $client, string $method = 'store'): self|AsaasExceptions
4045
{
41-
return new Client();
46+
if (! ClientMethods::tryFrom($method)) {
47+
return (new AsaasExceptions())('Method not found');
48+
}
49+
50+
if (! method_exists(Client::class, $method)) {
51+
return (new AsaasExceptions())('Method not found');
52+
}
53+
54+
try {
55+
$this->client = (new Client())::$method($client);
56+
} catch (\Exception $e) {
57+
return (new AsaasExceptions())($e->getMessage());
58+
}
59+
60+
return $this;
4261
}
4362

4463
/**
4564
* set pix
4665
*
4766
* @param array $pix
4867
*/
49-
public function pix(array $pix): self
68+
public function pix(array $pix): self|AsaasExceptions
5069
{
5170
try {
5271
AsaasPixRequest::validate($pix);
5372

54-
$pix = Http::asaas()
73+
extract($pix);
74+
75+
$payment = Http::asaas()
5576
->post('/payments', [
5677
'customer' => $this->client,
57-
'billingType' => 'BOLETO',
58-
'value' => $pix['amount'],
59-
'dueDate' => $pix['due_date'],
60-
'description' => $pix['description'],
78+
'billingType' => BillingType::PIX->value,
79+
'value' => $amount,
80+
'dueDate' => $due_date,
81+
'description' => $description,
6182
])->json();
6283

63-
print_r($pix);
84+
print_r($payment);
6485

65-
return $this;
86+
$this->payment = $payment;
6687
} catch (\Exception $e) {
6788
return (new AsaasExceptions())($e->getMessage());
6889
}
90+
91+
return $this;
6992
}
7093
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Payhub\Gateways\Asaas\Enums;
4+
5+
enum BillingType: string
6+
{
7+
case UNDEFINED = 'UNDEFINED';
8+
case BOLETO = 'BOLETO';
9+
case CREDIT_CARD = 'CREDIT_CARD';
10+
case PIX = 'PIX';
11+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Payhub\Gateways\Asaas\Enums;
4+
5+
enum ClientMethods: string
6+
{
7+
case ALL = 'all';
8+
case FIND = 'find';
9+
case STORE = 'store';
10+
case UPDATE = 'update';
11+
case DELETE = 'delete';
12+
}

src/Gateways/Asaas/Resources/Auth.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class Auth
1616
public function __invoke(array $credentials, bool $sandbox): void
1717
{
1818
$baseUrl = $sandbox ?
19-
'https://sandbox.asaas.com/api/v3' :
20-
'https://api.asaas.com/v3';
19+
env('ASAAS_URL_SANDBOX') :
20+
env('ASAAS_URL');
2121

2222
extract($credentials);
2323

src/Gateways/Asaas/Resources/Client.php

+54-25
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,80 @@
66
use Payhub\Exceptions\AsaasExceptions;
77
use Payhub\Gateways\Asaas\Requests\AsaasClientRequest;
88

9-
class Client
9+
final class Client
1010
{
1111
/**
12-
* @var string
12+
* store client
13+
*
14+
* @param array $client
15+
* @return $this
1316
*/
14-
public string $clientId;
17+
public static function store(array $client): string
18+
{
19+
AsaasClientRequest::validate($client);
20+
21+
try {
22+
23+
extract($client);
24+
25+
$client = self::find($cpf_cnpj);
26+
27+
if (! empty($client)) {
28+
return $client[0]['id'];
29+
}
30+
31+
$client = Http::asaas()
32+
->post(env('ASSAS_CLIENTS'), [
33+
'name' => $name,
34+
'cpfCnpj' => $cpf_cnpj,
35+
])->json();
36+
37+
return $client['id'];
38+
39+
} catch (\Exception $e) {
40+
return (new AsaasExceptions())($e->getMessage());
41+
}
42+
}
1543

1644
/**
17-
* Client constructor.
45+
* check if client exists
46+
*
47+
* @param string $client
48+
* @return array
1849
*/
19-
public function __construct()
50+
public static function find(string $cpfCnpj): array
2051
{
52+
$client = (object) Http::asaas()
53+
->get(env('ASSAS_CLIENTS'), [
54+
'cpfCnpj' => $cpfCnpj,
55+
])->json();
56+
57+
return $client->data;
2158
}
2259

2360
/**
24-
* store client
61+
* update client
2562
*
2663
* @param array $client
27-
* @return $this
64+
* @return string
2865
*/
29-
public function store(array $client): self
66+
public static function delete(array $client): string
3067
{
31-
AsaasClientRequest::validate($client);
32-
3368
try {
34-
$clientExists = (object) Http::asaas()
35-
->get('/customers', [
36-
'cpfCnpj' => $client['cpf_cnpj'],
37-
])->json();
69+
$client = self::find($client['cpf_cnpj']);
3870

39-
if (empty($clientExists->data)) {
40-
$client = Http::asaas()
41-
->post('/customers', [
42-
'name' => $client['name'],
43-
'cpfCnpj' => $client['cpf_cnpj'],
44-
])->json();
71+
if (empty($client)) {
72+
return 'Client not found';
73+
}
4574

46-
$this->clientId = $client['id'];
75+
$client = Http::asaas()
76+
->delete(env('ASSAS_CLIENTS') . '/' . $client[0]['id'])
77+
->json();
4778

48-
return $this;
49-
}
79+
echo 'Client deleted';
5080

51-
$this->clientId = $clientExists->data[0]['id'];
81+
return $client['id'];
5282

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

src/Payhub.php

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Payhub;
44

5+
use Dotenv\Dotenv;
56
use Exception;
67
use Illuminate\Container\Container;
78
use Illuminate\Http\Client\Factory;
@@ -29,9 +30,13 @@ public function __construct()
2930
Facade::setFacadeApplication($container);
3031

3132
$container->singleton('http', function () {
33+
3234
return new Factory();
3335
});
3436

37+
$dotenv = Dotenv::createImmutable(__DIR__);
38+
$dotenv->load();
39+
3540
class_alias(Http::class, 'Http');
3641
}
3742

0 commit comments

Comments
 (0)