· Flutter Deep Dives · 4 min read

In-app purchases and payment integration in Flutter and React Native

Mobile apps often rely on in-app purchases and payment integration to monetize their services. Flutter and React Native, two popular cross-platform frameworks, both offer robust capabilities for implementing in-app purchases and integrating payment systems.

In this blog post, we will explore how to incorporate in-app purchases and payment integration in your Flutter and React Native apps. We will discuss the libraries, tools, and steps involved in implementing these features.

Table of Contents

In-App Purchases in Flutter

Flutter provides the flutter_inapp_purchase package, which allows you to easily implement in-app purchases in your Flutter app. With this package, you can add consumable, non-consumable, and subscription-based products to your app.

To implement in-app purchases in Flutter, follow these steps:

  1. Add the flutter_inapp_purchase package to your pubspec.yaml file.
dependencies:
  flutter_inapp_purchase: ^3.2.1
  1. Initialize the in-app purchase functionality in your app.
import 'package:flutter_inapp_purchase/flutter_inapp_purchase.dart';

Future<void> initInAppPurchases() async {
  await FlutterInappPurchase.init();

  // Additional setup and listeners
}
  1. Fetch available products and display them in your app.
List<IAPItem> products = [];

Future<void> fetchProducts() async {
  List<IAPItem> fetchedProducts = await FlutterInappPurchase.getProducts(['product_id_1', 'product_id_2', ...]);
  
  setState(() {
    products = fetchedProducts;
  });
}

// Display the products in your app UI
  1. Implement the purchase flow when a user decides to buy a product.
Future<void> purchaseProduct(String productId) async {
  ProductDetails productDetails = products.firstWhere((product) => product.productId == productId);

  if (productDetails != null) {
    await FlutterInappPurchase.buyProduct(productDetails.productId);
    // Handle success or error scenarios
  } else {
    // Show an error message
  }
}

Payment Integration in Flutter

For payment integration, Flutter provides various plugins, such as stripe_payment, razorpay_flutter, and flutter_paystack. These plugins allow you to integrate popular payment gateways into your Flutter app.

Here’s an example of integrating Stripe payment in Flutter using the stripe_payment plugin:

  1. Add the stripe_payment package to your pubspec.yaml file.
dependencies:
  stripe_payment: ^1.0.7
  1. Configure Stripe with your API keys.
import 'package:stripe_payment/stripe_payment.dart';

void configureStripe() {
  StripePayment.setOptions(
    StripeOptions(publishableKey: "YOUR_PUBLISHABLE_KEY_HERE")
  );
}
  1. Collect payment details from the user.
PaymentMethod paymentMethod = await StripePayment.paymentRequestWithCardForm(CardFormPaymentRequest());
  1. Use the obtained payment method to make the payment.
StripePayment.createPaymentMethod(PaymentMethodRequest(
  card: CreditCard(
    number: paymentMethod.card.number,
    expMonth: paymentMethod.card.expMonth,
    expYear: paymentMethod.card.expYear,
  ),
)).then((paymentMethod) {
  // Complete the payment process with the payment method
}).catchError((error) {
  // Handle any errors during the payment process
});

In-App Purchases in React Native

In React Native, you can use the react-native-iap package to implement in-app purchases. This package provides a unified API for both iOS and Android platforms.

To implement in-app purchases in React Native, follow these steps:

  1. Add the react-native-iap package to your project.
npm install react-native-iap --save
  1. Link the package with your project.
react-native link react-native-iap
  1. Initialize the in-app purchase functionality.
import { IAP } from 'react-native-iap';

IAP.init();
  1. Fetch available products and display them in your app.
const productIds = ['product_id_1', 'product_id_2', ...];

IAP.getProducts(productIds).then((products) => {
  // Display the products in your app UI
}).catch((error) => {
  // Handle any errors
});
  1. Implement the purchase flow when a user decides to buy a product.
const productId = 'product_id';

IAP.requestPurchase(productId).then(() => {
  // Handle successful purchase
}).catch((error) => {
  // Handle any errors during the purchase process
});

Payment Integration in React Native

For payment integration in React Native, you can leverage existing payment gateway SDKs or use packages like react-native-stripe-api or react-native-paypal.

Using the react-native-stripe-api package, here’s an example of integrating Stripe payment in React Native:

  1. Add the react-native-stripe-api package to your project.
npm install react-native-stripe-api --save
  1. Configure Stripe with your API keys.
import { Stripe } from 'react-native-stripe-api';

const stripe = new Stripe('YOUR_SECRET_KEY_HERE');
  1. Collect payment details from the user.
const paymentMethod = await stripe.paymentRequestWithCardForm();
  1. Use the obtained payment method to make the payment.
stripe.createPaymentMethod({
  type: 'card',
  card: {
    number: paymentMethod.card.number,
    expMonth: paymentMethod.card.expMonth,
    expYear: paymentMethod.card.expYear,
    cvc: paymentMethod.card.cvc,
  },
}).then((paymentMethod) => {
  // Complete the payment process with the payment method
}).catch((error) => {
  // Handle any errors during the payment process
});

Conclusion

Implementing in-app purchases and payment integration in Flutter and React Native is crucial for monetizing mobile apps. Flutter provides the flutter_inapp_purchase package for in-app purchases and various payment gateway plugins. React Native offers the react-native-iap package for in-app purchases and you can leverage existing payment gateway SDKs or packages like react-native-stripe-api for payment integration.

By following the steps and utilizing the mentioned libraries, you can easily incorporate in-app purchases and payment systems in your Flutter and React Native apps, delivering a seamless user experience while generating revenue for your app.

#flutter #reactnative

Share:
Back to Blog