Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

CreateConnectedAccount.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Listeners;
  3. use App\Events\PlaidSuccess;
  4. use App\Models\Banking\Institution;
  5. use App\Models\Company;
  6. use App\Services\PlaidService;
  7. use Illuminate\Support\Facades\DB;
  8. class CreateConnectedAccount
  9. {
  10. /**
  11. * Create the event listener.
  12. */
  13. public function __construct(
  14. protected PlaidService $plaidService
  15. ) {}
  16. /**
  17. * Handle the event.
  18. */
  19. public function handle(PlaidSuccess $event): void
  20. {
  21. DB::transaction(function () use ($event) {
  22. $this->processPlaidSuccess($event);
  23. });
  24. }
  25. public function processPlaidSuccess(PlaidSuccess $event): void
  26. {
  27. $accessToken = $event->accessToken;
  28. $company = $event->company;
  29. $authResponse = $this->plaidService->getAccounts($accessToken);
  30. $institutionResponse = $this->plaidService->getInstitution($authResponse->item->institution_id, $company->profile?->address?->country_code);
  31. $this->processInstitution($authResponse, $institutionResponse, $company, $accessToken);
  32. }
  33. public function processInstitution($authResponse, $institutionResponse, Company $company, $accessToken): void
  34. {
  35. $institution = Institution::updateOrCreate([
  36. 'external_institution_id' => $authResponse->item->institution_id ?? null,
  37. ], [
  38. 'name' => $institutionResponse->institution->name ?? null,
  39. 'logo' => $institutionResponse->institution->logo ?? null,
  40. 'website' => $institutionResponse->institution->url ?? null,
  41. ]);
  42. foreach ($authResponse->accounts as $plaidAccount) {
  43. $this->processConnectedBankAccount($plaidAccount, $company, $institution, $authResponse, $accessToken);
  44. }
  45. }
  46. public function processConnectedBankAccount($plaidAccount, Company $company, Institution $institution, $authResponse, $accessToken): void
  47. {
  48. $identifierHash = md5($company->id . $institution->external_institution_id . $plaidAccount->name . $plaidAccount->mask);
  49. $company->connectedBankAccounts()->updateOrCreate([
  50. 'identifier' => $identifierHash,
  51. ], [
  52. 'institution_id' => $institution->id,
  53. 'external_account_id' => $plaidAccount->account_id,
  54. 'access_token' => $accessToken,
  55. 'item_id' => $authResponse->item->item_id,
  56. 'currency_code' => $plaidAccount->balances->iso_currency_code ?? 'USD',
  57. 'current_balance' => $plaidAccount->balances->current ?? 0,
  58. 'name' => $plaidAccount->name,
  59. 'mask' => $plaidAccount->mask,
  60. 'type' => $plaidAccount->type,
  61. 'subtype' => $plaidAccount->subtype,
  62. 'import_transactions' => false,
  63. ]);
  64. }
  65. }