1
1
import 'dart:convert' ;
2
2
3
3
import 'package:flutter/material.dart' ;
4
- import 'package:http/http.dart' as http;
5
4
import 'package:flutter_stripe/flutter_stripe.dart' ;
5
+ import 'package:http/http.dart' as http;
6
6
import 'package:stripe_example/widgets/loading_button.dart' ;
7
7
import 'package:stripe_platform_interface/stripe_platform_interface.dart' ;
8
8
@@ -15,6 +15,13 @@ class NoWebhookPaymentScreen extends StatefulWidget {
15
15
16
16
class _NoWebhookPaymentScreenState extends State <NoWebhookPaymentScreen > {
17
17
CardFieldInputDetails ? _card;
18
+ final _editController = CardEditController ();
19
+
20
+ @override
21
+ void dispose () {
22
+ _editController.dispose ();
23
+ super .dispose ();
24
+ }
18
25
19
26
@override
20
27
Widget build (BuildContext context) {
@@ -25,6 +32,7 @@ class _NoWebhookPaymentScreenState extends State<NoWebhookPaymentScreen> {
25
32
Padding (
26
33
padding: EdgeInsets .all (16 ),
27
34
child: CardField (
35
+ controller: _editController,
28
36
onCardChanged: (card) {
29
37
setState (() {
30
38
_card = card;
@@ -39,6 +47,32 @@ class _NoWebhookPaymentScreenState extends State<NoWebhookPaymentScreen> {
39
47
text: 'Pay' ,
40
48
),
41
49
),
50
+ Divider (
51
+ thickness: 2 ,
52
+ ),
53
+ Row (
54
+ mainAxisAlignment: MainAxisAlignment .center,
55
+ children: [
56
+ Padding (
57
+ padding: EdgeInsets .symmetric (horizontal: 16 , vertical: 16 ),
58
+ child: ElevatedButton (
59
+ onPressed: () => _editController.blur (),
60
+ child: Text ('Blur' ),
61
+ ),
62
+ ),
63
+ ElevatedButton (
64
+ onPressed: () => _editController.clear (),
65
+ child: Text ('Clear' ),
66
+ ),
67
+ Padding (
68
+ padding: const EdgeInsets .only (left: 16.0 ),
69
+ child: ElevatedButton (
70
+ onPressed: () => _editController.focus (),
71
+ child: Text ('Focus' ),
72
+ ),
73
+ ),
74
+ ],
75
+ ),
42
76
],
43
77
),
44
78
);
@@ -50,82 +84,81 @@ class _NoWebhookPaymentScreenState extends State<NoWebhookPaymentScreen> {
50
84
}
51
85
52
86
try {
87
+ // 1. Gather customer billing information (ex. email)
88
+
89
+ final billingDetails = BillingDetails (
90
+
91
+ phone: '+48888000888' ,
92
+ address: Address (
93
+ city: 'Houston' ,
94
+ country: 'US' ,
95
+ line1: '1459 Circle Drive' ,
96
+ line2: '' ,
97
+ state: 'Texas' ,
98
+ postalCode: '77063' ,
99
+ ),
100
+ ); // mocked data for tests
101
+
102
+ // 2. Create payment method
103
+ final paymentMethod =
104
+ await Stripe .instance.createPaymentMethod (PaymentMethodParams .card (
105
+ billingDetails: billingDetails,
106
+ ));
107
+
108
+ // 3. call API to create PaymentIntent
109
+ final paymentIntentResult = await callNoWebhookPayEndpointMethodId (
110
+ useStripeSdk: true ,
111
+ paymentMethodId: paymentMethod.id,
112
+ currency: 'usd' , // mocked data
113
+ items: [
114
+ {'id' : 'id' }
115
+ ],
116
+ );
53
117
54
- // 1. Gather customer billing information (ex. email)
55
-
56
- final billingDetails = BillingDetails (
57
-
58
- phone: '+48888000888' ,
59
- address: Address (
60
- city: 'Houston' ,
61
- country: 'US' ,
62
- line1: '1459 Circle Drive' ,
63
- line2: '' ,
64
- state: 'Texas' ,
65
- postalCode: '77063' ,
66
- ),
67
- ); // mocked data for tests
68
-
69
- // 2. Create payment method
70
- final paymentMethod =
71
- await Stripe .instance.createPaymentMethod (PaymentMethodParams .card (
72
- billingDetails: billingDetails,
73
- ));
74
-
75
- // 3. call API to create PaymentIntent
76
- final paymentIntentResult = await callNoWebhookPayEndpointMethodId (
77
- useStripeSdk: true ,
78
- paymentMethodId: paymentMethod.id,
79
- currency: 'usd' , // mocked data
80
- items: [
81
- {'id' : 'id' }
82
- ],
83
- );
84
-
85
- if (paymentIntentResult['error' ] != null ) {
86
- // Error during creating or confirming Intent
87
- ScaffoldMessenger .of (context).showSnackBar (
88
- SnackBar (content: Text ('Error: ${paymentIntentResult ['error' ]}' )));
89
- return ;
90
- }
118
+ if (paymentIntentResult['error' ] != null ) {
119
+ // Error during creating or confirming Intent
120
+ ScaffoldMessenger .of (context).showSnackBar (
121
+ SnackBar (content: Text ('Error: ${paymentIntentResult ['error' ]}' )));
122
+ return ;
123
+ }
91
124
92
- if (paymentIntentResult['clientSecret' ] != null &&
93
- paymentIntentResult['requiresAction' ] == null ) {
94
- // Payment succedeed
125
+ if (paymentIntentResult['clientSecret' ] != null &&
126
+ paymentIntentResult['requiresAction' ] == null ) {
127
+ // Payment succedeed
95
128
96
- ScaffoldMessenger .of (context).showSnackBar (SnackBar (
97
- content: Text ('Success!: The payment was confirmed successfully!' )));
98
- return ;
99
- }
129
+ ScaffoldMessenger .of (context).showSnackBar (SnackBar (
130
+ content:
131
+ Text ('Success!: The payment was confirmed successfully!' )));
132
+ return ;
133
+ }
100
134
101
- if (paymentIntentResult['clientSecret' ] != null &&
102
- paymentIntentResult['requiresAction' ] == true ) {
103
- // 4. if payment requires action calling handleCardAction
104
- final paymentIntent = await Stripe .instance
105
- .handleCardAction (paymentIntentResult['clientSecret' ]);
135
+ if (paymentIntentResult['clientSecret' ] != null &&
136
+ paymentIntentResult['requiresAction' ] == true ) {
137
+ // 4. if payment requires action calling handleCardAction
138
+ final paymentIntent = await Stripe .instance
139
+ .handleCardAction (paymentIntentResult['clientSecret' ]);
106
140
107
- // todo handle error
108
- /*if (cardActionError) {
141
+ // todo handle error
142
+ /*if (cardActionError) {
109
143
Alert.alert(
110
144
`Error code: ${cardActionError.code}`,
111
145
cardActionError.message
112
146
);
113
147
} else*/
114
148
115
- if (paymentIntent.status == PaymentIntentsStatus .RequiresConfirmation ) {
116
- // 5. Call API to confirm intent
117
- await confirmIntent (paymentIntent.id);
118
- } else {
119
- // Payment succedeed
120
- ScaffoldMessenger .of (context).showSnackBar (
121
- SnackBar (content: Text ('Error: ${paymentIntentResult ['error' ]}' )));
149
+ if (paymentIntent.status == PaymentIntentsStatus .RequiresConfirmation ) {
150
+ // 5. Call API to confirm intent
151
+ await confirmIntent (paymentIntent.id);
152
+ } else {
153
+ // Payment succedeed
154
+ ScaffoldMessenger .of (context).showSnackBar (SnackBar (
155
+ content: Text ('Error: ${paymentIntentResult ['error' ]}' )));
156
+ }
122
157
}
123
- }
124
-
125
158
} catch (e) {
126
- ScaffoldMessenger .of (context). showSnackBar (
127
- SnackBar (content: Text ('Error: $e ' )));
128
- rethrow ;
159
+ ScaffoldMessenger .of (context)
160
+ . showSnackBar ( SnackBar (content: Text ('Error: $e ' )));
161
+ rethrow ;
129
162
}
130
163
}
131
164
0 commit comments