|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/gin-gonic/gin" |
| 10 | + "github.com/plaid/plaid-go/plaid" |
| 11 | +) |
| 12 | + |
| 13 | +// Fill with your Plaid API keys - https://dashboard.plaid.com/account/keys |
| 14 | +var ( |
| 15 | + PLAID_CLIENT_ID = os.Getenv("PLAID_CLIENT_ID") |
| 16 | + PLAID_SECRET = os.Getenv("PLAID_SECRET") |
| 17 | + PLAID_PUBLIC_KEY = os.Getenv("PLAID_PUBLIC_KEY") |
| 18 | + // Use 'sandbox' to test with fake credentials in Plaid's Sandbox environment |
| 19 | + // Use `development` to test with real credentials while developing |
| 20 | + // Use `production` to go live with real users |
| 21 | + APP_PORT = os.Getenv("APP_PORT") |
| 22 | +) |
| 23 | + |
| 24 | +var clientOptions = plaid.ClientOptions{ |
| 25 | + PLAID_CLIENT_ID, |
| 26 | + PLAID_SECRET, |
| 27 | + PLAID_PUBLIC_KEY, |
| 28 | + plaid.Sandbox, // Available environments are Sandbox, Development, and Production |
| 29 | + &http.Client{}, |
| 30 | +} |
| 31 | + |
| 32 | +var client, err = plaid.NewClient(clientOptions) |
| 33 | + |
| 34 | +var accessToken string |
| 35 | +var itemID string |
| 36 | + |
| 37 | +func getAccessToken(c *gin.Context) { |
| 38 | + publicToken := c.PostForm("public_token") |
| 39 | + response, err := client.ExchangePublicToken(publicToken) |
| 40 | + accessToken = response.AccessToken |
| 41 | + itemID = response.ItemID |
| 42 | + |
| 43 | + if err != nil { |
| 44 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + fmt.Println("public token: " + publicToken) |
| 49 | + fmt.Println("access token: " + accessToken) |
| 50 | + fmt.Println("item ID: " + itemID) |
| 51 | + |
| 52 | + c.JSON(http.StatusOK, gin.H{ |
| 53 | + "access_token": accessToken, |
| 54 | + "item_id": itemID, |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +func auth(c *gin.Context) { |
| 59 | + response, err := client.GetAuth(accessToken) |
| 60 | + |
| 61 | + if err != nil { |
| 62 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + c.JSON(http.StatusOK, gin.H{ |
| 67 | + "accounts": response.Accounts, |
| 68 | + "numbers": response.Numbers, |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +func accounts(c *gin.Context) { |
| 73 | + response, err := client.GetAccounts(accessToken) |
| 74 | + |
| 75 | + if err != nil { |
| 76 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + c.JSON(http.StatusOK, gin.H{ |
| 81 | + "accounts": response.Accounts, |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +func balance(c *gin.Context) { |
| 86 | + response, err := client.GetBalances(accessToken) |
| 87 | + |
| 88 | + if err != nil { |
| 89 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + c.JSON(http.StatusOK, gin.H{ |
| 94 | + "accounts": response.Accounts, |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +func item(c *gin.Context) { |
| 99 | + response, err := client.GetItem(accessToken) |
| 100 | + |
| 101 | + if err != nil { |
| 102 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + institution, err := client.GetInstitutionByID(response.Item.InstitutionID) |
| 107 | + |
| 108 | + if err != nil { |
| 109 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + c.JSON(http.StatusOK, gin.H{ |
| 114 | + "item": response.Item, |
| 115 | + "institution": institution.Institution, |
| 116 | + }) |
| 117 | +} |
| 118 | + |
| 119 | +func identity(c *gin.Context) { |
| 120 | + response, err := client.GetIdentity(accessToken) |
| 121 | + |
| 122 | + if err != nil { |
| 123 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + c.JSON(http.StatusOK, gin.H{ |
| 128 | + "identity": response.Accounts, |
| 129 | + }) |
| 130 | +} |
| 131 | + |
| 132 | +func transactions(c *gin.Context) { |
| 133 | + // pull transactions for the past 30 days |
| 134 | + endDate := time.Now().Local().Format("2006-01-02") |
| 135 | + startDate := time.Now().Local().Add(-30 * 24 * time.Hour).Format("2006-01-02") |
| 136 | + |
| 137 | + response, err := client.GetTransactions(accessToken, startDate, endDate) |
| 138 | + |
| 139 | + if err != nil { |
| 140 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 141 | + return |
| 142 | + } |
| 143 | + |
| 144 | + c.JSON(http.StatusOK, gin.H{ |
| 145 | + "accounts": response.Accounts, |
| 146 | + "transactions": response.Transactions, |
| 147 | + }) |
| 148 | +} |
| 149 | + |
| 150 | +func createPublicToken(c *gin.Context) { |
| 151 | + // Create a one-time use public_token for the Item. |
| 152 | + // This public_token can be used to initialize Link in update mode for a user |
| 153 | + publicToken, err := client.CreatePublicToken(accessToken) |
| 154 | + if err != nil { |
| 155 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + c.JSON(http.StatusOK, gin.H{ |
| 160 | + "public_token": publicToken, |
| 161 | + }) |
| 162 | +} |
| 163 | + |
| 164 | +func main() { |
| 165 | + if APP_PORT == "" { |
| 166 | + APP_PORT = "8000" |
| 167 | + } |
| 168 | + |
| 169 | + r := gin.Default() |
| 170 | + r.LoadHTMLFiles("templates/index.tmpl") |
| 171 | + r.Static("/static", "./static") |
| 172 | + |
| 173 | + r.GET("/", func(c *gin.Context) { |
| 174 | + c.HTML(http.StatusOK, "index.tmpl", gin.H{ |
| 175 | + "plaid_environment": "sandbox", // Switch this environment |
| 176 | + "plaid_public_key": PLAID_PUBLIC_KEY, |
| 177 | + }) |
| 178 | + }) |
| 179 | + |
| 180 | + r.POST("/set_access_token", getAccessToken) |
| 181 | + r.GET("/auth", auth) |
| 182 | + r.GET("/accounts", accounts) |
| 183 | + r.GET("/balance", balance) |
| 184 | + r.GET("/item", item) |
| 185 | + r.POST("/item", item) |
| 186 | + r.GET("/identity", identity) |
| 187 | + r.GET("/transactions", transactions) |
| 188 | + r.POST("/transactions", transactions) |
| 189 | + r.GET("/create_public_token", createPublicToken) |
| 190 | + |
| 191 | + r.Run(":" + APP_PORT) |
| 192 | +} |
0 commit comments