forked from plaid/quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
299 lines (249 loc) · 8.05 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/gin-gonic/gin"
"github.com/plaid/plaid-go/plaid"
)
func init() {
if PLAID_PRODUCTS == "" {
PLAID_PRODUCTS = "transactions"
}
if PLAID_COUNTRY_CODES == "" {
PLAID_COUNTRY_CODES = "US,CA,GB,FR,ES,IE,NL"
}
}
// Fill with your Plaid API keys - https://dashboard.plaid.com/account/keys
var (
PLAID_CLIENT_ID = os.Getenv("PLAID_CLIENT_ID")
PLAID_SECRET = os.Getenv("PLAID_SECRET")
PLAID_PUBLIC_KEY = os.Getenv("PLAID_PUBLIC_KEY")
PLAID_PRODUCTS = os.Getenv("PLAID_PRODUCTS")
PLAID_COUNTRY_CODES = os.Getenv("PLAID_COUNTRY_CODES")
// Parameters used for the OAuth redirect Link flow.
//
// Set PLAID_OAUTH_REDIRECT_URI to 'http://localhost:8000/oauth-response.html'
// The OAuth redirect flow requires an endpoint on the developer's website
// that the bank website should redirect to. You will need to whitelist
// this redirect URI for your client ID through the Plaid developer dashboard
// at https://dashboard.plaid.com/team/api.
PLAID_OAUTH_REDIRECT_URI = os.Getenv("PLAID_OAUTH_REDIRECT_URI")
// Set PLAID_OAUTH_NONCE to a unique identifier such as a UUID for each Link
// session. The nonce will be used to re-open Link upon completion of the OAuth
// redirect. The nonce must be at least 16 characters long.
PLAID_OAUTH_NONCE = os.Getenv("PLAID_OAUTH_NONCE")
// Use 'sandbox' to test with fake credentials in Plaid's Sandbox environment
// Use `development` to test with real credentials while developing
// Use `production` to go live with real users
APP_PORT = os.Getenv("APP_PORT")
)
var clientOptions = plaid.ClientOptions{
PLAID_CLIENT_ID,
PLAID_SECRET,
PLAID_PUBLIC_KEY,
plaid.Sandbox, // Available environments are Sandbox, Development, and Production
&http.Client{},
}
var client, err = plaid.NewClient(clientOptions)
// We store the access_token in memory - in production, store it in a secure
// persistent data store.
var accessToken string
var itemID string
// The payment_token is only relevant for the UK Payment Initiation product.
// We store the payment_token in memory - in production, store it in a secure
// persistent data store.
var paymentToken string
var paymentID string
func getAccessToken(c *gin.Context) {
publicToken := c.PostForm("public_token")
response, err := client.ExchangePublicToken(publicToken)
accessToken = response.AccessToken
itemID = response.ItemID
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
fmt.Println("public token: " + publicToken)
fmt.Println("access token: " + accessToken)
fmt.Println("item ID: " + itemID)
c.JSON(http.StatusOK, gin.H{
"access_token": accessToken,
"item_id": itemID,
})
}
// This functionality is only relevant for the UK Payment Initiation product.
// Sets the payment token in memory on the server side. We generate a new
// payment token so that the developer is not required to supply one.
// This makes the quickstart easier to use.
func setPaymentToken(c *gin.Context) {
recipientCreateResp, err := client.CreatePaymentRecipient("Harry Potter", "GB33BUKB20201555555555", plaid.PaymentRecipientAddress{
Street: []string{"4 Privet Drive"},
City: "Little Whinging",
PostalCode: "11111",
Country: "GB",
})
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
recipientID := recipientCreateResp.RecipientID
paymentCreateResp, err := client.CreatePayment(recipientID, "payment-ref", plaid.PaymentAmount{
Currency: "GBP",
Value: 12.34,
})
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
paymentID = paymentCreateResp.PaymentID
paymentTokenCreateResp, err := client.CreatePaymentToken(paymentID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
paymentToken = paymentTokenCreateResp.PaymentToken
fmt.Println("payment token: " + paymentToken)
fmt.Println("payment id: " + paymentID)
c.JSON(http.StatusOK, gin.H{
"payment_token": paymentToken,
})
}
func auth(c *gin.Context) {
response, err := client.GetAuth(accessToken)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"accounts": response.Accounts,
"numbers": response.Numbers,
})
}
func accounts(c *gin.Context) {
response, err := client.GetAccounts(accessToken)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"accounts": response.Accounts,
})
}
func balance(c *gin.Context) {
response, err := client.GetBalances(accessToken)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"accounts": response.Accounts,
})
}
func item(c *gin.Context) {
response, err := client.GetItem(accessToken)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
institution, err := client.GetInstitutionByID(response.Item.InstitutionID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"item": response.Item,
"institution": institution.Institution,
})
}
func identity(c *gin.Context) {
response, err := client.GetIdentity(accessToken)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"identity": response.Accounts,
})
}
func transactions(c *gin.Context) {
// pull transactions for the past 30 days
endDate := time.Now().Local().Format("2006-01-02")
startDate := time.Now().Local().Add(-30 * 24 * time.Hour).Format("2006-01-02")
response, err := client.GetTransactions(accessToken, startDate, endDate)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"accounts": response.Accounts,
"transactions": response.Transactions,
})
}
// This functionality is only relevant for the UK Payment Initiation product.
// Retrieve Payment for a specified Payment ID
func payment(c *gin.Context) {
response, err := client.GetPayment(paymentID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"payment": response.Payment,
})
}
func createPublicToken(c *gin.Context) {
// Create a one-time use public_token for the Item.
// This public_token can be used to initialize Link in update mode for a user
publicToken, err := client.CreatePublicToken(accessToken)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"public_token": publicToken,
})
}
func main() {
if APP_PORT == "" {
APP_PORT = "8000"
}
r := gin.Default()
r.LoadHTMLFiles("templates/index.tmpl", "templates/oauth-response.tmpl")
r.Static("/static", "./static")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"plaid_environment": "sandbox", // Switch this environment
"plaid_public_key": PLAID_PUBLIC_KEY,
"plaid_products": PLAID_PRODUCTS,
"plaid_country_codes": PLAID_COUNTRY_CODES,
"plaid_oauth_redirect_uri": PLAID_OAUTH_REDIRECT_URI,
"plaid_oauth_nonce": PLAID_OAUTH_NONCE,
"item_id": itemID,
"access_token": accessToken,
})
})
r.GET("/oauth-response.html", func(c *gin.Context) {
c.HTML(http.StatusOK, "oauth-response.tmpl", gin.H{
"plaid_environment": "sandbox", // Switch this environment
"plaid_public_key": PLAID_PUBLIC_KEY,
"plaid_products": PLAID_PRODUCTS,
"plaid_country_codes": PLAID_COUNTRY_CODES,
"plaid_oauth_nonce": PLAID_OAUTH_NONCE,
})
})
r.POST("/set_access_token", getAccessToken)
r.POST("/set_payment_token", setPaymentToken)
r.GET("/auth", auth)
r.GET("/accounts", accounts)
r.GET("/balance", balance)
r.GET("/item", item)
r.POST("/item", item)
r.GET("/identity", identity)
r.GET("/transactions", transactions)
r.POST("/transactions", transactions)
r.GET("/payment", payment)
r.GET("/create_public_token", createPublicToken)
r.Run(":" + APP_PORT)
}