Skip to content

Commit

Permalink
feat: add customersheet to stripe sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Remon committed Oct 11, 2023
1 parent c11b25d commit c30aef5
Show file tree
Hide file tree
Showing 13 changed files with 1,498 additions and 10 deletions.
23 changes: 23 additions & 0 deletions packages/stripe/lib/src/stripe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,29 @@ class Stripe {
}
}

/// Initializes the customer sheet with the provided [parameters].
Future<CustomerSheetResult?> initCustomerSheet(
CustomerSheetInitParams params) async {
await _awaitForSettings();
return _platform.initCustomerSheet(params);
}

/// Display the customersheet sheet. With the provided [options].
Future<CustomerSheetResult?> presentCustomerSheet({
CustomerSheetPresentParams? options,
}) async {
await _awaitForSettings();
return _platform.presentCustomerSheet(options: options);
}

/// Retrieve the customer sheet payment option selection.
Future<CustomerSheetResult?>
retrieveCustomerSheetPaymentOptionSelection() async {
await _awaitForSettings();

return _platform.retrieveCustomerSheetPaymentOptionSelection();
}

FutureOr<void> _awaitForSettings() {
if (_needsSettings) {
_settingsFuture = applySettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:flutter/services.dart';
import 'package:stripe_platform_interface/src/models/ach_params.dart';
import 'package:stripe_platform_interface/src/models/create_token_data.dart';
import 'package:stripe_platform_interface/src/models/customer_sheet.dart';
import 'package:stripe_platform_interface/src/models/financial_connections.dart';
import 'package:stripe_platform_interface/src/models/google_pay.dart';
import 'package:stripe_platform_interface/src/models/intent_creation_callback_params.dart';
Expand Down Expand Up @@ -246,6 +247,44 @@ class MethodChannelStripe extends StripePlatform {
}
}

@override
Future<CustomerSheetResult?> initCustomerSheet(
CustomerSheetInitParams params) async {
final result = await _methodChannel.invokeMethod(
'initCustomerSheet',
{'params': params.toJson()},
);

if (result is List) {
return null;
} else {
return _parseCustomerSheetResult(result);
}
}

@override
Future<CustomerSheetResult?> presentCustomerSheet({
CustomerSheetPresentParams? options,
}) async {
final result = await _methodChannel.invokeMethod<dynamic>(
'presentCustomerSheet',
{'params': {}, 'options': options?.toJson() ?? {}},
);

return _parseCustomerSheetResult(result);
}

@override
Future<CustomerSheetResult?>
retrieveCustomerSheetPaymentOptionSelection() async {
final result = await _methodChannel.invokeMethod<dynamic>(
'retrieveCustomerSheetPaymentOptionSelection',
{},
);

return _parseCustomerSheetResult(result);
}

@override
Future<TokenData> createToken(CreateTokenParams params) async {
final invokeParams = params.map(
Expand Down Expand Up @@ -305,6 +344,33 @@ class MethodChannelStripe extends StripePlatform {
}
}

CustomerSheetResult? _parseCustomerSheetResult(Map<String, dynamic>? result) {
if (result != null) {
if (result.isEmpty) {
return null;
} else if (result['paymentOption'] != null) {
return CustomerSheetResult.fromJson(result['paymentOption']);
} else {
if (result['error'] != null) {
//workaround for tojson in sumtypes

Check warning on line 355 in packages/stripe_platform_interface/lib/src/method_channel_stripe.dart

View workflow job for this annotation

GitHub Actions / Typo CI

sumtypes

"sumtypes" is a typo. Did you mean "subtypes"?

Check warning on line 355 in packages/stripe_platform_interface/lib/src/method_channel_stripe.dart

View workflow job for this annotation

GitHub Actions / Typo CI

tojson

"tojson" is a typo. Did you mean "torsion"?
result['runtimeType'] = 'failed';
throw StripeException.fromJson(result);
} else {
throw StripeError<CustomerSheetError>(
message:
'Unknown result this is likely a problem in the plugin $result',
code: CustomerSheetError.unknown,
);
}
}
} else {
throw const StripeError<CustomerSheetError>(
message: 'Result should not be null',
code: CustomerSheetError.unknown,
);
}
}

@override
Future<PaymentMethod> createGooglePayPaymentMethod(
CreateGooglePayPaymentParams params) async {
Expand Down
116 changes: 116 additions & 0 deletions packages/stripe_platform_interface/lib/src/models/customer_sheet.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:stripe_platform_interface/stripe_platform_interface.dart';

part 'customer_sheet.freezed.dart';

Check warning on line 5 in packages/stripe_platform_interface/lib/src/models/customer_sheet.dart

View workflow job for this annotation

GitHub Actions / Typo CI

freezed

"freezed" is a typo. Did you mean "freezes"?
part 'customer_sheet.g.dart';

/// Parameters to setup a Customer sheet
///
/// For more info see https://stripe.com/docs/elements/customer-sheet?platform=react-native
@freezed

Check warning on line 11 in packages/stripe_platform_interface/lib/src/models/customer_sheet.dart

View workflow job for this annotation

GitHub Actions / Typo CI

freezed

"freezed" is a typo. Did you mean "freezes"?
class CustomerSheetInitParams with _$CustomerSheetInitParams {
@JsonSerializable(explicitToJson: true)
const factory CustomerSheetInitParams({
/// Color styling used for the Customersheet UI
@JsonKey(toJson: UserInterfaceStyleKey.toJson) ThemeMode? style,

/// Appearance of the customersheet.
///
/// When no appearance defined it will fallback to [style] or Stripe default.
PaymentSheetAppearance? appearance,

/// Optional but recommended for cards, required for other payment methods. The SetupIntent client secret that will be used to confirm a new payment method. If this is missing, you will only be able to add cards without authentication steps.
String? setupIntentClientSecret,

/// The identifier of the Stripe Customer object. See https://stripe.com/docs/api/customers/object#customer_object-id
required String customerId,

/// A short-lived token that allows the SDK to access a Customer's payment methods.
required String customerEphemeralKeySecret,

/// Your customer-facing business name. The default value is the name of your app.
String? merchantDisplayName,

/// Optional configuration for setting the header text of the Payment Method selection screen
String? headerTextForSelectionScreen,

/// CustomerSheet pre-populates fields with the values provided. If `billingDetailsCollectionConfiguration.attachDefaultsToPaymentMethod` is `true`, these values will be attached to the payment method even if they are not collected by the CustomerSheet UI.
BillingDetails? defaultBillingDetails,

/// Describes how billing details should be collected. All values default to `AUTOMATIC`. If `NEVER` is used for a required field for the Payment Method, you must provide an appropriate value as part of `defaultBillingDetails`.
BillingDetailsCollectionConfiguration?
billingDetailsCollectionConfiguration,

/// URL that redirects back to your app that CustomerSheet can use to auto-dismiss web views used for additional authentication, e.g. 3DS2
String? returnURL,

/// Optional configuration to display a custom message when a saved payment method is removed. iOS only.
String? removeSavedPaymentMethodMessage,

/// Whether to show Apple Pay as an option. Defaults to `false`.
@Default(true) bool applePayEnabled,

/// Whether to show Google Pay as an option. Defaults to `false`.
@Default(true) bool googlePayEnabled,
}) = _CustomerSheetInitParams;

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

@freezed

Check warning on line 62 in packages/stripe_platform_interface/lib/src/models/customer_sheet.dart

View workflow job for this annotation

GitHub Actions / Typo CI

freezed

"freezed" is a typo. Did you mean "freezes"?
class CustomerSheetPresentParams with _$CustomerSheetPresentParams {
@JsonSerializable(explicitToJson: true)
const factory CustomerSheetPresentParams({
/// Controls how the modal is presented (after animation). iOS only. Defaults to `popover`.
CustomerSheetPresentationStyle? presentationStyle,

/// Controls how the modal animates. iOS only.
CustomerSheetAnimationStyle? animationStyle,

/// Time (in milliseconds) before the Customer Sheet will automatically dismiss.
int? timeout,
}) = _CustomerSheetPresentParams;

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

@freezed

Check warning on line 80 in packages/stripe_platform_interface/lib/src/models/customer_sheet.dart

View workflow job for this annotation

GitHub Actions / Typo CI

freezed

"freezed" is a typo. Did you mean "freezes"?
class CustomerSheetResult with _$CustomerSheetResult {
@JsonSerializable(explicitToJson: true)
const factory CustomerSheetResult({
/// The users selected payment option, if one exists.
PaymentSheetPaymentOption? paymentOption,

/// The Stripe PaymentMethod associated with the paymentOption, if it exists.
PaymentMethod? paymentMethod,

/// The error that occurred
StripeError? error,
}) = _CustomerSheetResult;

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

enum CustomerSheetAnimationStyle {
flip,
curl,
slide,
dissolve,
}

enum CustomerSheetPresentationStyle {
fullscreen,
popover,
}

/*
/** Optional override. It is generally recommended to rely on the default behavior, but- provide a CustomerAdapter here if
* you would prefer retrieving and updating your Stripe customer object via your own backend instead.
* WARNING: When implementing your own CustomerAdapter, ensure your application complies with all applicable laws and regulations, including data privacy and consumer protection.
*/
customerAdapter?: CustomerAdapter;
*/
Loading

0 comments on commit c30aef5

Please sign in to comment.