forked from plaid/plaid-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
127 lines (107 loc) · 4.33 KB
/
Copy pathmain.go
File metadata and controls
127 lines (107 loc) · 4.33 KB
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
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/plaid/plaid-go/plaid"
)
func handleError(err error) {
if err != nil {
panic(err)
}
}
func main() {
// Creates a new Plaid Client
clientOptions := plaid.ClientOptions{
os.Getenv("PLAID_CLIENT_ID"),
os.Getenv("PLAID_SECRET"),
plaid.Sandbox,
&http.Client{},
}
client, err := plaid.NewClient(clientOptions)
handleError(err)
// POST /institutions/get
instsResp, err := client.GetInstitutions(5, 0, []string{"US"})
handleError(err)
fmt.Println(instsResp.Institutions[0].Name, "has products:", instsResp.Institutions[0].Products)
// POST /institutions/get_by_id
instResp, err := client.GetInstitutionByID(instsResp.Institutions[0].ID, []string{"US"})
handleError(err)
fmt.Println(instResp.Institution.Name, "has MFA:", instResp.Institution.MFA)
// POST /institutions/search
instSearchResp, err := client.SearchInstitutions("Ally", []string{"transactions"}, []string{"US"})
handleError(err)
fmt.Println(instSearchResp.Institutions[0].Name, "has ID:", instSearchResp.Institutions[0].ID)
// POST /categories/get
categoriesResp, err := client.GetCategories()
handleError(err)
fmt.Println("Category group", categoriesResp.Categories[0].Group, "has items:", categoriesResp.Categories[0].Hierarchy)
// POST /sandbox/public_token/create
publicTokenResp, err := client.CreateSandboxPublicToken(instResp.Institution.ID, []string{"auth", "transactions"})
handleError(err)
fmt.Println("Created sandbox public token:", publicTokenResp.PublicToken)
// POST /item/public_token/exchange
accessTokenResp, err := client.ExchangePublicToken(publicTokenResp.PublicToken)
handleError(err)
fmt.Println("Public token -> Access Token", accessTokenResp.AccessToken, "for item:", accessTokenResp.ItemID)
// POST /accounts/balance/get
balanceResp, err := client.GetBalances(accessTokenResp.AccessToken)
handleError(err)
fmt.Println("Account with name", balanceResp.Accounts[0].Name, "has available balance", balanceResp.Accounts[0].Balances.Available)
// POST /accounts/get
accountsResp, err := client.GetAccounts(accessTokenResp.AccessToken)
handleError(err)
fmt.Println("Access token is associated with", len(accountsResp.Accounts), "accounts")
// POST /auth/get
authResp, err := client.GetAuth(accessTokenResp.AccessToken)
handleError(err)
fmt.Println("Account has number:", authResp.Numbers.ACH[0].Account)
// POST /transactions/get
transactionsResp, err := client.GetTransactions(accessTokenResp.AccessToken, "2010-01-01", "2018-01-01")
if plaidErr, ok := err.(plaid.Error); ok {
// Poll until transactions are ready
for ok && plaidErr.ErrorCode == "PRODUCT_NOT_READY" {
time.Sleep(5 * time.Second)
transactionsResp, err = client.GetTransactions(accessTokenResp.AccessToken, "2010-01-01", "2018-01-01")
plaidErr, ok = err.(plaid.Error)
}
handleError(err)
}
fmt.Println("Number of transactions:", len(transactionsResp.Transactions))
params := plaid.OptionalRecipientCreateParams{
BACS: &plaid.PaymentRecipientBacs{
Account: "12345678",
SortCode: "01-02-03",
},
Address: &plaid.PaymentRecipientAddress{
Street: []string{"Street Name 999"},
City: "City",
PostalCode: "99999",
Country: "GB",
},
}
// POST /payment_initiation/recipient/create
paymentRecipientCreateResp, err := client.CreatePaymentRecipient("John Doe", params)
handleError(err)
fmt.Println("Recipient ID:", paymentRecipientCreateResp.RecipientID)
// POST /payment_initiation/recipient/get
paymentRecipientGetResp, err := client.GetPaymentRecipient(paymentRecipientCreateResp.RecipientID)
handleError(err)
fmt.Println("Recipient get response:", paymentRecipientGetResp)
// POST /payment_initiation/payment/create
paymentCreateResp, err := client.CreatePayment(paymentRecipientCreateResp.RecipientID, "TestPayment", plaid.PaymentAmount{
Currency: "GBP",
Value: 100.0,
})
handleError(err)
fmt.Println("Payment ID:", paymentCreateResp.PaymentID)
// POST /payment_initiation/payment/get
paymentGetResp, err := client.GetPayment(paymentCreateResp.PaymentID)
handleError(err)
fmt.Println("Payment get response:", paymentGetResp)
// POST /webhook_verification_key/get
webhookVerificationKeyGetResp, err := client.GetWebhookVerificationKey("6c5516e1-92dc-479e-a8ff-5a51992e0001")
handleError(err)
fmt.Println("Webhook verification key response:", webhookVerificationKeyGetResp)
}