Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create proper result object for present paymentsheet #1290

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/stripe/lib/src/stripe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -481,23 +481,23 @@ class Stripe {
///
/// See [SetupPaymentSheetParameters] for more info. In order to show the
/// payment sheet it is required to call [presentPaymentSheet].
Future<void> initPaymentSheet({
Future<PaymentSheetPaymentOption?> initPaymentSheet({
required SetupPaymentSheetParameters paymentSheetParameters,
}) async {
assert(
!(paymentSheetParameters.applePay != null &&
instance._merchantIdentifier == null),
'merchantIdentifier must be specified if you are using Apple Pay. Please refer to this article to get a merchant identifier: https://support.stripe.com/questions/enable-apple-pay-on-your-stripe-account');
await _awaitForSettings();
await _platform.initPaymentSheet(paymentSheetParameters);
return _platform.initPaymentSheet(paymentSheetParameters);
}

/// Displays the paymentsheet
///
/// See [PresentPaymentSheetPameters] for more details
///
/// throws [StripeException] in case of a failure
Future<void> presentPaymentSheet({
Future<PaymentSheetPaymentOption?> presentPaymentSheet({
PaymentSheetPresentOptions? options,
}) async {
await _awaitForSettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,28 +226,29 @@ class MethodChannelStripe extends StripePlatform {
}

@override
Future<void> initPaymentSheet(SetupPaymentSheetParameters params) async {
await _methodChannel.invokeMethod(
Future<PaymentSheetPaymentOption?> initPaymentSheet(
SetupPaymentSheetParameters params) async {
final result = await _methodChannel.invokeMethod(
'initPaymentSheet',
{'params': params.toJson()},
);
if (result is List) {
return null;
} else {
return _parsePaymentSheetResult(result);
}
}

@override
Future<void> presentPaymentSheet({
Future<PaymentSheetPaymentOption?> presentPaymentSheet({
PaymentSheetPresentOptions? options,
}) async {
final result = await _methodChannel.invokeMethod<dynamic>(
'presentPaymentSheet',
{'params': {}, 'options': options?.toJson() ?? {}},
);

// iOS returns empty list on success
if (result is List) {
return;
} else {
return _parsePaymentSheetResult(result);
}
return _parsePaymentSheetResult(result);
}

@override
Expand All @@ -262,12 +263,11 @@ class MethodChannelStripe extends StripePlatform {
Future<void> confirmPaymentSheetPayment() async {
final result = await _methodChannel
.invokeMethod<dynamic>('confirmPaymentSheetPayment');

// iOS returns empty list on success
if (result is List) {
return;
} else {
return _parsePaymentSheetResult(result);
_parsePaymentSheetResult(result);
}
}

Expand Down Expand Up @@ -295,10 +295,13 @@ class MethodChannelStripe extends StripePlatform {
});
}

void _parsePaymentSheetResult(Map<String, dynamic>? result) {
PaymentSheetPaymentOption? _parsePaymentSheetResult(
Map<String, dynamic>? result) {
if (result != null) {
if (result.isEmpty || result['paymentOption'] != null) {
return;
if (result.isEmpty) {
return null;
} else if (result['paymentOption'] != null) {
return PaymentSheetPaymentOption.fromJson(result['paymentOption']);
} else {
if (result['error'] != null) {
//workaround for tojson in sumtypes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,21 @@ class PaymentSheetPresentOptions with _$PaymentSheetPresentOptions {
_$PaymentSheetPresentOptionsFromJson(json);
}

@freezed
class PaymentSheetPaymentOption with _$PaymentSheetPaymentOption {
@JsonSerializable(explicitToJson: true)
const factory PaymentSheetPaymentOption({
/// The label of the payment option
required String label,

/// String decoding of the image
required String image,
}) = _PaymentSheetPaymentOption;

factory PaymentSheetPaymentOption.fromJson(Map<String, dynamic> json) =>
_$PaymentSheetPaymentOptionFromJson(json);
}

@freezed
class BillingDetailsCollectionConfiguration
with _$BillingDetailsCollectionConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3867,6 +3867,178 @@ abstract class _PaymentSheetPresentOptions
get copyWith => throw _privateConstructorUsedError;
}

PaymentSheetPaymentOption _$PaymentSheetPaymentOptionFromJson(
Map<String, dynamic> json) {
return _PaymentSheetPaymentOption.fromJson(json);
}

/// @nodoc
mixin _$PaymentSheetPaymentOption {
/// The label of the payment option
String get label => throw _privateConstructorUsedError;

/// String decoding of the image
String get image => throw _privateConstructorUsedError;

Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
$PaymentSheetPaymentOptionCopyWith<PaymentSheetPaymentOption> get copyWith =>
throw _privateConstructorUsedError;
}

/// @nodoc
abstract class $PaymentSheetPaymentOptionCopyWith<$Res> {
factory $PaymentSheetPaymentOptionCopyWith(PaymentSheetPaymentOption value,
$Res Function(PaymentSheetPaymentOption) then) =
_$PaymentSheetPaymentOptionCopyWithImpl<$Res, PaymentSheetPaymentOption>;
@useResult
$Res call({String label, String image});
}

/// @nodoc
class _$PaymentSheetPaymentOptionCopyWithImpl<$Res,
$Val extends PaymentSheetPaymentOption>
implements $PaymentSheetPaymentOptionCopyWith<$Res> {
_$PaymentSheetPaymentOptionCopyWithImpl(this._value, this._then);

// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;

@pragma('vm:prefer-inline')
@override
$Res call({
Object? label = null,
Object? image = null,
}) {
return _then(_value.copyWith(
label: null == label
? _value.label
: label // ignore: cast_nullable_to_non_nullable
as String,
image: null == image
? _value.image
: image // ignore: cast_nullable_to_non_nullable
as String,
) as $Val);
}
}

/// @nodoc
abstract class _$$_PaymentSheetPaymentOptionCopyWith<$Res>
implements $PaymentSheetPaymentOptionCopyWith<$Res> {
factory _$$_PaymentSheetPaymentOptionCopyWith(
_$_PaymentSheetPaymentOption value,
$Res Function(_$_PaymentSheetPaymentOption) then) =
__$$_PaymentSheetPaymentOptionCopyWithImpl<$Res>;
@override
@useResult
$Res call({String label, String image});
}

/// @nodoc
class __$$_PaymentSheetPaymentOptionCopyWithImpl<$Res>
extends _$PaymentSheetPaymentOptionCopyWithImpl<$Res,
_$_PaymentSheetPaymentOption>
implements _$$_PaymentSheetPaymentOptionCopyWith<$Res> {
__$$_PaymentSheetPaymentOptionCopyWithImpl(
_$_PaymentSheetPaymentOption _value,
$Res Function(_$_PaymentSheetPaymentOption) _then)
: super(_value, _then);

@pragma('vm:prefer-inline')
@override
$Res call({
Object? label = null,
Object? image = null,
}) {
return _then(_$_PaymentSheetPaymentOption(
label: null == label
? _value.label
: label // ignore: cast_nullable_to_non_nullable
as String,
image: null == image
? _value.image
: image // ignore: cast_nullable_to_non_nullable
as String,
));
}
}

/// @nodoc

@JsonSerializable(explicitToJson: true)
class _$_PaymentSheetPaymentOption implements _PaymentSheetPaymentOption {
const _$_PaymentSheetPaymentOption(
{required this.label, required this.image});

factory _$_PaymentSheetPaymentOption.fromJson(Map<String, dynamic> json) =>
_$$_PaymentSheetPaymentOptionFromJson(json);

/// The label of the payment option
@override
final String label;

/// String decoding of the image
@override
final String image;

@override
String toString() {
return 'PaymentSheetPaymentOption(label: $label, image: $image)';
}

@override
bool operator ==(dynamic other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$_PaymentSheetPaymentOption &&
(identical(other.label, label) || other.label == label) &&
(identical(other.image, image) || other.image == image));
}

@JsonKey(ignore: true)
@override
int get hashCode => Object.hash(runtimeType, label, image);

@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
_$$_PaymentSheetPaymentOptionCopyWith<_$_PaymentSheetPaymentOption>
get copyWith => __$$_PaymentSheetPaymentOptionCopyWithImpl<
_$_PaymentSheetPaymentOption>(this, _$identity);

@override
Map<String, dynamic> toJson() {
return _$$_PaymentSheetPaymentOptionToJson(
this,
);
}
}

abstract class _PaymentSheetPaymentOption implements PaymentSheetPaymentOption {
const factory _PaymentSheetPaymentOption(
{required final String label,
required final String image}) = _$_PaymentSheetPaymentOption;

factory _PaymentSheetPaymentOption.fromJson(Map<String, dynamic> json) =
_$_PaymentSheetPaymentOption.fromJson;

@override

/// The label of the payment option
String get label;
@override

/// String decoding of the image
String get image;
@override
@JsonKey(ignore: true)
_$$_PaymentSheetPaymentOptionCopyWith<_$_PaymentSheetPaymentOption>
get copyWith => throw _privateConstructorUsedError;
}

BillingDetailsCollectionConfiguration
_$BillingDetailsCollectionConfigurationFromJson(Map<String, dynamic> json) {
return _BillingDetailsCollectionConfiguration.fromJson(json);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ abstract class StripePlatform extends PlatformInterface {
Future<bool> isApplePaySupported() async => false;

/// Configure the payment sheet using [SetupPaymentSheetParameters] as config.
Future<void> initPaymentSheet(SetupPaymentSheetParameters params);
Future<PaymentSheetPaymentOption?> initPaymentSheet(SetupPaymentSheetParameters params);

/// Display the payment sheet.
Future<void> presentPaymentSheet({
Future<PaymentSheetPaymentOption?> presentPaymentSheet({
PaymentSheetPresentOptions? options,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MethodChannelMock {
this.delay = Duration.zero,
this.result,
}) : methodChannel = MethodChannel(channelName, const JSONMethodCodec()) {
TestDefaultBinaryMessengerBinding.instance?.defaultBinaryMessenger
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(methodChannel, _handler);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/stripe_web/lib/src/web_stripe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,12 @@ class WebStripe extends StripePlatform {
}

@override
Future<void> initPaymentSheet(SetupPaymentSheetParameters params) {
Future<PaymentSheetPaymentOption?> initPaymentSheet(SetupPaymentSheetParameters params) {
throw WebUnsupportedError.method('initPaymentSheet');
}

@override
Future<void> presentPaymentSheet({
Future<PaymentSheetPaymentOption?> presentPaymentSheet({
PaymentSheetPresentOptions? options,
}) {
throw WebUnsupportedError.method('presentPaymentSheet');
Expand Down