flutter_google_pay 0.1.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 86

flutter_google_pay #

pub

Accept Payments with Android Pay using the Payment Request API.

Usage #

  import 'package:flutter_google_pay/flutter_google_pay.dart';
  
  
  _makeStripePayment() async {
      var environment = 'rest'; // or 'production'

      if (!(await FlutterGooglePay.isAvailable(environment))) {
        _showToast(scaffoldContext, 'Google pay not available');
      } else {
        PaymentItem pm = PaymentItem(
            stripeToken: 'pk_test_1IV5H8NyhgGYOeK6vYV3Qw8f',
            stripeVersion: "2018-11-08",
            currencyCode: "usd",
            amount: "0.10",
            gateway: 'stripe');

        FlutterGooglePay.makePayment(pm).then((Result result) {
          if (result.status == ResultStatus.SUCCESS) {
            _showToast(scaffoldContext, 'Success');
          }
        }).catchError((dynamic error) {
          _showToast(scaffoldContext, error.toString());
        });
      }
    }

    _makeCustomPayment() async {
      var environment = 'rest'; // or 'production'

      if (!(await FlutterGooglePay.isAvailable(environment))) {
        _showToast(scaffoldContext, 'Google pay not available');
      } else {
        ///docs https://developers.google.com/pay/api/android/guides/tutorial
        PaymentBuilder pb = PaymentBuilder()
          ..addGateway("example")
          ..addTransactionInfo("1.0", "USD")
          ..addAllowedCardAuthMethods(["PAN_ONLY", "CRYPTOGRAM_3DS"])
          ..addAllowedCardNetworks(
              ["AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"])
          ..addBillingAddressRequired(true)
          ..addPhoneNumberRequired(true)
          ..addShippingAddressRequired(true)
          ..addShippingSupportedCountries(["US", "GB"])
          ..addMerchantInfo("Example");

        FlutterGooglePay.makeCustomPayment(pb.build()).then((Result result) {
          if (result.status == ResultStatus.SUCCESS) {
            _showToast(scaffoldContext, 'Success');
          } else if (result.error != null) {
            _showToast(context, result.error);
          }
        }).catchError((error) {
          //TODO
        });
      }
    }
  

Doc for creating custom payment data: #

Google Pay

0.1.4 #

  • Added PaymentBuilder for building custom requests

0.1.3+3 #

  • Added more data about payment info

0.1.3+1 #

  • Example update

0.1.3 #

  • Fix bug with payment result. Added environment pros. Sample update

0.1.2+2 #

  • Remove annotation

0.1.2+1 #

  • Docs update

0.1.0 #

  • First version of Google Pay

example/lib/main.dart

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

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  BuildContext scaffoldContext;

  @override
  void initState() {
    super.initState();
  }

  _makeStripePayment() async {
    var environment = 'rest'; // or 'production'

    if (!(await FlutterGooglePay.isAvailable(environment))) {
      _showToast(scaffoldContext, 'Google pay not available');
    } else {
      PaymentItem pm = PaymentItem(
          stripeToken: 'pk_test_1IV5H8NyhgGYOeK6vYV3Qw8f',
          stripeVersion: "2018-11-08",
          currencyCode: "usd",
          amount: "0.10",
          gateway: 'stripe');

      FlutterGooglePay.makePayment(pm).then((Result result) {
        if (result.status == ResultStatus.SUCCESS) {
          _showToast(scaffoldContext, 'Success');
        }
      }).catchError((dynamic error) {
        _showToast(scaffoldContext, error.toString());
      });
    }
  }

  _makeCustomPayment() async {
    var environment = 'rest'; // or 'production'

    if (!(await FlutterGooglePay.isAvailable(environment))) {
      _showToast(scaffoldContext, 'Google pay not available');
    } else {
      ///docs https://developers.google.com/pay/api/android/guides/tutorial
      PaymentBuilder pb = PaymentBuilder()
        ..addGateway("example")
        ..addTransactionInfo("1.0", "USD")
        ..addAllowedCardAuthMethods(["PAN_ONLY", "CRYPTOGRAM_3DS"])
        ..addAllowedCardNetworks(
            ["AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"])
        ..addBillingAddressRequired(true)
        ..addPhoneNumberRequired(true)
        ..addShippingAddressRequired(true)
        ..addShippingSupportedCountries(["US", "GB"])
        ..addMerchantInfo("Example");

      FlutterGooglePay.makeCustomPayment(pb.build()).then((Result result) {
        if (result.status == ResultStatus.SUCCESS) {
          _showToast(scaffoldContext, 'Success');
        } else if (result.error != null) {
          _showToast(context, result.error);
        }
      }).catchError((error) {
        //TODO
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: Builder(builder: (context) {
            scaffoldContext = context;
            return Center(
                child: Column(
              children: <Widget>[
                FlatButton(
                  onPressed: _makeStripePayment,
                  child: Text('Stripe pay'),
                ),
                FlatButton(
                  onPressed: _makeCustomPayment,
                  child: Text('Custom pay'),
                ),
              ],
            ));
          })),
    );
  }

  void _showToast(BuildContext context, String message) {
    final scaffold = Scaffold.of(context);
    scaffold.showSnackBar(SnackBar(
      content: Text(message),
      action: SnackBarAction(
        label: 'UNDO',
        onPressed: () {},
      ),
    ));
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_google_pay: ^0.1.4

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_google_pay/flutter_google_pay.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
73
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
86
Learn more about scoring.

We analyzed this package on Mar 30, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test