Pay with Stripe
Pay with Stripe is a Flutter package that simplifies the Stripe payment process. Instead of handling multiple steps manually, this package allows you to complete the entire payment process with just a single line of code.
With this package, you can easily collect payment details using Stripeβs secure infrastructure, providing a smooth and hassle-free experience for both developers and users.
Demo
Recommended usage
If you're selling digital products or services within your app, (e.g. subscriptions, in-game currencies, game levels, access to premium content, or unlocking a full version), you must use the app store's in-app purchase APIs. See Apple's and Google's guidelines for more information. For all other scenarios you can use this SDK to process payments via Stripe.
π Installation
To install the Pay with stripe Package, follow these steps
-
Add the package to your project's dependencies in the
pubspec.yamlfile:dependencies: pay_with_stripe: ^0.0.3 -
Run the following command to fetch the package:
flutter pub get
Requirements
Note: Currently, this package supports only Android and iOS. Web support is planned for future updates.
Android
This plugin requires several changes to be able to work on Android devices. Please make sure you follow all these steps:
- Use Android 5.0 (API level 21) and above.
- Use Kotlin version 1.8.0 and above: example
- Requires Android Gradle plugin 8 and higher
- Using a descendant of
Theme.AppCompatfor your activity: example, example night theme - Using an up-to-date Android gradle build tools version: example and an up-to-date gradle version accordingly: example
- Using
FlutterFragmentActivityinstead ofFlutterActivityinMainActivity.kt: example - Add the following rules to your
proguard-rules.profile: example
-dontwarn com.stripe.android.pushProvisioning.PushProvisioningActivity$g
-dontwarn com.stripe.android.pushProvisioning.PushProvisioningActivityStarter$Args
-dontwarn com.stripe.android.pushProvisioning.PushProvisioningActivityStarter$Error
-dontwarn com.stripe.android.pushProvisioning.PushProvisioningActivityStarter
-dontwarn com.stripe.android.pushProvisioning.PushProvisioningEphemeralKeyProvider
- Add the following line to your
gradle.propertiesfile: example
android.enableR8.fullMode=false
This will prevent crashes with the Stripe SDK on Android (see issue).
- Rebuild the app, as the above changes don't update with hot reload
These changes are needed because the Android Stripe SDK requires the use of the AppCompat theme for their UI components and the Support Fragment Manager for the Payment Sheets
If you are having troubles to make this package to work on Android, join this discussion to get some support.
iOS
Compatible with apps targeting iOS 13 or above.
To upgrade your iOS deployment target to 13.0, you can either do so in Xcode under your Build Settings, or by modifying IPHONEOS_DEPLOYMENT_TARGET in your project.pbxproj directly.
You will also need to update in your Podfile:
platform :ios, '13.0'
For card scanning add the following to your Info.plist:
<key>NSCameraUsageDescription</key>
<string>Scan your card to add it automatically</string>
<key>NSCameraUsageDescription</key>
<string>To scan cards</string>
Usage
-
Import the package into your Dart file:
import 'package:pay_with_stripe/pay_with_stripe.dart'; -
To initiate a payment using Pay with Stripe, use the following method:
final result = await PayWithStrip.makePayment( paymentModel: PaymentModel( publishableKey: publishableKey, // get publish key from strip dashboard secretKey:secretKey, // get secretKey key from strip dashboard amount: 100)); // $100 log("result: $result"); -
(Optional) Save card details for future payments
If you want to store the user's card details, so they don't need to enter them again for future payments, use the following method once during account creation:
await PayWithStrip.createCustomer( customerModel: CustomerModel( customerId: "customerId", // A unique ID created for the user secretKey: "secretKey")); // get secretKey key from strip dashboard- This allows the user to make future payments instantly without re-entering card details.
- The
customerIdused here must be the same as the one used during payment to link the saved card to the user. - If this method is not used, the user will need to enter their card details manually for each transaction.
Payment Configuration Parameters
Below is a list of parameters required to configure and process a payment using Pay with Stripe.
Mandatory fields are marked as required, while others are optional and can be customized based on your needs.
| Attribute | Type | Required | Description |
|---|---|---|---|
publishableKey |
String |
β Yes | The publishable key from your Stripe dashboard. |
secretKey |
String |
β Yes | The secret key from your Stripe dashboard. |
amount |
num |
β Yes | The payment amount (e.g., 100 for $100). |
currency |
Currency |
β No | The currency for the payment. Supports only USD and EUR. Defaults to USD if not provided. |
saveCard |
bool |
β No | If true, the card details will be saved for future use. The user won't need to re-enter them. |
customerId |
String? |
β No | A unique ID for the user. Required for saving and reusing cards. Can only be used if the user already exists in Stripe's Customers database. |
merchantDisplayName |
String |
β No | The name of the merchant displayed in the payment UI. |
style |
ThemeMode? |
β No | The theme style for the payment UI (e.g., light or dark mode). |
Test Card
For testing payments, you can use the following Stripe test card:
- Card Number:
4242 4242 4242 4242
β‘ Donate
If you would like to support me, please consider making a donation through one of the following links:
Thank you for your support!