|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_stripe/flutter_stripe.dart'; |
| 5 | +import 'package:http/http.dart' as http; |
| 6 | +import 'package:stripe_example/widgets/loading_button.dart'; |
| 7 | +import 'package:stripe_platform_interface/stripe_platform_interface.dart'; |
| 8 | + |
| 9 | +import '../config.dart'; |
| 10 | + |
| 11 | +class CustomCardPaymentScreen extends StatefulWidget { |
| 12 | + @override |
| 13 | + _CustomCardPaymentScreenState createState() => |
| 14 | + _CustomCardPaymentScreenState(); |
| 15 | +} |
| 16 | + |
| 17 | +class _CustomCardPaymentScreenState extends State<CustomCardPaymentScreen> { |
| 18 | + CardDetails _card = CardDetails(); |
| 19 | + String _email = ''; |
| 20 | + bool? _saveCard = false; |
| 21 | + |
| 22 | + @override |
| 23 | + Widget build(BuildContext context) { |
| 24 | + return Scaffold( |
| 25 | + appBar: AppBar(), |
| 26 | + body: Column( |
| 27 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 28 | + children: [ |
| 29 | + Padding( |
| 30 | + padding: EdgeInsets.all(16), |
| 31 | + child: Text( |
| 32 | + 'If you don\'t want to or can\'t rely on the CardField you' |
| 33 | + ' can use the dangerouslyUpdateCardDetails in combination with ' |
| 34 | + 'your own card field implementation. ' |
| 35 | + 'Please beware that this will potentially break PCI compliance: ' |
| 36 | + 'https://stripe.com/docs/security/guide#validating-pci-compliance')), |
| 37 | + Padding( |
| 38 | + padding: EdgeInsets.all(16), |
| 39 | + child: TextField( |
| 40 | + decoration: InputDecoration(hintText: 'Email'), |
| 41 | + onChanged: (value) { |
| 42 | + setState(() { |
| 43 | + _email = value; |
| 44 | + }); |
| 45 | + }, |
| 46 | + ), |
| 47 | + ), |
| 48 | + Padding( |
| 49 | + padding: EdgeInsets.all(16), |
| 50 | + child: Row( |
| 51 | + children: [ |
| 52 | + Expanded( |
| 53 | + flex: 2, |
| 54 | + child: TextField( |
| 55 | + decoration: InputDecoration(hintText: 'Number'), |
| 56 | + onChanged: (number) { |
| 57 | + setState(() { |
| 58 | + _card = _card.copyWith(number: number); |
| 59 | + }); |
| 60 | + }, |
| 61 | + keyboardType: TextInputType.number, |
| 62 | + ), |
| 63 | + ), |
| 64 | + Container( |
| 65 | + padding: const EdgeInsets.symmetric(horizontal: 4), |
| 66 | + width: 80, |
| 67 | + child: TextField( |
| 68 | + decoration: InputDecoration(hintText: 'Exp. Year'), |
| 69 | + onChanged: (number) { |
| 70 | + setState(() { |
| 71 | + _card = _card.copyWith( |
| 72 | + expirationYear: int.tryParse(number)); |
| 73 | + }); |
| 74 | + }, |
| 75 | + keyboardType: TextInputType.number, |
| 76 | + ), |
| 77 | + ), |
| 78 | + Container( |
| 79 | + padding: const EdgeInsets.symmetric(horizontal: 4), |
| 80 | + width: 80, |
| 81 | + child: TextField( |
| 82 | + decoration: InputDecoration(hintText: 'Exp. Month'), |
| 83 | + onChanged: (number) { |
| 84 | + setState(() { |
| 85 | + _card = _card.copyWith( |
| 86 | + expirationMonth: int.tryParse(number)); |
| 87 | + }); |
| 88 | + }, |
| 89 | + keyboardType: TextInputType.number, |
| 90 | + ), |
| 91 | + ), |
| 92 | + Container( |
| 93 | + padding: const EdgeInsets.symmetric(horizontal: 4), |
| 94 | + width: 80, |
| 95 | + child: TextField( |
| 96 | + decoration: InputDecoration(hintText: 'CVC'), |
| 97 | + onChanged: (number) { |
| 98 | + setState(() { |
| 99 | + _card = _card.copyWith(cvc: number); |
| 100 | + }); |
| 101 | + }, |
| 102 | + keyboardType: TextInputType.number, |
| 103 | + ), |
| 104 | + ), |
| 105 | + ], |
| 106 | + ), |
| 107 | + ), |
| 108 | + CheckboxListTile( |
| 109 | + value: _saveCard, |
| 110 | + onChanged: (value) { |
| 111 | + setState(() { |
| 112 | + _saveCard = value; |
| 113 | + }); |
| 114 | + }, |
| 115 | + title: Text('Save card during payment'), |
| 116 | + ), |
| 117 | + Padding( |
| 118 | + padding: EdgeInsets.all(16), |
| 119 | + child: LoadingButton( |
| 120 | + onPressed: _handlePayPress, |
| 121 | + text: 'Pay', |
| 122 | + ), |
| 123 | + ), |
| 124 | + ], |
| 125 | + ), |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + Future<void> _handlePayPress() async { |
| 130 | + await Stripe.instance.dangerouslyUpdateCardDetails(_card); |
| 131 | + |
| 132 | + // 1. fetch Intent Client Secret from backend |
| 133 | + final clientSecret = await fetchPaymentIntentClientSecret(); |
| 134 | + |
| 135 | + // 2. Gather customer billing information (ex. email) |
| 136 | + final billingDetails = BillingDetails( |
| 137 | + |
| 138 | + phone: '+48888000888', |
| 139 | + address: Address( |
| 140 | + city: 'Houston', |
| 141 | + country: 'US', |
| 142 | + line1: '1459 Circle Drive', |
| 143 | + line2: '', |
| 144 | + state: 'Texas', |
| 145 | + postalCode: '77063', |
| 146 | + ), |
| 147 | + ); // mo mocked data for tests |
| 148 | + |
| 149 | + // 3. Confirm payment with card details |
| 150 | + // The rest will be done automatically using CustomCards |
| 151 | + // ignore: unused_local_variable |
| 152 | + final paymentIntent = await Stripe.instance.confirmPayment( |
| 153 | + clientSecret['clientSecret'], |
| 154 | + PaymentMethodParams.card( |
| 155 | + billingDetails: billingDetails, |
| 156 | + setupFutureUsage: |
| 157 | + _saveCard == true ? PaymentIntentsFutureUsage.OffSession : null, |
| 158 | + ), |
| 159 | + ); |
| 160 | + |
| 161 | + ScaffoldMessenger.of(context).showSnackBar(SnackBar( |
| 162 | + content: Text('Success!: The payment was confirmed successfully!'))); |
| 163 | + } |
| 164 | + |
| 165 | + Future<Map<String, dynamic>> fetchPaymentIntentClientSecret() async { |
| 166 | + final url = Uri.parse('$kApiUrl/create-payment-intent'); |
| 167 | + final response = await http.post( |
| 168 | + url, |
| 169 | + headers: { |
| 170 | + 'Content-Type': 'application/json', |
| 171 | + }, |
| 172 | + body: json.encode({ |
| 173 | + 'email': _email, |
| 174 | + 'currency': 'usd', |
| 175 | + 'items': [ |
| 176 | + {'id': 'id'} |
| 177 | + ], |
| 178 | + 'request_three_d_secure': 'any', |
| 179 | + }), |
| 180 | + ); |
| 181 | + return json.decode(response.body); |
| 182 | + } |
| 183 | +} |
0 commit comments