|
| 1 | +library flutter_plaid; |
| 2 | + |
| 3 | +import 'dart:convert'; |
| 4 | + |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | +import 'package:flutter/widgets.dart'; |
| 7 | +import 'package:http/http.dart'; |
| 8 | +import 'package:webview_flutter/webview_flutter.dart'; |
| 9 | + |
| 10 | +class FlutterPlaidApi { |
| 11 | + Configuration _configuration; |
| 12 | + |
| 13 | + FlutterPlaidApi(Configuration configuration) { |
| 14 | + this._configuration = configuration; |
| 15 | + } |
| 16 | + |
| 17 | + /// stripeToken = false use for get plaid token and accountId |
| 18 | + /// stripeToken = true: use for add the new payment method, returns stripe_token |
| 19 | + launch(BuildContext context, success(Result result), |
| 20 | + {bool stripeToken = false}) { |
| 21 | + _WebViewPage _webViewPage = new _WebViewPage(); |
| 22 | + _webViewPage._init(this._configuration, success, stripeToken, context); |
| 23 | + |
| 24 | + Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) { |
| 25 | + return _webViewPage.build(context); |
| 26 | + })); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +class _WebViewPage { |
| 31 | + String _url; |
| 32 | + Function(Result result) _success; |
| 33 | + Configuration _config; |
| 34 | + bool _stripeToken; |
| 35 | + BuildContext _context; |
| 36 | + |
| 37 | + _init(Configuration config, success(Result result), bool stripeToken, |
| 38 | + BuildContext context) { |
| 39 | + this._success = success; |
| 40 | + this._config = config; |
| 41 | + this._stripeToken = stripeToken; |
| 42 | + this._context = context; |
| 43 | + _url = config.plaidBaseUrl + |
| 44 | + '?key=' + |
| 45 | + config.plaidPublicKey + |
| 46 | + '&isWebview=true' + |
| 47 | + '&product=auth' + |
| 48 | + '&isMobile=true' + |
| 49 | + '&apiVersion=v2' + |
| 50 | + '&selectAccount=true' + |
| 51 | + '&webhook=https://requestb.in' + |
| 52 | + '&env=' + |
| 53 | + config.plaidEnvironment; |
| 54 | + debugPrint('init plaid: ' + _url); |
| 55 | + } |
| 56 | + |
| 57 | + _parseUrl(String url) { |
| 58 | + if (url?.isNotEmpty != null) { |
| 59 | + Uri uri = Uri.parse(url); |
| 60 | + debugPrint('PLAID uri: ' + uri.toString()); |
| 61 | + Map<String, String> queryParams = uri.queryParameters; |
| 62 | + List<String> segments = uri.pathSegments; |
| 63 | + debugPrint('queryParams: ' + queryParams?.toString()); |
| 64 | + debugPrint('segments: ' + segments?.toString()); |
| 65 | + _processParams(queryParams, url); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + _processParams(Map<String, String> queryParams, String url) async { |
| 70 | + if (queryParams != null) { |
| 71 | + String eventName = queryParams['event_name'] ?? 'unknow'; |
| 72 | + debugPrint("PLAID Event name: " + eventName); |
| 73 | + |
| 74 | + if (eventName == 'EXIT' || (url?.contains('/exit?') ?? false)) { |
| 75 | + this._closeWebView(); |
| 76 | + } else if (eventName == 'HANDOFF') { |
| 77 | + this._closeWebView(); |
| 78 | + } |
| 79 | + dynamic token = queryParams['public_token']; |
| 80 | + dynamic accountId = queryParams['account_id']; |
| 81 | + if (token != null && accountId != null) { |
| 82 | + if (!_stripeToken) { |
| 83 | + this._success(Result(token, accountId, queryParams)); |
| 84 | + } else { |
| 85 | + await this._fetchStripeToken(token, accountId); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + _fetchStripeToken(String token, String accountId) async { |
| 92 | + var headers = {'Content-Type': 'application/json'}; |
| 93 | + |
| 94 | + Response responseAccessToken = |
| 95 | + await post(_config.environmentPlaidPathAccessToken, |
| 96 | + body: json.encode({ |
| 97 | + 'public_token': token, |
| 98 | + 'client_id': this._config.plaidClientId, |
| 99 | + 'secret': this._config.secret |
| 100 | + }), |
| 101 | + headers: headers); |
| 102 | + var accessTokenData = |
| 103 | + json.decode(utf8.decode(responseAccessToken.bodyBytes)); |
| 104 | + String accessToken = accessTokenData['access_token']; |
| 105 | + |
| 106 | + Response responseStripeToken = |
| 107 | + await post(_config.environmentPlaidPathStripeToken, |
| 108 | + body: json.encode({ |
| 109 | + 'client_id': this._config.plaidClientId, |
| 110 | + 'secret': this._config.secret, |
| 111 | + 'access_token': accessToken, |
| 112 | + 'account_id': accountId |
| 113 | + }), |
| 114 | + headers: headers); |
| 115 | + |
| 116 | + var stripeTokenData = |
| 117 | + json.decode(utf8.decode(responseStripeToken.bodyBytes)); |
| 118 | + _success(Result( |
| 119 | + stripeTokenData['stripe_bank_account_token'], null, stripeTokenData)); |
| 120 | + } |
| 121 | + |
| 122 | + _closeWebView() { |
| 123 | + if (_context != null && Navigator.canPop(_context)) { |
| 124 | + Navigator.pop(_context); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + Widget build(BuildContext context) { |
| 129 | + var webView = new WebView( |
| 130 | + initialUrl: _url, |
| 131 | + javascriptMode: JavascriptMode.unrestricted, |
| 132 | + navigationDelegate: (NavigationRequest navigation) { |
| 133 | + if (navigation.url.contains('plaidlink://')) { |
| 134 | + this._parseUrl(navigation.url); |
| 135 | + return NavigationDecision.prevent; |
| 136 | + } |
| 137 | + return NavigationDecision.navigate; |
| 138 | + }, |
| 139 | + ); |
| 140 | + return Scaffold(body: webView); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +class Configuration { |
| 145 | + String plaidPublicKey; |
| 146 | + String plaidBaseUrl; |
| 147 | + String plaidEnvironment; |
| 148 | + String environmentPlaidPathAccessToken; |
| 149 | + String environmentPlaidPathStripeToken; |
| 150 | + String plaidClientId; |
| 151 | + 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}); |
| 161 | +} |
| 162 | + |
| 163 | +class Result { |
| 164 | + String token; |
| 165 | + String accountId; |
| 166 | + dynamic response; |
| 167 | + |
| 168 | + Result(this.token, this.accountId, this.response); |
| 169 | +} |
0 commit comments