stripe_sdk 2.4.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 90

Flutter Stripe SDK #

https://pub.dev/packages/stripe_sdk

A native dart package for Stripe. There are various other flutter plugins that wrap existing Stripe libraries, but this package uses a different approach. It does not wrap existing Stripe libraries, but instead accesses the Stripe API directly.

See examples for additional examples.

Features #

  • Supports all types of SCA, including 3DS, 3DS2, BankID and others.
  • Handle payments with complete SCA support.
  • Add, remove and update payment methods, sources and cards, optionally with SCA.
  • Manage customer information.
  • Create all types of Stripe tokens.
  • Forms, widgets and utilities to use directly, or create your own UI!

Supported APIs #

  • PaymentIntent, with SCA
  • SetupIntent, with SCA
  • PaymentMethod
  • Customer
  • Cards
  • Sources
  • Tokens

Planned features #

  • Add additional UI widgets
  • Support for additional APIs
  • Offer complete UI flow for adding payment method
  • Offer complete UI flow for checkout

Overview #

  • The return type for each function is Future<Map<String, dynamic>>, where the value depends on the stripe API version.

The library has three classes to access the Stripe API:

  • Stripe for generic, non-customer specific APIs, using publishable keys.
  • CustomerSession for customer-specific APIs, using stripe ephemeral keys.
  • StripeApi enables raw REST calls against the Stripe API.

Stripe #

Aims to provide high-level functionality similar to the official mobile Stripe SDKs.

CustomerSession #

Requires a Stripe ephemeral key.

Provides functionality similar to CustomerSession in the Stripe Android SDK.

StripeApi #

Provides basic low-level methods to access the Stripe REST API.

  • Limited to the APIs that can be used with a public key or ephemeral key.
  • Library methods map to a Stripe API call with the same name.
  • Additional parameters can be provided as an optional argument.

Stripe and CustomerSession use this internally.

Initialization #

All classes offer a singleton instance that can be initated by calling the init(...) methods and then accessed through .instance. Regular instances can also be created using the constructor, which allows them to be managed by e.g. dependency injection instead.

Stripe #

