-
-
Notifications
You must be signed in to change notification settings - Fork 541
/
Copy pathideal_screen.dart
95 lines (86 loc) · 2.66 KB
/
ideal_screen.dart
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
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
import 'package:http/http.dart' as http;
import 'package:stripe_example/widgets/example_scaffold.dart';
import 'package:stripe_example/widgets/loading_button.dart';
import '../../config.dart';
class IdealScreen extends StatelessWidget {
const IdealScreen({super.key});
Future<Map<String, dynamic>> _createPaymentIntent() async {
final url = Uri.parse('$kApiUrl/create-payment-intent');
final response = await http.post(
url,
headers: {
'Content-Type': 'application/json',
},
body: json.encode({
'currency': 'eur',
'payment_method_types': ['ideal'],
'amount': 1099
}),
);
return json.decode(response.body);
}
Future<void> _pay(BuildContext context) async {
// Precondition:
//Make sure to have set a custom URI scheme in your app and add it to Stripe SDK
// see file main.dart in this example app.
// 1. on the backend create a payment intent for payment method and save the
// client secret.
final scaffoldMessenger = ScaffoldMessenger.of(context);
final result = await _createPaymentIntent();
final clientSecret = await result['clientSecret'];
// 2. use the client secret to confirm the payment and handle the result.
try {
await Stripe.instance.confirmPayment(
paymentIntentClientSecret: clientSecret,
data: PaymentMethodParams.ideal(
paymentMethodData: PaymentMethodDataIdeal(
bankIdentifierCode: 'INGBNL2A',
bankName: kIsWeb ? 'revolut' : 'ing',
),
),
);
if (context.mounted) {
scaffoldMessenger.showSnackBar(
SnackBar(
content: Text('Payment successfully completed'),
),
);
}
} on Exception catch (e) {
if (e is StripeException && context.mounted) {
scaffoldMessenger.showSnackBar(
SnackBar(
content: Text(
'Error from Stripe: ${e.error.localizedMessage ?? e.error.code}'),
),
);
} else if (context.mounted) {
scaffoldMessenger.showSnackBar(
SnackBar(
content: Text('Unforeseen error: $e'),
),
);
}
}
}
@override
Widget build(BuildContext context) {
return ExampleScaffold(
title: 'Ideal',
tags: ['Payment method'],
padding: EdgeInsets.all(16),
children: [
LoadingButton(
onPressed: () async {
await _pay(context);
},
text: 'Pay',
),
],
);
}
}