Skip to content

Commit

Permalink
Merge pull request #28 from flutter-stripe/add-comments-to-public-api
Browse files Browse the repository at this point in the history
WIP Add documentation for Stripe package
  • Loading branch information
jonasbark authored Apr 29, 2021
2 parents 438c10e + f085c03 commit ef5f800
Show file tree
Hide file tree
Showing 7 changed files with 524 additions and 85 deletions.
232 changes: 149 additions & 83 deletions stripe/lib/src/stripe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,19 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:stripe_platform_interface/stripe_platform_interface.dart';

/// [Stripe] is the facade of the library and exposes the operations that can be
/// executed on the Stripe platform.
///
class Stripe {
/// Disables the platform override in order to use a manually registered
// Disables the platform override in order to use a manually registered
// ignore: comment_references
/// [SharePlatform] for testing purposes.
/// See https://github.com/flutter/flutter/issues/52267 for more details.
///
Stripe._();

static StripePlatform? __platform;

// This is to manually endorse the Linux plugin until automatic registration
// of dart plugins is implemented.
// [SharePlatform] for testing purposes.
// See https://github.com/flutter/flutter/issues/52267 for more details.
static StripePlatform get _platform {
__platform ??= StripePlatform.instance;
return __platform!;
}

static late final Stripe instance = Stripe._();

String? _publishableKey;
static String get publishableKey {
assert(instance._publishableKey != null,
'A publishableKey is required and missing');
return instance._publishableKey!;
}
//
Stripe._();

/// Sets the publishable key that is used to identify the account on the
/// Stripe platform.
static set publishableKey(String value) {
if (value == instance._publishableKey) {
return;
Expand All @@ -40,8 +25,17 @@ class Stripe {
instance.markNeedsSettings();
}

String? _stripeAccountId;
/// Retrieves the publishable API key.
static String get publishableKey {
assert(instance._publishableKey != null,
'A publishableKey is required and missing');
return instance._publishableKey!;
}

/// Retrieves the id associate with the Stripe account.
static String? get stripeAccountId => instance._stripeAccountId;

/// Sets the account id that is generated when creating a Stripe account.
static set stripeAccountId(String? value) {
if (value == instance._stripeAccountId) {
return;
Expand All @@ -50,9 +44,11 @@ class Stripe {
instance.markNeedsSettings();
}

ThreeDSecureConfigurationParams? _threeDSecureParams;
/// Retrieves the configuration parameters for 3D secure.
static ThreeDSecureConfigurationParams? get threeDSecureParams =>
instance._threeDSecureParams;

/// Sets the configuration parameters for 3D secure.
static set threeDSecureParams(ThreeDSecureConfigurationParams? value) {
if (value == instance._threeDSecureParams) {
return;
Expand All @@ -61,8 +57,10 @@ class Stripe {
instance.markNeedsSettings();
}

String? _merchantIdentifier;
/// Retrieves the merchant identifier.
static String? get merchantIdentifier => instance._merchantIdentifier;

/// Sets the merchant identifier.
static set merchantIdentifier(String? value) {
if (value == instance._merchantIdentifier) {
return;
Expand All @@ -71,43 +69,20 @@ class Stripe {
instance.markNeedsSettings();
}

bool _needsSettings = true;
void markNeedsSettings() {
_needsSettings = true;
}

Future<void>? settingsFuture;
FutureOr<void> awaitForSettings() {
if (_needsSettings) {
settingsFuture = applySettings();
}
if (settingsFuture != null) {
return settingsFuture;
}
return null;
}

/// Reconfigures the Stripe platform by applying the current values for
/// [publishableKey], [merchantIdentifier], [stripeAccountId],
/// [threeDSecureParams]
Future<void> applySettings() => _initialise(
publishableKey: publishableKey,
merchantIdentifier: merchantIdentifier,
stripeAccountId: stripeAccountId,
threeDSecureParams: threeDSecureParams,
);

Future<void> _initialise({
required String publishableKey,
String? stripeAccountId,
ThreeDSecureConfigurationParams? threeDSecureParams,
String? merchantIdentifier,
}) async {
await _platform.initialise(
publishableKey: publishableKey,
stripeAccountId: stripeAccountId,
threeDSecureParams: threeDSecureParams,
merchantIdentifier: merchantIdentifier,
);
}

/// Exposes a [ValueListenable] whether or not Apple pay is supported for this
/// device.
///
/// Always returns false on non Apple platforms.
ValueListenable<bool> get isApplePaySupported {
if (_isApplePaySupported == null) {
_isApplePaySupported = ValueNotifier(false);
Expand All @@ -116,20 +91,29 @@ class Stripe {
return _isApplePaySupported!;
}

ValueNotifier<bool>? _isApplePaySupported;

Future<void> checkApplePaySupport() async {
await awaitForSettings();
///Checks if Apple pay is supported on this device.
///
/// Always returns false on non Apple devices.
Future<bool> checkApplePaySupport() async {
await _awaitForSettings();
final isSupported = await _platform.isApplePaySupported();
_isApplePaySupported ??= ValueNotifier(false);
_isApplePaySupported?.value = isSupported;
return isSupported;
}

///Converts payment information defined in [data] into a [PaymentMethod]
///object that can be passed to your server.
///
/// [data] specificies the parameters associated with the specific
/// paymentmethod. See [PaymentMethodParams] for more details.
///
/// Throws an [StripeError] in case creating the payment method fails.
Future<PaymentMethod> createPaymentMethod(
PaymentMethodParams data, [
Map<String, String> options = const {},
]) async {
await awaitForSettings();
await _awaitForSettings();
try {
final paymentMethod = await _platform.createPaymentMethod(data, options);
return paymentMethod;
Expand All @@ -138,8 +122,11 @@ class Stripe {
}
}

/// Retrieves a [PaymentIntent] using the provided [clientSecret].
///
/// Throws an [StripeError] in case retrieving the intent fails.
Future<PaymentIntent> retrievePaymentIntent(String clientSecret) async {
await awaitForSettings();
await _awaitForSettings();
try {
final paymentMethod = await _platform.retrievePaymentIntent(clientSecret);
return paymentMethod;
Expand All @@ -148,25 +135,14 @@ class Stripe {
}
}

Future<PaymentIntent> confirmPaymentMethod(
String paymentIntentClientSecret,
PaymentMethodParams data, [
Map<String, String> options = const {},
]) async {
await awaitForSettings();
try {
final paymentMethod = await _platform.confirmPaymentMethod(
paymentIntentClientSecret, data, options);
return paymentMethod;
} on StripeError catch (_) {
rethrow;
}
}

/// Presents an Apple payment sheet using [params] for additional
/// configuration. See [ApplePayPresentParams] for more details.
///
/// Throws an [StripeError] in case presenting the payment sheet fails.
Future<void> presentApplePay(
ApplePayPresentParams params,
) async {
await awaitForSettings();
await _awaitForSettings();
if (!isApplePaySupported.value) {
//throw StripeError<ApplePayError>
//(ApplePayError.canceled, 'APPLE_PAY_NOT_SUPPORTED_MESSAGE');
Expand All @@ -178,10 +154,14 @@ class Stripe {
}
}

/// Confirms the Apple pay payment using the provided [clientSecret].
/// Use this method when the form is being submitted.
///
/// Throws an [StripeError] in confirming the payment fails.
Future<void> confirmApplePayPayment(
String clientSecret,
) async {
await awaitForSettings();
await _awaitForSettings();
if (!isApplePaySupported.value) {
//throw StripeError<ApplePayError>
//(ApplePayError.canceled, 'APPLE_PAY_NOT_SUPPORTED_MESSAGE');
Expand All @@ -193,10 +173,35 @@ class Stripe {
}
}

/// Confirms a payment method, using the provided [paymentIntentClientSecret]
/// and [data].
///
/// See [PaymentMethodParams] for more details. The method returns a
/// [PaymentIntent].
Future<PaymentIntent> confirmPaymentMethod(
String paymentIntentClientSecret,
PaymentMethodParams data, [
Map<String, String> options = const {},
]) async {
await _awaitForSettings();
try {
final paymentMethod = await _platform.confirmPaymentMethod(
paymentIntentClientSecret, data, options);
return paymentMethod;
} on StripeError catch (_) {
rethrow;
}
}

/// Use this method in case the [PaymentIntent] status is
/// [PaymentIntentsStatus.RequiresAction]. Executing this action can take
/// several seconds and it is important to not resubmit the form.
///
/// Throws [StripeError] in case handling the cardaction fails.
Future<PaymentIntent> handleCardAction(
String paymentIntentClientSecret,
) async {
await awaitForSettings();
await _awaitForSettings();
try {
final paymentIntent =
await _platform.handleCardAction(paymentIntentClientSecret);
Expand All @@ -207,12 +212,18 @@ class Stripe {
}
}

/// Confirm the [SetupIntent] using the [paymentIntentClientSecret]
/// and [params].
///
/// Use this method when the customer submits the form for SetupIntent.
///
/// Throws a [StripeError] when confirming the setupintent fails.
Future<SetupIntent> confirmSetupIntent(
String paymentIntentClientSecret,
PaymentMethodParams params, [
Map<String, String> options = const {},
]) async {
await awaitForSettings();
await _awaitForSettings();
try {
final setupIntent = await _platform.confirmSetupIntent(
paymentIntentClientSecret, params, options);
Expand All @@ -223,10 +234,15 @@ class Stripe {
}
}

/// Creates a token that represents an updated CVC.
///
/// Returns a single-use token.
///
/// Throws [StripeError] in case creating the token fails.
Future<String?> createTokenForCVCUpdate(
String cvc,
) async {
await awaitForSettings();
await _awaitForSettings();
try {
final tokenId = await _platform.createTokenForCVCUpdate(
cvc,
Expand All @@ -237,4 +253,54 @@ class Stripe {
rethrow;
}
}

FutureOr<void> _awaitForSettings() {
if (_needsSettings) {
_settingsFuture = applySettings();
}
if (_settingsFuture != null) {
return _settingsFuture;
}
return null;
}

Future<void>? _settingsFuture;

static late final Stripe instance = Stripe._();

String? _publishableKey;
String? _stripeAccountId;
ThreeDSecureConfigurationParams? _threeDSecureParams;
String? _merchantIdentifier;

static StripePlatform? __platform;

// This is to manually endorse the Linux plugin until automatic registration
// of dart plugins is implemented.
// See https://github.com/flutter/flutter/issues/52267 for more details.
static StripePlatform get _platform {
__platform ??= StripePlatform.instance;
return __platform!;
}

bool _needsSettings = true;
void markNeedsSettings() {
_needsSettings = true;
}

Future<void> _initialise({
required String publishableKey,
String? stripeAccountId,
ThreeDSecureConfigurationParams? threeDSecureParams,
String? merchantIdentifier,
}) async {
await _platform.initialise(
publishableKey: publishableKey,
stripeAccountId: stripeAccountId,
threeDSecureParams: threeDSecureParams,
merchantIdentifier: merchantIdentifier,
);
}

ValueNotifier<bool>? _isApplePaySupported;
}
13 changes: 12 additions & 1 deletion stripe_platform_interface/lib/src/models/address.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import 'package:freezed_annotation/freezed_annotation.dart';

part 'address.g.dart';
part 'address.freezed.dart';
part 'address.g.dart';

@freezed

/// Address information
class Address with _$Address {
@JsonSerializable(explicitToJson: true)
const factory Address({
/// City, town or district.
required String? city,
required String? country,

/// Address line1 (e.g. Street, C/O , PO Box).
required String? line1,

/// Address line2 (e.g. building, appartment or unit).
required String? line2,

/// ZIP or postal code.
required String? postalCode,

/// State or province.
required String? state,
}) = _Address;

Expand Down
Loading

0 comments on commit ef5f800

Please sign in to comment.