Skip to content

Commit

Permalink
feat: add handlenextaction for setupinten
Browse files Browse the repository at this point in the history
  • Loading branch information
Remon committed Aug 28, 2023
1 parent 91bf823 commit c054a6a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
22 changes: 21 additions & 1 deletion packages/stripe/lib/src/stripe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,25 @@ class Stripe {
}
}

/// Use this method in case the [SetupIntent] status is
/// [PaymentIntentsStatus.RequiresAction]. Executing this action can take
/// several seconds and it is important to not resubmit the form.
///
/// Throws a [StripeException] when confirming the handle card action fails.
Future<SetupIntent> handleNextActionForSetupIntent(
String setupIntentClientSecret,
{String? returnURL}) async {
await _awaitForSettings();
try {
final paymentIntent = await _platform.handleNextActionForSetupIntent(
setupIntentClientSecret,
returnURL: returnURL);
return paymentIntent;
} on StripeError {
rethrow;
}
}

/// Confirm the [SetupIntent] using the [paymentIntentClientSecret]
/// and [params].
///
Expand Down Expand Up @@ -446,7 +465,8 @@ class Stripe {

/// Method used to confirm to the user that the intent is created successfull
/// or not successfull when using a defferred payment method.
Future<void> intentCreationCallback(IntentCreationCallbackParams params) async {
Future<void> intentCreationCallback(
IntentCreationCallbackParams params) async {
await _awaitForSettings();
return await _platform.intentCreationCallback(params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ If you continue to have trouble, follow this discussion to get some support http
paymentIntentClientSecret = call.requiredArgument("paymentIntentClientSecret"),
promise = Promise(result)
)
"handleNextActionForSetup" -> stripeSdk.handleNextActionForSetup(
setupIntentClientSecret = call.requiredArgument("setupIntentClientSecret"),
promise = Promise(result)
)

"confirmPayment" -> stripeSdk.confirmPayment(
paymentIntentClientSecret = call.requiredArgument("paymentIntentClientSecret"),
params = call.optionalArgument("params"),
Expand Down
14 changes: 14 additions & 0 deletions packages/stripe_ios/ios/Classes/StripePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,20 @@ extension StripePlugin {
)
}

func handleNextActionForSetupIntent(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
guard let arguments = call.arguments as? FlutterMap,
let paymentIntentClientSecret = arguments["setupIntentClientSecret"] as? String else {
result(FlutterError.invalidParams)
return
}
handleNextActionForSetup(
setupIntentClientSecret: paymentIntentClientSecret,
returnURL: arguments["returnURL"] as? String,
resolver: resolver(for: result),
rejecter: rejecter(for: result)
)
}

func confirmPayment(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
guard let arguments = call.arguments as? FlutterMap,
let paymentIntentClientSecret = arguments["paymentIntentClientSecret"] as? String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ class MethodChannelStripe extends StripePlatform {
.parse(result: result!, successResultKey: 'paymentIntent');
}

@override
Future<SetupIntent> handleNextActionForSetupIntent(String setupIntentClientSecret,
{String? returnURL}) async {
final result = await _methodChannel
.invokeMapMethod<String, dynamic>('handleNextAction', {
'setupIntentClientSecret': setupIntentClientSecret,
if (_platformIsIos) 'returnURL': returnURL,
});

return ResultParser<SetupIntent>(
parseJson: (json) => SetupIntent.fromJson(json))
.parse(result: result!, successResultKey: 'setupIntent');
}

@override
Future<void> openApplePaySetup() async {
if (!_platformIsIos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ abstract class StripePlatform extends PlatformInterface {

Future<PaymentIntent> handleNextAction(String paymentIntentClientSecret,
{String? returnURL});

Future<SetupIntent> handleNextActionForSetupIntent(
String setupIntentClientSecret,
{String? returnURL});

Future<PaymentIntent> confirmPayment(
String paymentIntentClientSecret,
PaymentMethodParams? params,
Expand Down
7 changes: 7 additions & 0 deletions packages/stripe_web/lib/src/web_stripe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,13 @@ class WebStripe extends StripePlatform {
Future<void> intentCreationCallback(IntentCreationCallbackParams params) {
throw WebUnsupportedError.method('intentCreationCallback');
}

@override
Future<SetupIntent> handleNextActionForSetupIntent(
String setupIntentClientSecret,
{String? returnURL}) {
throw WebUnsupportedError.method('handleNextActionForSetupIntent');
}
}

class WebUnsupportedError extends Error implements UnsupportedError {
Expand Down

0 comments on commit c054a6a

Please sign in to comment.