private function getPlaidToken($publicToken, $accountId)
{
$clientId = $this->accountGateway->getPlaidClientId();
$secret = $this->accountGateway->getPlaidSecret();
if (!$clientId) {
throw new Exception('plaid client id not set');
// TODO use text strings
}
if (!$secret) {
throw new Exception('plaid secret not set');
}
try {
$subdomain = $this->accountGateway->getPlaidEnvironment() == 'production' ? 'api' : 'tartan';
$response = (new \GuzzleHttp\Client(['base_uri' => "https://{$subdomain}.plaid.com"]))->request('POST', 'exchange_token', ['allow_redirects' => false, 'headers' => ['content-type' => 'application/x-www-form-urlencoded'], 'body' => http_build_query(['client_id' => $clientId, 'secret' => $secret, 'public_token' => $publicToken, 'account_id' => $accountId])]);
return json_decode($response->getBody(), true);
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
$response = $e->getResponse();
$body = json_decode($response->getBody(), true);
if ($body && !empty($body['message'])) {
throw new Exception($body['message']);
} else {
throw new Exception($e->getMessage());
}
}
}