Stripe.init("pk_xxx");
// or, to manage your own instances
final stripe = Stripe("pk_xxx);

CustomerSession #

CustomerSession.init((apiVersion) => server.getEphemeralKeyFromServer(apiVersion));
// or, to manage your own instances
final session = CustomerSession((apiVersion) => server.getEphemeralKeyFromServer(apiVersion))

StripeApi #

StripeApi.init("pk_xxx");
// or, to manage your own instances
final stripeApi = StripeApi("pk_xxx);

UI #

Use CardForm to add or edit credit card details, or build your own form using the pre-built FormFields.

final formKey = GlobalKey<FormState>();
final card = StripeCard();

final form = CardForm(card:card, formKey: formKey);
 
onPressed: () async {
                if (formKey.currentState.validate()) {
                  formKey.currentState.save();
                }
}


SCA/PSD2 #

The library offers complete support for SCA on iOS and Android. It handles all types of SCA, including 3DS, 3DS2, BankID and others. It handles SCA by launching the authentication flow in a web browser, and returns the result to the app.

Stripe.init("pk_xxx");
final clientSecret = await server.createPaymentIntent(Stripe.getReturnUrl());
final paymentIntent = await Stripe.instance.confirmPayment(clientSecret, "pm_card_visa");

Android #

You need to declare this intent filter in android/app/src/main/AndroidManifest.xml:

<manifest ...>
  <!-- ... other tags -->
  <application ...>
    <activity ...>
      <!-- ... other tags -->

      <!-- Deep Links -->
      <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
          android:scheme="stripesdk"
          android:host="3ds.stripesdk.io" />
      </intent-filter>
    </activity>
  </application>
</manifest>

IOS #

For iOS you need to declare the scheme in ios/Runner/Info.plist (or through Xcode's Target Info editor, under URL Types):

<!-- ... other tags -->
<plist>
    <dict>
    <!-- ... other tags -->
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLName</key>
        <string>3ds.stripesdk.io</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>stripesdk</string>
        </array>
        </dict>
    </array>
    <!-- ... other tags -->
    </dict>
</plist>

Stripe SDK Changelog #

2.4.2 #

  • Allow custom form field error text
  • Fix bug where card number input field would allow more than 16 digits

2.4.1 #

  • Add support for custom input decorators on card form and form fields

2.4.0 #

  • Minor breaking change: Split package into two separate sub-libraries:

    • stripe_sdk: API related functionality
    • stripe_sdk_ui: UI widgets and utilities
  • Replaced ListView with Column inside the CardForm

  • Updated dependencies

2.3.0 #

  • Add CardForm widget, which can be used to add or edit credit cards.
    • Complete validation for card number, expiration date and CVC
    • Individual FormField widgets can be used to create a custom form
  • Fix bug in Stripe.authenticatePayment

2.2.0 #

  • Add support for connected accounts
    • Add optional constructor parameter stripeAccount for all APIs

2.1.1 #

  • Remove unused stripeAccount property on StripeApi

2.1.0 #

  • Add Stripe.confirmSetupIntentWithPaymentMethod

2.0.0 #

  • Rewrite of internal API
  • Fixed several issues and bugs
  • Restructured public API with breaking changes
    • Split Stripe into StripeApi and Stripe
    • SCA related features moved to Stripe
    • Moved basic Stripe API requests to StripeApi

See README and examples for further details details.

1.1.1 #

  • Misc minor updates and fixes

1.1.0 #

  • Complete support for SetupIntent with SCA
  • Fix bug with confirmPayment
  • Internal refactoring

1.0.1 #

  • Support multiple simultaneous authentication flows
  • Improve documentation and examples
  • Allow specifying apiVersion for CustomerSession
  • Allow multiple instances of Stripe and CustomerSession

1.0.0+1 #

  • Improve examples

1.0.0 #

  • Improve API for SCA-related features
  • Improve examples

0.0.2 #

  • Remove typed models
  • Add some support for payment intents
  • Complete support for payment methods
  • Complete support for tokens
  • Add examples
  • Major cleanup

0.0.1+2 #

  • Add analysis_options.yaml
  • Fix dartanalyzer issues
  • Fix other misc packaging issues

0.0.1+1 #

  • Initial release

example/main.dart

import 'dart:async';

import 'package:stripe_sdk/stripe_sdk.dart';

const publishableKey = "my-key";

/// StripeApi provides direct access to Stripe REST API
exampleSetupStripeApi() async {
  StripeApi.init(publishableKey);
  // See Stripe API documentation for details
  final cardData = {};
  await StripeApi.instance.createPaymentMethod(cardData);
}

/// CustomerSession provides access to customer specific APIs
exampleSetupSession() {
  CustomerSession.initCustomerSession(
      (apiVersion) => _fetchEphemeralKeyFromMyServer(apiVersion));
  CustomerSession.instance.listPaymentMethods();
}

Future<String> _fetchEphemeralKeyFromMyServer(String apiVersion) {
  // Send the apiVersion to your server, create the key and return the raw http body.
  return Future.value("raw-http-body");
}

/// This method supports the default payment flow as documented by Stripe.
/// https://stripe.com/docs/payments/payment-intents/android
exampleConfirmPayment() async {
  Stripe.init(publishableKey);
  final paymentIntentClientSecret =
      await _createPaymentIntent(Stripe.getReturnUrl());
  final paymentIntent = await Stripe.instance
      .confirmPayment(paymentIntentClientSecret, "pm-paymentMethod");
  if (paymentIntent['status'] == 'success') {
    // Confirmation successfull
  } else {
    // Handle other states
  }
}

/// Create payment intent and return the client secret.
/// The `return_url` must be set on the PaymentIntent.
/// https://stripe.com/docs/payments/payment-intents/android#create-payment-intent
Future<String> _createPaymentIntent(String returnUrl) {
  return Future.value("client_secret");
}

/// This method supports the manual payment flow as documented by Stripe.
/// https://stripe.com/docs/payments/payment-intents/android-manual
exampleAuthenticatePayment() async {
  Stripe.init(publishableKey);
  final paymentIntentClientSecret =
      await _createAndConfirmPaymentIntent(Stripe.getReturnUrl());
  final paymentIntent =
      await Stripe.instance.authenticatePayment(paymentIntentClientSecret);
  if (paymentIntent['status'] == "success") {
    // Authentication was successfull
  } else {
    // See stripe documentation for details on other possible statuses
  }
}

/// Create and confirm a payment intent on your server.
/// The `returnUrl` must be set on the PaymentIntent by your server.
/// Return the payment intent client secret.
/// https://stripe.com/docs/payments/payment-intents/android-manual#create-and-confirm-payment-intent-manual
Future<String> _createAndConfirmPaymentIntent(String returnUrl) {
  return Future.value("client_secret");
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  stripe_sdk: ^2.4.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:stripe_sdk/stripe_sdk.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
80
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
90
Learn more about scoring.

We analyzed this package on Dec 13, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.1+4
  • Flutter: 1.12.13+hotfix.4

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
flutter 0.0.0
http ^0.12.0 0.12.0+2
mask_text_input_formatter ^1.0.5 1.0.5
uni_links ^0.2.0 0.2.0
url_launcher ^5.1.2 5.4.1
Transitive dependencies
async 2.4.0
charcode 1.1.2
collection 1.14.11 1.14.12
flutter_web_plugins 0.0.0
http_parser 3.1.3
meta 1.1.8
path 1.6.4
pedantic 1.9.0
plugin_platform_interface 1.0.1
sky_engine 0.0.99
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
url_launcher_macos 0.0.1+2
url_launcher_platform_interface 1.0.4
url_launcher_web 0.1.0+2
vector_math 2.0.8
Dev dependencies
flutter_test