2017-01-06 19:06:06 +00:00
|
|
|
/*
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
Copyright 2017 The Perkeep Authors
|
2017-01-06 19:06:06 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package plaid implements an importer for financial transactions from plaid.com
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
package plaid // import "perkeep.org/pkg/importer/plaid"
|
2017-01-06 19:06:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2018-01-03 05:03:30 +00:00
|
|
|
"perkeep.org/internal/httputil"
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
"perkeep.org/pkg/blob"
|
|
|
|
"perkeep.org/pkg/importer"
|
|
|
|
"perkeep.org/pkg/schema"
|
|
|
|
"perkeep.org/pkg/schema/nodeattr"
|
2017-01-06 19:06:06 +00:00
|
|
|
|
|
|
|
"github.com/plaid/plaid-go/plaid"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
importer.Register("plaid", &imp{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type imp struct{}
|
|
|
|
|
2018-04-22 17:41:45 +00:00
|
|
|
func (*imp) Properties() importer.Properties {
|
|
|
|
return importer.Properties{
|
2018-04-28 00:06:59 +00:00
|
|
|
Title: "Plaid",
|
|
|
|
Description: "import your financial transactions from plaid.com",
|
2018-04-22 17:41:45 +00:00
|
|
|
SupportsIncremental: true,
|
|
|
|
NeedsAPIKey: true,
|
|
|
|
}
|
2017-01-06 19:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
acctAttrToken = "plaidAccountToken"
|
|
|
|
acctAttrUsername = "username"
|
|
|
|
acctInstitution = "institutionType"
|
|
|
|
|
|
|
|
plaidTransactionTimeFormat = "2006-01-02"
|
|
|
|
plaidTransactionNodeType = "plaid.io:transaction"
|
|
|
|
plaidLastTransaction = "lastTransactionSyncDate"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (*imp) IsAccountReady(acct *importer.Object) (ready bool, err error) {
|
|
|
|
return acct.Attr(acctAttrToken) != "" && acct.Attr(acctAttrUsername) != "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*imp) SummarizeAccount(acct *importer.Object) string {
|
|
|
|
return fmt.Sprintf("%s (%s)", acct.Attr(acctAttrUsername), acct.Attr(acctInstitution))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*imp) ServeSetup(w http.ResponseWriter, r *http.Request, ctx *importer.SetupContext) error {
|
|
|
|
args := struct {
|
|
|
|
Ctx *importer.SetupContext
|
|
|
|
Inst InstitutionNameMap
|
|
|
|
}{
|
|
|
|
ctx,
|
|
|
|
supportedInstitutions,
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmpl.ExecuteTemplate(w, "serveSetup", args)
|
|
|
|
}
|
|
|
|
|
|
|
|
var tmpl = template.Must(template.New("root").Parse(`
|
|
|
|
{{define "serveSetup"}}
|
|
|
|
<h1>Configuring Bank Account</h1>
|
|
|
|
<p>Enter your username/password credentials for your bank/card account and select the institution type.
|
|
|
|
<form method="get" action="{{.Ctx.CallbackURL}}">
|
|
|
|
<input type="hidden" name="acct" value="{{.Ctx.AccountNode.PermanodeRef}}">
|
|
|
|
<table border=0 cellpadding=3>
|
|
|
|
<tr><td align=right>Username</td><td><input name="username" size=50></td></tr>
|
|
|
|
<tr><td align=right>Password</td><td><input name="password" size=50 type="password"></td></tr>
|
|
|
|
<tr><td>Institution</td><td><select name="institution">
|
|
|
|
{{range .Inst}}
|
|
|
|
<option value="{{.CodeName}}">{{.DisplayName}}</option>
|
|
|
|
{{end}}
|
|
|
|
</select></td></tr>
|
|
|
|
<tr><td align=right></td><td align=right><input type="submit" value="Add"></td></tr>
|
|
|
|
</table>
|
|
|
|
</form>
|
|
|
|
{{end}}
|
|
|
|
`))
|
|
|
|
|
|
|
|
var _ importer.ImporterSetupHTMLer = (*imp)(nil)
|
|
|
|
|
|
|
|
func (im *imp) AccountSetupHTML(host *importer.Host) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
<h1>Configuring Plaid</h1>
|
|
|
|
<p>Signup for a developer account on <a href='https://dashboard.plaid.com/signup'>Plaid dashboard</a>
|
|
|
|
<p>After following signup steps and verifying your email, get your developer credentials
|
|
|
|
(under "Send your first request"), and copy your client ID and secret above.
|
|
|
|
<p>
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *imp) ServeCallback(w http.ResponseWriter, r *http.Request, ctx *importer.SetupContext) {
|
|
|
|
username := r.FormValue("username")
|
|
|
|
password := r.FormValue("password")
|
|
|
|
if username == "" || password == "" {
|
|
|
|
http.Error(w, "Username and password are both required", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
institution := r.FormValue("institution")
|
|
|
|
|
|
|
|
clientID, secret, err := ctx.Credentials()
|
|
|
|
if err != nil {
|
|
|
|
httputil.ServeError(w, r, fmt.Errorf("Credentials error: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client := plaid.NewClient(clientID, secret, plaid.Tartan)
|
|
|
|
res, _, err := client.ConnectAddUser(username, password, "", institution, nil)
|
|
|
|
if err != nil {
|
|
|
|
httputil.ServeError(w, r, fmt.Errorf("ConnectAddUser error: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ctx.AccountNode.SetAttrs(
|
|
|
|
"title", fmt.Sprintf("%s account: %s", institution, username),
|
|
|
|
acctAttrUsername, username,
|
|
|
|
acctAttrToken, res.AccessToken,
|
|
|
|
acctInstitution, institution,
|
|
|
|
); err != nil {
|
|
|
|
httputil.ServeError(w, r, fmt.Errorf("Error setting attributes: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, ctx.AccountURL(), http.StatusFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *imp) Run(ctx *importer.RunContext) (err error) {
|
|
|
|
log.Printf("Running plaid importer.")
|
|
|
|
defer func() {
|
|
|
|
log.Printf("plaid importer returned: %v", err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
clientID, secret, err := ctx.Credentials()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var opt plaid.ConnectGetOptions
|
|
|
|
if start := ctx.AccountNode().Attr(plaidLastTransaction); start != "" {
|
|
|
|
opt.GTE = start
|
|
|
|
}
|
|
|
|
|
|
|
|
client := plaid.NewClient(clientID, secret, plaid.Tartan)
|
|
|
|
resp, _, err := client.ConnectGet(ctx.AccountNode().Attr(acctAttrToken), &opt)
|
|
|
|
if err != nil {
|
2017-12-10 09:13:00 +00:00
|
|
|
fmt.Errorf("connectGet: %s", err)
|
2017-01-06 19:06:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var latestTrans string
|
|
|
|
for _, t := range resp.Transactions {
|
|
|
|
tdate, err := im.importTransaction(ctx, &t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if tdate > latestTrans {
|
|
|
|
latestTrans = tdate
|
|
|
|
ctx.AccountNode().SetAttr(plaidLastTransaction, latestTrans)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *imp) importTransaction(ctx *importer.RunContext, t *plaid.Transaction) (string, error) {
|
|
|
|
itemNode, err := ctx.RootNode().ChildPathObject(t.ID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
transJSON, err := json.Marshal(t)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-01-16 23:03:16 +00:00
|
|
|
fileRef, err := schema.WriteFileFromReader(ctx.Context(), ctx.Host.Target(), "", bytes.NewBuffer(transJSON))
|
2017-01-06 19:06:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
transactionTime, err := time.Parse(plaidTransactionTimeFormat, t.Date)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := itemNode.SetAttrs(
|
|
|
|
nodeattr.Type, plaidTransactionNodeType,
|
|
|
|
nodeattr.DateCreated, schema.RFC3339FromTime(transactionTime),
|
|
|
|
"transactionId", t.ID,
|
|
|
|
"vendor", t.Name,
|
|
|
|
"amount", fmt.Sprintf("%f", t.Amount),
|
|
|
|
"currency", "USD",
|
|
|
|
"categoryId", t.CategoryID,
|
|
|
|
nodeattr.Title, t.Name,
|
|
|
|
nodeattr.CamliContent, fileRef.String(),
|
|
|
|
); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the transaction includes location information (rare), use the supplied
|
|
|
|
// lat/long. Partial address data (eg, the US state) without corresponding lat/long
|
|
|
|
// is also sometimes returned; no attempt is made to geocode that info currently.
|
|
|
|
if t.Meta.Location.Coordinates.Lat != 0 && t.Meta.Location.Coordinates.Lon != 0 {
|
|
|
|
if err := itemNode.SetAttrs(
|
|
|
|
"latitude", fmt.Sprintf("%f", t.Meta.Location.Coordinates.Lat),
|
|
|
|
"longitude", fmt.Sprintf("%f", t.Meta.Location.Coordinates.Lon),
|
|
|
|
); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.Date, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *imp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
httputil.BadRequestError(w, "Unexpected path: %s", r.URL.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *imp) CallbackRequestAccount(r *http.Request) (blob.Ref, error) {
|
|
|
|
return importer.OAuth1{}.CallbackRequestAccount(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (im *imp) CallbackURLParameters(acctRef blob.Ref) url.Values {
|
|
|
|
return importer.OAuth1{}.CallbackURLParameters(acctRef)
|
|
|
|
}
|