Skip to content

Commit

Permalink
Create setup intent with initial PM for stripe test cards
Browse files Browse the repository at this point in the history
  • Loading branch information
ezet committed Apr 25, 2020
1 parent cdb20b7 commit 08f2135
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
8 changes: 8 additions & 0 deletions example/lib/network/network_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ class NetworkService {
return IntentResponse(response['status'], response['clientSecret']);
}

// ignore: deprecated_member_use
Future<IntentResponse> 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<IntentResponse> createAutomaticPaymentIntent(int amount) async {
final params = {
Expand Down
24 changes: 11 additions & 13 deletions example/lib/setup_intent_with_sca.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
10 changes: 9 additions & 1 deletion lib/src/ui/screens/add_payment_method_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class _AddPaymentMethodScreenState extends State<AddPaymentMethodScreen> {
final StripeCard _cardData;
final GlobalKey<FormState> _formKey;
final CardForm _form;

// ignore: deprecated_member_use_from_same_package
Future<IntentResponse> setupIntent;

_AddPaymentMethodScreenState(this._form)
Expand All @@ -64,7 +66,7 @@ class _AddPaymentMethodScreenState extends State<AddPaymentMethodScreen> {

@override
void initState() {
setupIntent = widget._createSetupIntent();
if (widget._useSetupIntent) setupIntent = widget._createSetupIntent();
super.initState();
}

Expand All @@ -90,13 +92,19 @@ class _AddPaymentMethodScreenState extends State<AddPaymentMethodScreen> {

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);
}
},
)
Expand Down

0 comments on commit 08f2135

Please sign in to comment.