-
Notifications
You must be signed in to change notification settings - Fork 6
/
PlaidLink.vue
142 lines (137 loc) · 3.83 KB
/
PlaidLink.vue
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
<template>
<div @click="link_open" :style="{display: 'inline'}">
<slot></slot>
</div>
</template>
<script>
export default {
props: {
clientName: String,
env: {
type: String,
required: true,
default: 'sandbox',
validator: function (value) {
// The value must match one of these strings
return ['sandbox', 'development', 'production'].indexOf(value) !== -1
}
},
isWebview: {
type: Boolean,
required: false
},
link_token: {
type: String,
required: false
},
public_key: {
type: String,
required: false
},
products: Array,
receivedRedirectUri: {
type: String,
required: false
},
webhook: {
type: String,
required: false
},
onLoad: {
type: Function,
required: false
},
onSuccess: Function,
onExit: {
type: Function,
required: false
},
onEvent: {
type: Function,
required: false
}
},
data() {
return {
plaid: null,
linkHandler: null
}
},
methods: {
link_open(e) {
e.preventDefault()
let self = this
if (this.plaid != null) {
// destroy link handler to prevent stacking of iframes
if (this.linkHandler != null) {
this.linkHandler.destroy();
this.linkHandler = null;
}
this.linkHandler = this.plaid.create({
clientName: this.clientName,
env: this.env,
isWebview: this.isWebview,
key: this.public_key,
product: this.products,
receivedRedirectUri: this.receivedRedirectUri,
token: this.link_token,
// Optional – use webhooks to get transaction and error updates
webhook: this.webhook,
onLoad: function() {
// Optional, called when Link loads
self.onLoad()
},
onSuccess: function(public_token, metadata) {
// Send the public_token to your app server.
// The metadata object contains info about the institution the
// user selected and the account ID or IDs, if the
// Select Account view is enabled.
self.onSuccess(public_token, metadata)
},
onExit: function(err, metadata) {
// The user exited the Link flow.
if (err != null) {
// The user encountered a Plaid API error prior to exiting.
}
// metadata contains information about the institution
// that the user selected and the most recent API request IDs.
// Storing this information can be helpful for support.
self.onExit(err, metadata)
},
onEvent: function(eventName, metadata) {
// Optionally capture Link flow events, streamed through
// this callback as your users connect an Item to Plaid.
// For example:
// eventName = "TRANSITION_VIEW"
// metadata = {
// link_session_id: "123-abc",
// mfa_type: "questions",
// timestamp: "2017-09-14T14:42:19.350Z",
// view_name: "MFA",
// }
self.onEvent(eventName, metadata)
}
});
this.linkHandler.open();
}
}
},
mounted() {
// Only download the script if not already done
if (window.Plaid) {
this.plaid = window.Plaid
return
}
let linkScript = document.createElement('script')
linkScript.async = true
linkScript.setAttribute('src', 'https://cdn.plaid.com/link/v2/stable/link-initialize.js')
document.head.appendChild(linkScript)
linkScript.onload = () => {
this.plaid = window.Plaid
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>