選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CreateConnectedAccount.php 2.9KB

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