Skip to content

Commit d0a96cf

Browse files
committed
fix: handle setupIntent response key in collectBankAccount and verifyMicrodeposits
When isPaymentIntent is false, the native SDK returns the result under the 'setupIntent' key, but the Dart method channel handler always looked for 'paymentIntent', causing a parsing failure. Extract a shared _parseIntentResult helper that checks isPaymentIntent and uses the correct response key. For setup intents, construct a PaymentIntent from the setup intent data to satisfy the platform interface contract.
1 parent 3d908f0 commit d0a96cf

1 file changed

Lines changed: 67 additions & 4 deletions

File tree

packages/stripe_platform_interface/lib/src/method_channel_stripe.dart

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'package:stripe_platform_interface/src/models/wallet.dart';
1414
import 'package:stripe_platform_interface/src/result_parser.dart';
1515

1616
import 'models/app_info.dart';
17+
import 'models/capture_method.dart';
1718
import 'models/card_details.dart';
1819
import 'models/errors.dart';
1920
import 'models/payment_intents.dart';
@@ -583,9 +584,7 @@ class MethodChannelStripe extends StripePlatform {
583584

584585
_financialConnectionsEventHandler = params.onEvent;
585586

586-
return ResultParser<PaymentIntent>(
587-
parseJson: (json) => PaymentIntent.fromJson(json),
588-
).parse(result: result!, successResultKey: 'paymentIntent');
587+
return _parseIntentResult(result!, isPaymentIntent: isPaymentIntent);
589588
}
590589

591590
@override
@@ -601,9 +600,73 @@ class MethodChannelStripe extends StripePlatform {
601600
'clientSecret': clientSecret,
602601
});
603602

603+
return _parseIntentResult(result!, isPaymentIntent: isPaymentIntent);
604+
}
605+
606+
/// Parses a method channel result that may contain either a `paymentIntent`
607+
/// or `setupIntent` key depending on [isPaymentIntent].
608+
///
609+
/// When [isPaymentIntent] is false, the native SDK returns the result under
610+
/// the `setupIntent` key. This helper converts the setup intent data into a
611+
/// [PaymentIntent] to satisfy the platform interface contract.
612+
PaymentIntent _parseIntentResult(
613+
Map<String, dynamic> result, {
614+
required bool isPaymentIntent,
615+
}) {
616+
if (isPaymentIntent) {
617+
return ResultParser<PaymentIntent>(
618+
parseJson: (json) => PaymentIntent.fromJson(json),
619+
).parse(result: result, successResultKey: 'paymentIntent');
620+
}
621+
622+
final setupIntentJson = result['setupIntent'] as Map<String, dynamic>?;
623+
if (setupIntentJson != null) {
624+
return PaymentIntent(
625+
id: setupIntentJson['id'] as String? ?? '',
626+
amount: 0,
627+
created: setupIntentJson['created']?.toString() ?? '',
628+
currency: '',
629+
status: _parseStatus(setupIntentJson['status'] as String? ?? ''),
630+
clientSecret: setupIntentJson['clientSecret'] as String? ?? '',
631+
livemode: setupIntentJson['livemode'] as bool? ?? false,
632+
paymentMethodId: setupIntentJson['paymentMethodId'] as String?,
633+
captureMethod: CaptureMethod.Automatic,
634+
confirmationMethod: ConfirmationMethod.Automatic,
635+
);
636+
}
637+
638+
// Fallback: try paymentIntent key in case the native SDK format changes.
604639
return ResultParser<PaymentIntent>(
605640
parseJson: (json) => PaymentIntent.fromJson(json),
606-
).parse(result: result!, successResultKey: 'paymentIntent');
641+
).parse(result: result, successResultKey: 'paymentIntent');
642+
}
643+
644+
static PaymentIntentsStatus _parseStatus(String value) {
645+
switch (value) {
646+
case 'Succeeded':
647+
case 'succeeded':
648+
return PaymentIntentsStatus.Succeeded;
649+
case 'RequiresPaymentMethod':
650+
case 'requires_payment_method':
651+
return PaymentIntentsStatus.RequiresPaymentMethod;
652+
case 'RequiresConfirmation':
653+
case 'requires_confirmation':
654+
return PaymentIntentsStatus.RequiresConfirmation;
655+
case 'Canceled':
656+
case 'canceled':
657+
return PaymentIntentsStatus.Canceled;
658+
case 'Processing':
659+
case 'processing':
660+
return PaymentIntentsStatus.Processing;
661+
case 'RequiresAction':
662+
case 'requires_action':
663+
return PaymentIntentsStatus.RequiresAction;
664+
case 'RequiresCapture':
665+
case 'requires_capture':
666+
return PaymentIntentsStatus.RequiresCapture;
667+
default:
668+
return PaymentIntentsStatus.Unknown;
669+
}
607670
}
608671

609672
@override

0 commit comments

Comments
 (0)