@@ -8,18 +8,17 @@ import 'package:http/http.dart';
88import 'package:webview_flutter/webview_flutter.dart' ;
99
1010class FlutterPlaidApi {
11- Configuration _configuration;
12-
1311 FlutterPlaidApi (Configuration configuration) {
14- this . _configuration = configuration;
12+ _configuration = configuration;
1513 }
14+ Configuration _configuration;
1615
1716 /// stripeToken = false use for get plaid token and accountId
1817 /// stripeToken = true: use for add the new payment method, returns stripe_token
1918 launch (BuildContext context, success (Result result),
2019 {bool stripeToken = false }) {
21- _WebViewPage _webViewPage = new _WebViewPage ();
22- _webViewPage._init (this . _configuration, success, stripeToken, context);
20+ final _WebViewPage _webViewPage = _WebViewPage ();
21+ _webViewPage._init (_configuration, success, stripeToken, context);
2322
2423 Navigator .push (context, MaterialPageRoute (builder: (BuildContext context) {
2524 return _webViewPage.build (context);
@@ -36,30 +35,30 @@ class _WebViewPage {
3635
3736 _init (Configuration config, success (Result result), bool stripeToken,
3837 BuildContext context) {
39- this . _success = success;
40- this . _config = config;
41- this . _stripeToken = stripeToken;
42- this . _context = context;
38+ _success = success;
39+ _config = config;
40+ _stripeToken = stripeToken;
41+ _context = context;
4342 _url = config.plaidBaseUrl +
4443 '?key=' +
4544 config.plaidPublicKey +
46- '&isWebview=true' +
47- '&product=auth' +
48- '&isMobile=true' +
49- '&apiVersion=v2' +
50- '&selectAccount=true' +
51- '&webhook=https://requestb.in' +
45+ '&isWebview=' + config.isWebview +
46+ '&product=' + config.products +
47+ '&isMobile=' + config.isMobile +
48+ '&apiVersion=' + config.apiVersion +
49+ '&selectAccount=' + config.selectAccount +
50+ '&webhook=' + config.webhook +
5251 '&env=' +
5352 config.plaidEnvironment;
5453 debugPrint ('init plaid: ' + _url);
5554 }
5655
5756 _parseUrl (String url) {
5857 if (url? .isNotEmpty != null ) {
59- Uri uri = Uri .parse (url);
58+ final Uri uri = Uri .parse (url);
6059 debugPrint ('PLAID uri: ' + uri.toString ());
61- Map <String , String > queryParams = uri.queryParameters;
62- List <String > segments = uri.pathSegments;
60+ final Map <String , String > queryParams = uri.queryParameters;
61+ final List <String > segments = uri.pathSegments;
6362 debugPrint ('queryParams: ' + queryParams? .toString ());
6463 debugPrint ('segments: ' + segments? .toString ());
6564 _processParams (queryParams, url);
@@ -68,52 +67,52 @@ class _WebViewPage {
6867
6968 _processParams (Map <String , String > queryParams, String url) async {
7069 if (queryParams != null ) {
71- String eventName = queryParams['event_name' ] ?? 'unknow' ;
72- debugPrint (" PLAID Event name: " + eventName);
70+ final String eventName = queryParams['event_name' ] ?? 'unknow' ;
71+ debugPrint (' PLAID Event name: $ eventName ' );
7372
7473 if (eventName == 'EXIT' || (url? .contains ('/exit?' ) ?? false )) {
75- this . _closeWebView ();
74+ _closeWebView ();
7675 } else if (eventName == 'HANDOFF' ) {
77- this . _closeWebView ();
76+ _closeWebView ();
7877 }
79- dynamic token = queryParams['public_token' ];
80- dynamic accountId = queryParams['account_id' ];
78+ final dynamic token = queryParams['public_token' ];
79+ final dynamic accountId = queryParams['account_id' ];
8180 if (token != null && accountId != null ) {
8281 if (! _stripeToken) {
8382 this ._success (Result (token, accountId, queryParams));
8483 } else {
85- await this . _fetchStripeToken (token, accountId);
84+ await _fetchStripeToken (token, accountId);
8685 }
8786 }
8887 }
8988 }
9089
9190 _fetchStripeToken (String token, String accountId) async {
92- var headers = {'Content-Type' : 'application/json' };
91+ final headers = {'Content-Type' : 'application/json' };
9392
94- Response responseAccessToken =
93+ final Response responseAccessToken =
9594 await post (_config.environmentPlaidPathAccessToken,
9695 body: json.encode ({
9796 'public_token' : token,
98- 'client_id' : this . _config.plaidClientId,
99- 'secret' : this . _config.secret
97+ 'client_id' : _config.plaidClientId,
98+ 'secret' : _config.secret
10099 }),
101100 headers: headers);
102- var accessTokenData =
101+ final accessTokenData =
103102 json.decode (utf8.decode (responseAccessToken.bodyBytes));
104- String accessToken = accessTokenData['access_token' ];
103+ final String accessToken = accessTokenData['access_token' ];
105104
106- Response responseStripeToken =
105+ final Response responseStripeToken =
107106 await post (_config.environmentPlaidPathStripeToken,
108107 body: json.encode ({
109- 'client_id' : this . _config.plaidClientId,
110- 'secret' : this . _config.secret,
108+ 'client_id' : _config.plaidClientId,
109+ 'secret' : _config.secret,
111110 'access_token' : accessToken,
112111 'account_id' : accountId
113112 }),
114113 headers: headers);
115114
116- var stripeTokenData =
115+ final stripeTokenData =
117116 json.decode (utf8.decode (responseStripeToken.bodyBytes));
118117 _success (Result (
119118 stripeTokenData['stripe_bank_account_token' ], null , stripeTokenData));
@@ -126,12 +125,12 @@ class _WebViewPage {
126125 }
127126
128127 Widget build (BuildContext context) {
129- var webView = new WebView (
128+ final webView = WebView (
130129 initialUrl: _url,
131130 javascriptMode: JavascriptMode .unrestricted,
132131 navigationDelegate: (NavigationRequest navigation) {
133132 if (navigation.url.contains ('plaidlink://' )) {
134- this . _parseUrl (navigation.url);
133+ _parseUrl (navigation.url);
135134 return NavigationDecision .prevent;
136135 }
137136 return NavigationDecision .navigate;
@@ -142,28 +141,41 @@ class _WebViewPage {
142141}
143142
144143class Configuration {
144+ Configuration (
145+ {
146+ @required this .plaidPublicKey,
147+ @required this .plaidBaseUrl,
148+ @required this .plaidEnvironment,
149+ @required this .environmentPlaidPathAccessToken,
150+ @required this .environmentPlaidPathStripeToken,
151+ @required this .plaidClientId,
152+ @required this .secret,
153+ this .webhook = 'https://requestb.in' ,
154+ this .products = 'auth,income' ,//e.g. auth or auth,income
155+ this .selectAccount = 'true' ,//e.g. auth or auth,income
156+ this .isMobile = 'true' ,
157+ this .apiVersion = 'v2' ,
158+ this .isWebview = 'true' ,
159+ });
145160 String plaidPublicKey;
146161 String plaidBaseUrl;
147162 String plaidEnvironment;
148163 String environmentPlaidPathAccessToken;
149164 String environmentPlaidPathStripeToken;
150165 String plaidClientId;
151166 String secret;
152-
153- Configuration (
154- {@required this .plaidPublicKey,
155- @required this .plaidBaseUrl,
156- @required this .plaidEnvironment,
157- @required this .environmentPlaidPathAccessToken,
158- @required this .environmentPlaidPathStripeToken,
159- @required this .plaidClientId,
160- @required this .secret});
167+ String webhook;
168+ String products;
169+ String selectAccount;
170+ String apiVersion;
171+ String isMobile;
172+ String isWebview;
161173}
162174
163175class Result {
176+ Result (this .token, this .accountId, this .response);
177+
164178 String token;
165179 String accountId;
166180 dynamic response;
167-
168- Result (this .token, this .accountId, this .response);
169181}
0 commit comments