Integrate Plaid with PHP

Plaid provides a simple method for IBV, or Instant Bank Verification. By integrating Plaid into your app or website, your customers can use their online banking usernames and passwords to give you secure, tokenized access to their bank account. With that access, you can verify their identity, check their balance, or take a look at their transactions, all without storing their sensitive account details on your server.

Plaid + PHP

When starting out with Plaid, I found it difficult to find good references for using it with PHP. The recommended libraries offered were for Ruby, Node JS, and other platforms requiring changing my server configuration and downloading 2.4 bazillion packages. Even the Community, but not “approved,” PHP libraries on Plaid’s site were rife with dependencies. Call me old fashioned, but I enjoy the simplicity of the Apache/PHP setup for creating and running websites, so I set to getting Plaid working in my environment.

The steps to using Plaid’s API are:

  1. Setup your Endpoints and Keys
  2. Create an Item
  3. Process your Item
  4. Accept webhooks
  5. Use your data
Setup your Endpoints and Keys

You will need to create a Plaid account, and in their (very sparse) dashboard, you will find your keys.

Plaid's minimalist dashboard
Plaid’s minimalist dashboard

One cool thing about Plaid’s environments is that you never need to change your keys–just your endpoints. So, along with those keys in your config file, you will need to include the correct environment to use. Since we’ll be testing here, we’ll use the sandbox endpoint.

Create an Item

In Plaid, an Item is the Object that contains everything about the accounts for one customer at one financial institution. If you have a customer that uses your Plaid integration, an Item will be everything about that customer’s banking data that you have access to, including transactions.

When using the Sandbox endpoint, Plaid displays the test credentials that give you a positive result (see the image below, at the bottom of each screen). This is especially helpful if you have other folks testing your implementation. For other testing scenarios, they have a way to generate any error you’re looking for in the docs.

Plaid's convenient drop-in module
Plaid’s convenient drop-in module

Plaid uses a bit of Javascript to serve their front-end module to your customers. This has some benefits and detriments:

Pros
You don’t have to code every bank logo and name into the interface.
It looks pretty slick, and each major bank’s branding is there, which should help your customers feel more comfortable divulging their credentials.
You can customize the text on the front end (N.B. This is new as of July 11, 2017).
Cons
The customizable text is your-account-wide, meaning you can’t have two versions on your site.
It may not match the look & feel of the page you want to use it on.
If a customer cannot find their bank, you have to rely on your language on the interface to get them back to the page you control. There’s no error from Plaid on this event, so knowing just how many of your customers do not find their bank is impossible, and there’s no way to direct them right away to using another method of bank verification, like providing their Bank Account ID and Routing number.

To implement on your page, first include the library between the HEAD tags:

Also in the HEAD section, use Javascript to trigger the Plaid module. I’m using jQuery here.

Near the end of your page, include the integration Javascript for initiating the Plaid module. Here, you can identify some settings for the interface, such as what aspects of the Item you want access to (Auth, Identity, etc.) and limiting choices to specific banks. Be sure to set selectAccount: true if you will be using the Plaid token with Stripe or other payment gateway.

Finally, add the button link that you want a customer to click to open the Plaid module.

To test, open the page and click the button. You should see the Plaid module take over the page and ask you to select your bank. If not, check your console (in Chrome, F12) for error messages from Plaid.

Process Your Item

Using an AJAX post, use the data returned to POST to a page on your server finalize the Plaid authentication.

On the server side, in this case in process_plaid_token.php, you need to save the Access Token and Item ID in your DB. The Access Token is for accessing Transactions and other Item data, and the Item ID will be used to identify the Item to which a Webhook refers. Without a saved Item ID, your webhooks are SOL, so don’t skip this!

To get these important pieces of information, you need to exchange your public token posted from Javascript (which will expire in 30 minutes) for an access token (which will not expire).

Another helpful piece of info to associate with your customer is their chosen bank account, if you have them select one. You will have access to ALL bank accounts on their Item, but if your terms of service are specific, you should limit your data to that one account.

If you are integrating with another service, like Stripe or Dwolla, this a good time to do it, since you need to use the Plaid public token before it expires.

If everything went as planned, our data (simply printing to our error log in this example) looks like this:

Accept Webhooks

You may be inclined to skip this part “for later,” but I recommend you do not. Plaid really expects you to use Webhooks to keep your data updated, and pulling data from them can be slow at times due to the many bank systems they need to connect with. Relying on just-in-time content from them is not sustainable at testing levels, let alone at scale. You should plan to use their webhooks to let you know when to update, and you will have a faster, but still up-to-date system.

The URL of your webhook should have been included in your Javascript when you initiated an Item. When you create that file to accept the webhooks, you can consume the webhook data like so:

You can see all the possible webhook values at Plaid.com.

Use the Data

Now that you have a test Item and its matching access token, you can use the Plaid data. Use your webhooks to tell you when to make these pulls using cURL.

Balance

Visit Plaid.com to see the full data the call below can return.

Access any of the data below using the same format for your cURL calls, but change the endpoint URL to Plaid.

Identity

Get contact info for the bank account holder, which is helpful for verifying a customer is who they say they are. Identity Documentation

Transactions

Retrieve individual transactions for each account, for all account types and transaction types. Transaction Documentation

Income

If your application requires only income information, use this endpoint to get you the results of Plaid’s calculations. Income Documentation

Next Steps

Changing Endpoints

When you’re ready to move onto the Development endpoint, which allows you to use REAL bank credentials and, basically, do everything the Production endpoint can do except for free, you will need to contact Plaid Sales. I found their disclosure of this information lacking, and was very frustrated until I found it noted on a message board.

In Development, you can see real data for customers, or just for testing, for 100 Items. You will need a contract agreeing to payment terms before you can go to Production, so once you have the Sandbox testing done, it makes sense to get the negotiations going and make sure the pricing is worth it for your application.

Further Integration, Error Checking, Etc.

Inevitably, your customers will change their passwords for their banks. You will need to give them a way to easily reauthorize your application, and Plaid has a way to do that. They also have ways to use their banking tokens for connections to payment gateways like Stripe and Dwolla. I recommend heading over to Plaid’s Documentation to continue reading about everything you can do with their developer-friendly banking product.

Full Plaid Link Code

The PHP page that calls Plaid’s module–be sure to insert your own Plaid keys.

Server-side token processing page.