Skip to content

Commit

Permalink
feat: added customer id integration with ASAAS
Browse files Browse the repository at this point in the history
  • Loading branch information
icarojobs committed Mar 7, 2024
1 parent b10feac commit 6aed087
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 13 deletions.
49 changes: 36 additions & 13 deletions app/Filament/Pages/Auth/FreezerControlRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace App\Filament\Pages\Auth;

use App\Models\Customer;
use App\Services\PaymentGateway\Connectors\AsaasConnector;
use App\Services\PaymentGateway\Gateway;
use Closure;
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
use DomainException;
Expand All @@ -15,6 +17,7 @@
use Filament\Notifications\Notification;
use Filament\Pages\Auth\Register;
use Filament\Support\RawJs;
use Illuminate\Support\Str;
use Override;

class FreezerControlRegister extends Register
Expand All @@ -33,6 +36,7 @@ public function form(Form $form): Form
TextInput::make('document')
->label('CPF')
->required()
->unique()
->mask(RawJs::make(<<<'JS'
'999.999.999-99'
JS
Expand Down Expand Up @@ -69,14 +73,38 @@ function () {
#[Override]
public function register(): ?RegistrationResponse
{
try {
$this->rateLimit(2);
$this->rateLimit(2);

$this->data = $this->form->getState();
$this->data = $this->form->getState();

// if (! $this->checkIfCustomerHasMore18YearsOld()) {
// throw new DomainException('Infelizmente, você não tem idade para se cadastrar no '.config('app.name'));
// }
try {
$adapter = new AsaasConnector();
$gateway = new Gateway($adapter);

$data = [
'name' => $this->data['name'],
'cpfCnpj' => sanitize($this->data['document']),
'email' => $this->data['email'],
'mobilePhone' => sanitize($this->data['mobile']),
];

$customer = $gateway->customer()->create($data);

if (!isset($customer['id'])) {
$message = str('')
->append("Não foi possível criar o ID do Cliente: ")
->append($customers['errors'][0]['description'] ?? 'Erro inesperado')
->toString();

Notification::make('register_error')
->title('Erro ao criar cliente')
->body($message)
->danger()
->persistent()
->send();

return null;
}
} catch (TooManyRequestsException $exception) {
Notification::make()
->title(__('filament-panels::pages/auth/register.notifications.throttled.title', [
Expand All @@ -91,7 +119,7 @@ public function register(): ?RegistrationResponse
->send();

return null;
} catch (DomainException $domainException) {
} catch (\DomainException|\Exception $domainException) {
Notification::make()
->title('Erro ao realizar cadastro')
->body($domainException->getMessage())
Expand All @@ -101,6 +129,7 @@ public function register(): ?RegistrationResponse
return null;
}

$this->data['customer_id'] = $customer['id'];
Customer::create($this->data);

Notification::make()
Expand All @@ -113,10 +142,4 @@ public function register(): ?RegistrationResponse

return null;
}

// private function checkIfCustomerHasMore18YearsOld(): bool
// {
// return now()->parse($this->data['birthdate'])->age >= 18;
// //return (now()->diffInDays($this->data['birthdate']) / 365) >= 18;
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('customers', function (Blueprint $table) {
$table->string('customer_id')->index()->after('user_id');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('customers', function (Blueprint $table) {
$table->dropColumn('customer_id');
});
}
};

0 comments on commit 6aed087

Please sign in to comment.