Skip to content

Commit

Permalink
Update plaid to use link key (#40)
Browse files Browse the repository at this point in the history
* Update the env example

* Add a method to create a link token

* Add new create link token controller

* Add the configs for the new plaid options

* Update the link feature to work with link tokens!

* Add bin commands for linux

* Clean up the controoller
  • Loading branch information
Austin Kregel authored Sep 20, 2020
1 parent 45efcc5 commit d5fddcf
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PLAID_ENV=sandbox
PLAID_PUBLIC_KEY=
PLAID_SECRET=
PLAID_CLIENT_ID=
PLAID_LANGUAGE=en
PLAID_COUNTRY_CODES=US

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
Expand Down
3 changes: 2 additions & 1 deletion app/Contracts/Services/PlaidServiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ public function getCategories(): array;
* @return LengthAwarePaginatorContract
*/
public function getInstitutions(int $count = 500, int $page = 1): LengthAwarePaginatorContract;
}
public function createLinkToken(string $userId): array;
}
14 changes: 14 additions & 0 deletions app/Http/Controllers/Plaid/CreateLinkTokenController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace App\Http\Controllers\Plaid;

use App\Contracts\Services\PlaidServiceContract;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class CreateLinkTokenController extends Controller
{
public function __invoke(Request $request, PlaidServiceContract $plaid)
{
return $plaid->createLinkToken($request->user()->id);
}
}
18 changes: 18 additions & 0 deletions app/Services/Banking/PlaidService.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,22 @@ public function getInstitutions(int $count = 500, int $page = 1): LengthAwarePag

return new LengthAwarePaginator($items['institutions'], $items['total'], $count, $page);
}

public function createLinkToken(string $userId): array
{
return $this->http
->{config('services.plaid.env')}()
->post('/link/token/create', [
'client_id' => config('services.plaid.client_id'),
'secret' => config('services.plaid.secret_key'),
'client_name' => config('services.plaid.client_name'),
'user' => [
'client_user_id' => $userId
],
'products' => config('services.plaid.products'),
'country_codes' => config('services.plaid.country_codes'),
'language' => config('services.plaid.language'),
])
->toArray();
}
}
3 changes: 3 additions & 0 deletions bin/artisan
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

docker exec -it finance-php php artisan "$@"
3 changes: 3 additions & 0 deletions bin/composer
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

docker exec -it finance-php composer "$@"
8 changes: 6 additions & 2 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@

'plaid' => [
'env' => env('PLAID_ENV', 'sandbox'),
'public_key' => env('PLAID_PUBLIC_KEY', ''),
'secret_key' => env('PLAID_SECRET', ''),
'client_id' => env('PLAID_CLIENT_ID', '')
'client_id' => env('PLAID_CLIENT_ID', ''),
'client_name' => env('APP_NAME'),
'language' => env('PLAID_LANGUAGE', 'en'),
'country_codes' => explode(',', env('PLAID_COUNTRY_CODES', 'US')),
'products' => ['transactions'],

],

'slack_webhook_url' => env('SLACK_WEBHOOK_URL'),
Expand Down
46 changes: 26 additions & 20 deletions resources/js/settings/Plaid/LinkAccount.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,36 @@
return dayjs(date);
},
},
mounted() {
const that = this;
var handler = Plaid.create({
clientName: 'Kregel API',
env: process.env.MIX_PLAID_ENV,
key: process.env.MIX_PLAID_KEY,
product: ['transactions'],
// webhook: this.url,
selectAccount: false,
onSuccess: (public_token, metadata) => {
axios.post('/api/plaid/exchange_token', {
public_token: public_token,
institution: metadata.institution.institution_id
})
.then((res) => {
Bus.$emit('fetchAccounts')
})
}
});
async mounted() {
const fetchLinkToken = async () => {
const { data } = await axios.post('/api/plaid/create-link-token');
return data.link_token;
};
const configs = {
token: await fetchLinkToken(),
onSuccess: async function(public_token, { institution: { institution_id } }) {
await axios.post('/api/plaid/exchange-token', { public_token: public_token, institution: institution_id });
},
onExit: async function(err, metadata) {
if (err != null && err.error_code === 'INVALID_LINK_TOKEN') {
handler.destroy();
handler = Plaid.create({
...configs,
token: await fetchLinkToken(),
});
}
if (err != null) {
this.$toasted.error(err.message || err.error_code);
return;
}
},
};
var handler = Plaid.create(configs)
Bus.$on('fetchAccounts', () => this.getAccounts());
Bus.$emit('fetchAccounts')
$('#link-button').on('click', function(e) {
handler.open();
});
Expand Down
4 changes: 3 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Controllers\Api\SubscriptionsController;
use App\Http\Controllers\DynamicViewController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\Plaid\CreateLinkTokenController;
use App\Http\Controllers\Plaid\TokenController;

/*
Expand Down Expand Up @@ -49,7 +50,8 @@
});

Route::post('actions/{action}', ActionController::class);
Route::post('plaid/exchange_token', TokenController::class);
Route::post('plaid/create-link-token', CreateLinkTokenController::class);
Route::post('plaid/exchange-token', TokenController::class);
Route::post('cache-clear', App\Http\Controllers\Api\CacheController::class);

Route::apiResource('alerts', App\Http\Controllers\Api\AlertController::class);
Expand Down

0 comments on commit d5fddcf

Please sign in to comment.