Flutter Stripe Payments

Accept payments in your Flutter apps

💳 What is Stripe in Flutter?

Stripe integration allows Flutter apps to securely process credit card payments, subscriptions, and digital wallets. It provides a complete payment solution with built-in security and compliance features for mobile applications.


// Add to pubspec.yaml
dependencies:
  flutter_stripe: ^10.0.0
  
// Initialize Stripe
Stripe.publishableKey = 'your_publishable_key';
                                    

Stripe Payment Features

💰

One-Time Payments

Accept single payments for products or services. Simple integration with card input forms and secure payment processing for immediate transactions in your Flutter app.

🔄

Subscriptions

Manage recurring billing cycles automatically. Handle monthly or yearly subscriptions with automatic renewals, trial periods, and flexible pricing plans for your users.

📱

Mobile Wallets

Support Apple Pay and Google Pay integration. Enable quick checkout with saved payment methods, providing seamless one-tap payment experience for mobile users.

🔒

Secure Processing

PCI-compliant payment handling built-in. Stripe manages security, encryption, and fraud detection automatically, keeping sensitive card data safe from your servers.

🔹 Setup Stripe Package

First, add the Stripe package to your Flutter project:

# pubspec.yaml
dependencies:
  flutter:
    sdk: flutter
  flutter_stripe: ^10.0.0
  http: ^1.1.0

Initialize Stripe in your main.dart:

import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // Set your publishable key
  Stripe.publishableKey = 'pk_test_your_key_here';
  
  runApp(MyApp());
}

🔹 Create Payment Intent

Create a payment intent on your server and process it in Flutter:

import 'package:http/http.dart' as http;
import 'dart:convert';

Future> createPaymentIntent(
  String amount, 
  String currency
) async {
  final url = Uri.parse('https://api.stripe.com/v1/payment_intents');
  
  final response = await http.post(
    url,
    headers: {
      'Authorization': 'Bearer sk_test_your_secret_key',
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: {
      'amount': amount,
      'currency': currency,
    },
  );
  
  return json.decode(response.body);
}

🔹 Payment Form Widget

Build a simple payment form with card input:

class PaymentScreen extends StatefulWidget {
  @override
  _PaymentScreenState createState() => _PaymentScreenState();
}

class _PaymentScreenState extends State {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Payment')),
      body: Padding(
        padding: EdgeInsets.all(16),
        child: Column(
          children: [
            CardField(
              onCardChanged: (card) {
                print('Card: ${card?.complete}');
              },
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () => processPayment(),
              child: Text('Pay \$50.00'),
            ),
          ],
        ),
      ),
    );
  }
  
  Future processPayment() async {
    // Payment processing logic
  }
}

🔹 Process Payment

Complete the payment flow with confirmation:

Future processPayment() async {
  try {
    // 1. Create payment intent
    final paymentIntent = await createPaymentIntent('5000', 'usd');
    
    // 2. Confirm payment
    await Stripe.instance.confirmPayment(
      paymentIntentClientSecret: paymentIntent['client_secret'],
      data: PaymentMethodParams.card(
        paymentMethodData: PaymentMethodData(),
      ),
    );
    
    // 3. Show success
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Payment successful!')),
    );
  } catch (e) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Payment failed: $e')),
    );
  }
}

🔹 Apple Pay Integration

Enable Apple Pay for iOS users:

Future payWithApplePay() async {
  try {
    // 1. Create payment intent
    final paymentIntent = await createPaymentIntent('5000', 'usd');
    
    // 2. Present Apple Pay
    await Stripe.instance.presentApplePay(
      params: ApplePayPresentParams(
        cartItems: [
          ApplePayCartSummaryItem.immediate(
            label: 'Product Name',
            amount: '50.00',
          ),
        ],
        country: 'US',
        currency: 'USD',
      ),
    );
    
    // 3. Confirm payment
    await Stripe.instance.confirmApplePayPayment(
      paymentIntent['client_secret'],
    );
    
    print('Apple Pay payment successful!');
  } catch (e) {
    print('Apple Pay failed: $e');
  }
}

🔹 Google Pay Integration

Enable Google Pay for Android users:

Future payWithGooglePay() async {
  try {
    // 1. Create payment intent
    final paymentIntent = await createPaymentIntent('5000', 'usd');
    
    // 2. Present Google Pay
    await Stripe.instance.initGooglePay(
      GooglePayInitParams(
        testEnv: true,
        merchantName: 'Your Store',
        countryCode: 'US',
      ),
    );
    
    await Stripe.instance.presentGooglePay(
      PresentGooglePayParams(
        clientSecret: paymentIntent['client_secret'],
        forSetupIntent: false,
      ),
    );
    
    print('Google Pay payment successful!');
  } catch (e) {
    print('Google Pay failed: $e');
  }
}

🧠 Test Your Knowledge

What package is used for Stripe in Flutter?