From 19ef27b5e5ab124aa1a6e962906a43f90ffc57f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Wed, 12 Jun 2024 19:33:33 +0200 Subject: [PATCH] Refactor GoCardlessOAuthWebhookController for WebhookRequest --- .../GoCardlessOAuthWebhookController.php | 42 ++++++++++++++----- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Gateways/GoCardlessOAuthWebhookController.php b/app/Http/Controllers/Gateways/GoCardlessOAuthWebhookController.php index b0a4286e3e..e9461594f5 100644 --- a/app/Http/Controllers/Gateways/GoCardlessOAuthWebhookController.php +++ b/app/Http/Controllers/Gateways/GoCardlessOAuthWebhookController.php @@ -10,22 +10,44 @@ * @license https://www.elastic.co/licensing/elastic-license */ -// @todo: Double check if this should be in admin module - namespace App\Http\Controllers\Gateways; use App\Http\Controllers\Controller; -use Illuminate\Http\Request; +use App\Http\Requests\GoCardless\WebhookRequest; +use App\Models\CompanyGateway; +use App\Repositories\CompanyRepository; +use Illuminate\Support\Arr; -class GoCardlessOAuthController extends Controller -{ - public function __invoke(Request $request) +class GoCardlessOAuthWebhookController extends Controller +{ + public function __construct( + protected CompanyRepository $company_repository, + ) { + } + + public function __invoke(WebhookRequest $request) { foreach ($request->events as $event) { - match ($event['details']['cause']) { - 'app_disconnected' => info($event), - default => nlog('Not acting on this event type: ' . $event['details']['cause']), - }; + nlog($event['action']); + + $e = Arr::dot($event); + + if ($event['action'] === 'disconnected') { + + /** @var \App\Models\CompanyGateway $company_gateway */ + $company_gateway = CompanyGateway::query() + ->whereJsonCotains('config->account_id', $e['links.organisation']) + ->firstOrFail(); + + $current = $company_gateway->getConfig('__current'); + + if ($current) { + $company_gateway->setConfig($current); + $company_gateway->save(); + } + + $this->company_repository->archive($company_gateway); + } } } }