Skip to content

Commit

Permalink
fix: solve problem of client response
Browse files Browse the repository at this point in the history
- The Payment Gateway Client Response was solved
- Created custom helper to instantiate Gateway
  • Loading branch information
icarojobs committed Aug 28, 2024
1 parent 838e141 commit bfd6e41
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 45 deletions.
48 changes: 20 additions & 28 deletions app/Filament/Resources/OrderResource/Pages/CreateOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,32 +157,39 @@ protected function getSteps(): array
];
}

/**
* @throws Halt
*/
protected function handleRecordCreation(array $data): Model
{
$chargeData = $this->prepareData($this->data);

$gateway = self::Gateway();
$gateway = get_gateway();
$payment = $gateway->payment()->create($chargeData);

$creditCardData = $this->prepareCreditCardData($this->data);

$response = $gateway->payment()->payWithCreditCard($payment['id'], $creditCardData);
$error = data_get($payment, 'error.errors.*.description')[0] ?? null;

if ($response['status'] == 'CONFIRMED') {

$data['status'] = OrderStatusEnum::PAID->value;
$record = static::getModel()::create($data);
} else {
if ($error) {
Notification::make()
->title('Erro ao processar pagamento')
->body('Não foi prossivel processar o pagamento. Tente novamente.')
->body($error)
->danger()
->persistent()
->send();

throw new Halt();
}

$creditCardData = $this->prepareCreditCardData($this->data);

$response = $gateway->payment()->payWithCreditCard($payment['id'], $creditCardData);

if ($response['status'] == 'CONFIRMED') {
$data['status'] = OrderStatusEnum::PAID->value;
$record = static::getModel()::create($data);
}

//This need to be a job??!?!
//This need to be a job??!?! Maybe yes.
OrderTransaction::create([
'order_id' => $record->id,
'charge_id' => $response['id'],
Expand All @@ -196,7 +203,6 @@ protected function handleRecordCreation(array $data): Model

]);


return $record;
}

Expand Down Expand Up @@ -237,22 +243,13 @@ private function prepareCreditCardData(array $data): array
];
}

private static function Gateway()
{
$adapter = app(AsaasConnector::class);
$gateway = app(Gateway::class, ['adapter' => $adapter]);

return $gateway;
}

protected function getCreatedNotificationTitle(): ?string
{
return 'Venda realizada com sucesso!';
}

public function chargePix(): void
{

// 1. Gerar cobrança
$data = [
"billingType" => "PIX",
Expand All @@ -263,9 +260,7 @@ public function chargePix(): void
"daysAfterDueDateToCancellationRegistration" => 1,
];

$adapter = app(AsaasConnector::class);
$gateway = app(Gateway::class, ['adapter' => $adapter]);

$gateway = get_gateway();
$payment = $gateway->payment()->create($data);

if (!isset($payment['id'])) {
Expand All @@ -286,9 +281,7 @@ public function checkPayment()
{
$chargeId = session('session_'.Auth::id())['payment_id'];

$adapter = app(AsaasConnector::class);
$gateway = new Gateway($adapter);

$gateway = get_gateway();
$payment = $gateway->payment()->get($chargeId);

$this->setStatus($payment);
Expand All @@ -301,7 +294,6 @@ public function checkPayment()
}
}


if (
in_array(
$this->paymentStatus,
Expand Down
9 changes: 9 additions & 0 deletions app/Helpers/custom-helpers.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<?php

use App\Services\PaymentGateway\Connectors\AsaasConnector;
use App\Services\PaymentGateway\Gateway;
use Carbon\CarbonImmutable;

if (!function_exists('get_gateway')) {
function get_gateway() {
$adapter = app(AsaasConnector::class);
return app(Gateway::class, ['adapter' => $adapter]);
}
}

if (!function_exists('clear_string')) {
function clear_string(?string $string): ?string
{
Expand Down
38 changes: 21 additions & 17 deletions app/Services/PaymentGateway/Connectors/AsaasConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,61 @@

use App\Services\PaymentGateway\Connectors\Asaas\Concerns\AsaasConfig;
use App\Services\PaymentGateway\Contracts\AdapterInterface;
use Illuminate\Support\Facades\Http;

class AsaasConnector implements AdapterInterface
{
use AsaasConfig;

public function get(string $url)
{
$request = $this->http
->get($url);

try {
return $this->http
->get($url)
return $request
->throw()
->json();
} catch (\Exception $exception) {
return ['error' => $exception->getMessage()];
} catch (\Exception) {
return ['error' => json_decode($request->body(), true)];
}
}

public function post(string $url, array $params)
{
$request = $this->http->post($url, $params);

try {
return $this->http
->post($url, $params)
return $request
->throw()
->json();
} catch (\Exception $exception) {
return ['error' => $exception->getMessage()];
} catch (\Exception) {
return ['error' => json_decode($request->body(), true)];
}
}

public function delete(string $url)
{
$request = $this->http->delete($url);

try {
return $this->http
->delete($url)
return $request
->throw()
->json();
} catch (\Exception $exception) {
return ['error' => $exception->getMessage()];
} catch (\Exception) {
return ['error' => json_decode($request->body(), true)];
}
}

public function put(string $url, array $params)
{
$request = $this->http->put($url, $params);

try {
return $this->http
->put($url, $params)
return $request
->throw()
->json();
} catch (\Exception $exception) {
return ['error' => $exception->getMessage()];
} catch (\Exception) {
return ['error' => json_decode($request->body(), true)];
}
}
}

0 comments on commit bfd6e41

Please sign in to comment.