diff --git a/app/Filament/Resources/OrderResource/Pages/CreateOrder.php b/app/Filament/Resources/OrderResource/Pages/CreateOrder.php index 3cbd782..b68e14c 100644 --- a/app/Filament/Resources/OrderResource/Pages/CreateOrder.php +++ b/app/Filament/Resources/OrderResource/Pages/CreateOrder.php @@ -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'], @@ -196,7 +203,6 @@ protected function handleRecordCreation(array $data): Model ]); - return $record; } @@ -237,14 +243,6 @@ 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!'; @@ -252,7 +250,6 @@ protected function getCreatedNotificationTitle(): ?string public function chargePix(): void { - // 1. Gerar cobrança $data = [ "billingType" => "PIX", @@ -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'])) { @@ -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); @@ -301,7 +294,6 @@ public function checkPayment() } } - if ( in_array( $this->paymentStatus, diff --git a/app/Helpers/custom-helpers.php b/app/Helpers/custom-helpers.php index d8a4412..f8f6a27 100644 --- a/app/Helpers/custom-helpers.php +++ b/app/Helpers/custom-helpers.php @@ -1,7 +1,16 @@ $adapter]); + } +} + if (!function_exists('clear_string')) { function clear_string(?string $string): ?string { diff --git a/app/Services/PaymentGateway/Connectors/AsaasConnector.php b/app/Services/PaymentGateway/Connectors/AsaasConnector.php index e506645..6a98186 100644 --- a/app/Services/PaymentGateway/Connectors/AsaasConnector.php +++ b/app/Services/PaymentGateway/Connectors/AsaasConnector.php @@ -6,7 +6,6 @@ use App\Services\PaymentGateway\Connectors\Asaas\Concerns\AsaasConfig; use App\Services\PaymentGateway\Contracts\AdapterInterface; -use Illuminate\Support\Facades\Http; class AsaasConnector implements AdapterInterface { @@ -14,49 +13,54 @@ class AsaasConnector implements AdapterInterface 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)]; } } }