diff --git a/example/lib/network/network_service.dart b/example/lib/network/network_service.dart index d65eced..e93de66 100644 --- a/example/lib/network/network_service.dart +++ b/example/lib/network/network_service.dart @@ -45,6 +45,14 @@ class NetworkService { return IntentResponse(response['status'], response['clientSecret']); } + // ignore: deprecated_member_use + Future createSetupIntentWithPaymentMethod(paymentMethod) async { + final params = {'paymentMethod': paymentMethod, "returnUrl": Stripe.instance.getReturnUrlForSca()}; + final response = await _call('createSetupIntent', params); + // ignore: deprecated_member_use + return IntentResponse(response['status'], response['clientSecret']); + } + // ignore: deprecated_member_use Future createAutomaticPaymentIntent(int amount) async { final params = { diff --git a/example/lib/setup_intent_with_sca.dart b/example/lib/setup_intent_with_sca.dart index 25066bb..7011756 100644 --- a/example/lib/setup_intent_with_sca.dart +++ b/example/lib/setup_intent_with_sca.dart @@ -42,21 +42,19 @@ class SetupIntentWithScaScreen extends StatelessWidget { final Stripe stripe = Stripe.instance; final NetworkService networkService = locator.get(); showProgressDialog(context); + final createSetupIntentResponse = await networkService.createSetupIntentWithPaymentMethod(paymentMethod); + final PaymentMethodStore paymentMethods = Provider.of(context, listen: false); + if (createSetupIntentResponse.status == 'succeeded') { + hideProgressDialog(context); + Navigator.pop(context, true); - final createSetupIntentResponse = await networkService.createSetupIntent(); - var setupIntent = - await stripe.confirmSetupIntentWithPaymentMethod(createSetupIntentResponse.clientSecret, paymentMethod); - - PaymentMethodStore store = Provider.of(context); - hideProgressDialog(context); - if (setupIntent['status'] == 'succeeded') { - - // A new method has been attached, so refresh the store. // ignore: unawaited_futures - store.refresh(); - } else { - // Something went wrong - Navigator.pop(context, false); + paymentMethods.refresh(); + + return; } + var setupIntent = await stripe.confirmSetupIntent(createSetupIntentResponse.clientSecret); + hideProgressDialog(context); + Navigator.pop(context, setupIntent['status'] == 'succeeded'); } } diff --git a/lib/src/ui/screens/add_payment_method_screen.dart b/lib/src/ui/screens/add_payment_method_screen.dart index 0a5233d..cc3779f 100644 --- a/lib/src/ui/screens/add_payment_method_screen.dart +++ b/lib/src/ui/screens/add_payment_method_screen.dart @@ -56,6 +56,8 @@ class _AddPaymentMethodScreenState extends State { final StripeCard _cardData; final GlobalKey _formKey; final CardForm _form; + + // ignore: deprecated_member_use_from_same_package Future setupIntent; _AddPaymentMethodScreenState(this._form) @@ -64,7 +66,7 @@ class _AddPaymentMethodScreenState extends State { @override void initState() { - setupIntent = widget._createSetupIntent(); + if (widget._useSetupIntent) setupIntent = widget._createSetupIntent(); super.initState(); } @@ -90,13 +92,19 @@ class _AddPaymentMethodScreenState extends State { hideProgressDialog(context); if (setupIntent['status'] == 'succeeded') { + /// A new payment method has been attached, so refresh the store. + // ignore: unawaited_futures + widget.paymentMethodStore.refresh(); Navigator.pop(context, true); + return; } } else { paymentMethod = await widget.paymentMethodStore.attachPaymentMethod(paymentMethod['id']); + hideProgressDialog(context); Navigator.pop(context, true); return; } + Navigator.pop(context, false); } }, )