PayEngine SDK for Flutter

The PayEngine Flutter SDK allows you to build delightful payment experiences in your native Android and iOS apps using Flutter. We provide powerful and customizable UI screens and elements that can be used out-of-the-box to collect your users' payment details

Installation

Add PayEngine registry to your project:

allprojects {
    repositories {
        ...your repositories
        
        maven {
            name = "pe-maven"
            url = uri("https://maven.payengine.co/payengine")
            credentials {
                username = "<username>"
                password = "<password>"
            }
        }
    }
}

Contact PayEngine support for username and password

Prerequisites

Android

To use secure fields components, you need to install and configure the Material Components theme in your app.

  1. Add the following dependency to your app/build.gradle file with the specified version:
implementation 'com.google.android.material:material:<version>'
  1. Set the appropriate style in your styles.xml file:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- ... -->
</style>

iOS

cd ios && pod install --repo-update && cd ..

Usage

PayEngine config

final PayEngineConfig config = const PayEngineConfig(
      publicKey: "pk_test_xxx",
      scriptURL: "https://console.payengine.dev");
final String merchantId = "<your-merchant-id>";

Wrap the config in a Provider

PayEngineProvider(
  config: Config.config, 
  child: const MyApp()
)

Credit Card Form

var additionalFields = List<PayEngineField>.from([
      PayEngineField('address_zip', 'text', 'Zip Code', true,
          keyboardType: PayEngineKeyboardType.number,
          pattern:
              r"^(?:\d{5}(?:-\d{4})?|[ABCEGHJKLMNPRSTVXY]\d[A-Z] ?\d[A-Z]\d)$"),
    ]);

final creditCardForm = CreditCardForm(additionalFields: additionalFields);

Container(padding: const EdgeInsets.all(10), child: creditCardForm),

OutlinedButton(
    onPressed: () async {
      final response = await creditCardForm.tokenize(merchantId);
      setState(() {
        result = response;
      });
    },
    child: const Text('Tokenize')),

Bank Account Form

final bankAccountForm = BankAccountForm();

Container(padding: const EdgeInsets.all(10), child: bankAccountForm),
OutlinedButton(
    onPressed: () async {
      final response = await bankAccountForm.tokenize(merchantId);
      setState(() {
        result = response;
      });
    },
    child: const Text('Tokenize')),

Google Pay and Apple Pay

Check whether is supported or not:

final isSupported = PayEngineNative.userCanPay(PayProvider.applePay, Config.merchantId)

Always call this method before rendering the pay button, see the example below.

Google Pay


final paymentRequest = PEPaymentRequest(
  merchantId: Config.merchantId, 
  paymentAmount: 10.00, 
  paymentItems: [
    PEPaymentItem(
      label: "Test Item", 
      amount: 10.00,
    )
  ],
  platformOptions: PEGooglePayOptions(billingAddressRequired: true, shippingAddressRequired: true)
);

// render button
FutureBuilder<bool>(
  future: PayEngineNative.userCanPay(
      PayProvider.googlePay, Config.merchantId),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.done) {
      if (snapshot.hasError) {
        return Center(
          child: Text(
            '${snapshot.error}',
          ),
        );
      } else if (snapshot.data == true) {
        return PEGooglePayButton(
          paymentRequest: paymentRequest,
          onPaymentFailed: (error) {
            debugPrint("onPaymentFailed $error");
            result = error.toString();
          },
          onPaymentSheetDismissed: () {
            debugPrint("onPaymentSheetDismissed");
          },
          onTokenDidReturn: (token, metadata, billingAddress, shippingAddress) {
            debugPrint("onTokenDidReturn $token $metadata ${billingAddress?.toJson()} ${shippingAddress?.toJson()}");
            result = token;
          },
        );
      } else {
        return const Text("Google Pay is not supported");
      }
    } else {
      return const SizedBox.shrink();
    }
  })

Apple Pay


final paymentRequest = PEPaymentRequest(
  merchantId: Config.merchantId,
  paymentAmount: 10.00,
  paymentItems: [
    PEPaymentItem(
      label: "Test Item",
      amount: 10.00,
    )
  ],
  platformOptions: PEApplePayOptions(
      requiredBillingContactFields: [PEContactField.postalAddress],
      requiredShippingContactFields: [PEContactField.name])
  );

// render button
FutureBuilder<bool>(
  future: PayEngineNative.userCanPay(
      PayProvider.applePay, Config.merchantId),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.done) {
      if (snapshot.hasError) {
        return Center(
          child: Text(
            '${snapshot.error}',
          ),
        );
      } else if (snapshot.data == true) {
        return PEApplePayButton(
          paymentRequest: paymentRequest,
          onPaymentFailed: (error) {
            debugPrint("onPaymentFailed $error");
            result = error.toString();
          },
          onPaymentSheetDismissed: () {
            debugPrint("onPaymentSheetDismissed");
          },
          onTokenDidReturn: (token, metadata, billingContact, shippingContact) {
            debugPrint("onTokenDidReturn $token $metadata ${billingContact?.toJson()} ${shippingContact?.toJson()}");
            result = token;
          },
        );
      } else {
        return const Text("Apple Pay is not supported");
      }
    } else {
      return const SizedBox.shrink();
    }
  })