pub package

A plugin to add payments to your Flutter application.

Platform Support

AndroidiOS
Google PayApple Pay

Getting started

Before you start, create an account with the payment providers you are planning to support and follow the setup instructions:

Apple Pay:

  1. Take a look at the integration requirements.
  2. Create a merchant identifier for your business.
  3. Create a payment processing certificate to encrypt payment information.

Google Pay:

  1. Take a look at the integration requirements.
  2. Sign up to the business console and create an account.

Usage

To start using this plugin, add pay as a dependency in your pubspec.yaml file:

dependencies:
  pay: ^1.0.5

Payment configuration

Create a payment profile with the desired configuration for your payment, either using a local file or loading it from a remote server. Take a look at some examples under the example/assets/ folder and explore the documentation for a complete list of options available:

Example

import 'package:pay/pay.dart';

const _paymentItems = [
  PaymentItem(
    label: 'Total',
    amount: '99.99',
    status: PaymentItemStatus.final_price,
  )
];

ApplePayButton(
  paymentConfigurationAsset: 'default_payment_profile_apple_pay.json',
  paymentItems: _paymentItems,
  style: ApplePayButtonStyle.black,
  type: ApplePayButtonType.buy,
  margin: const EdgeInsets.only(top: 15.0),
  onPaymentResult: onApplePayResult,
  loadingIndicator: const Center(
    child: CircularProgressIndicator(),
  ),
),

GooglePayButton(
  paymentConfigurationAsset: 'default_payment_profile_google_pay.json',
  paymentItems: _paymentItems,
  style: GooglePayButtonStyle.black,
  type: GooglePayButtonType.pay,
  margin: const EdgeInsets.only(top: 15.0),
  onPaymentResult: onGooglePayResult,
  loadingIndicator: const Center(
    child: CircularProgressIndicator(),
  ),
),

void onApplePayResult(paymentResult) {
  // Send the resulting Apple Pay token to your server / PSP
}

void onGooglePayResult(paymentResult) {
  // Send the resulting Google Pay token to your server / PSP
}

Advanced usage

If you prefer to have more control over each individual request and the button separately, you can instantiate a payment client and add the buttons to your layout independently:

import 'package:pay/pay.dart';

const _paymentItems = [
  PaymentItem(
    label: 'Total',
    amount: '99.99',
    status: PaymentItemStatus.final_price,
  )
];

Pay _payClient = Pay.withAssets([
  'default_payment_profile_apple_pay.json',
  'default_payment_profile_google_pay.json'
]);

As you can see, you can add multiple configurations to your payment client, one for each payment provider supported.

Now, you can use the userCanPay method to determine whether the user can start a payment process with a given provider. This call returns a Future<bool> result that you can use to decide what to do next. For example, you can feed the Future to a FutureBuilder that shows a different UI based on the result of the call:

@override
Widget build(BuildContext context) {
  return FutureBuilder<bool>(
    future: _payClient.userCanPay(PayProvider.google_pay),
    builder: (context, snapshot) {
      if (snapshot.connectionState == ConnectionState.done) {
        if (snapshot.data == true) {
          return RawGooglePayButton(
            style: GooglePayButtonStyle.black,
            type: GooglePayButtonType.pay,
            onPressed: onGooglePayPressed);
        } else {
          // userCanPay returned false
          // Consider showing an alternative payment method
        }
      }
    },
  );
}

Finally, handle the onPressed event and trigger the payment selector as follows:

void onGooglePayPressed() async {
  final result = await _payClient.showPaymentSelector(
    provider: PayProvider.google_pay,
    paymentItems: _paymentItems,
  );
  // Send the resulting Google Pay token to your server / PSP
}

Additional resources

Take a look at the following resources to manage your payment accounts and learn more about the APIs for the supported providers:

Google PayApple Pay
Platforms Android iOS
 Documentation OverviewOverview
 Console Google Pay Business ConsoleDeveloper portal
ReferenceAPI reference Apple Pay API
Style guidelines Brand guidelinesButtons and Marks

Note: This is not an officially supported Google product.

Libraries

pay
Copyright 2021 Google LLC. SPDX-License-Identifier: Apache-2.0