Skip to content

Commit 1ed0ae8

Browse files
authored
Enable automatic webview closing by demand (flutter-stripe#1096)
fixes flutter-stripe#1081
1 parent 091c6ec commit 1ed0ae8

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

example/lib/screens/card_payments/no_webhook_payment_screen.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@ class _NoWebhookPaymentScreenState extends State<NoWebhookPaymentScreen> {
135135
if (paymentIntentResult['clientSecret'] != null &&
136136
paymentIntentResult['requiresAction'] == true) {
137137
// 4. if payment requires action calling handleNextAction
138-
final paymentIntent = await Stripe.instance
139-
.handleNextAction(paymentIntentResult['clientSecret']);
138+
final paymentIntent = await Stripe.instance.handleNextAction(
139+
paymentIntentResult['clientSecret'],
140+
returnURL: 'flutterstripe://redirect',
141+
);
140142

141143
// todo handle error
142144
/*if (cardActionError) {

example/server/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ app.post(
288288
const params: Stripe.PaymentIntentCreateParams = {
289289
amount: orderAmount,
290290
confirm: true,
291+
return_url: 'flutterstripe://redirect',
291292
confirmation_method: 'manual',
292293
currency,
293294
payment_method: paymentMethods.data[0].id,
@@ -306,6 +307,7 @@ app.post(
306307
const params: Stripe.PaymentIntentCreateParams = {
307308
amount: orderAmount,
308309
confirm: true,
310+
return_url: 'flutterstripe://redirect',
309311
confirmation_method: 'manual',
310312
currency,
311313
payment_method: paymentMethodId,

packages/stripe/lib/src/stripe.dart

+4-5
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,12 @@ class Stripe {
287287
/// several seconds and it is important to not resubmit the form.
288288
///
289289
/// Throws a [StripeException] when confirming the handle card action fails.
290-
Future<PaymentIntent> handleNextAction(
291-
String paymentIntentClientSecret,
292-
) async {
290+
Future<PaymentIntent> handleNextAction(String paymentIntentClientSecret,
291+
{String? returnURL}) async {
293292
await _awaitForSettings();
294293
try {
295-
final paymentIntent =
296-
await _platform.handleNextAction(paymentIntentClientSecret);
294+
final paymentIntent = await _platform
295+
.handleNextAction(paymentIntentClientSecret, returnURL: returnURL);
297296
return paymentIntent;
298297
} on StripeError {
299298
//throw StripeError<CardActionError>(error.code, error.message);

packages/stripe_ios/ios/Classes/StripePlugin.swift

+15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class StripePlugin: StripeSdk, FlutterPlugin, ViewManagerDelegate {
2121

2222
let instance = StripePlugin(channel: channel)
2323
registrar.addMethodCallDelegate(instance, channel: channel)
24+
registrar.addApplicationDelegate(instance)
2425

2526
// Card Field
2627
let cardFieldFactory = CardFieldViewFactory(messenger: registrar.messenger(), delegate:instance)
@@ -133,6 +134,20 @@ class StripePlugin: StripeSdk, FlutterPlugin, ViewManagerDelegate {
133134
func sendEvent(withName name: String, body: [String: Any]) {
134135
channel.invokeMethod(name, arguments: body)
135136
}
137+
138+
func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
139+
return StripeAPI.handleURLCallback(with: url)
140+
}
141+
142+
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]) -> Void) -> Bool {
143+
144+
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
145+
if let url = userActivity.webpageURL {
146+
return StripeAPI.handleURLCallback(with: url)
147+
}
148+
}
149+
return false
150+
}
136151
}
137152

138153

0 commit comments

Comments
 (0)