Google Pay in Flutter
What is Google Pay?
Google Pay (known as G Pay) is a digital wallet platform and online payment system developed by Google to power in-app, online, and in-person contactless purchases on mobile devices. Users in the United States and India can also use an iOS device, albeit with limited functionality. In addition to this, the service also supports passes such as coupons, boarding passes, campus ID cards, car keys, event tickets, movie tickets, public transportation tickets, store cards, health records, and loyalty cards.
How it works
When a user taps the Google Pay button in any app, they see a payment sheet that displays the payment methods saved to their Google Account. Users can quickly select a payment method, add an optional shipping address, or add new information before finalizing the payment.
Google Pay payment flow:
G Pay integrates with these payment processors (PSPs).
Integration of Google Pay
We will be looking at the pay package for the integration of Google Pay. This package takes care of both Apple Pay and Google Pay.
Under the hood, this package uses the Google Pay API. The things required to set up the Pay API will eventually be required to integrate the pay package.
Setup:
Now, let's set up your code.
Internally, the Google API requires the following Gradle dependency (from android native) which is handled by the pay package:
implementation "com.google.android.gms:play-services-wallet:18.0.0"
Hence, we need to change the following in the build.gradle of your app:
Now you have access to the GooglePayButton and the ApplePayButton.
GooglePayButton
GooglePayButton is a Flutter widget to show the Google Pay button according to the rules and constraints specified in PayButton.
This widget provides an alternative UI-based integration path that wraps the API calls of the payment libraries and includes them as part of the lifecycle of the widget. Hence:
For instance, if the code is running on iOS, then ApplePayButton will be shown (provided you have set up the ApplePay configuration).
This is what GooglePayButton looks like:
Note: GooglePayButton extends from PayButton
Payment Configuration
In the above snippet, we see PaymentConfiguration, which is a required parameter. This parameter holds the information about a payment transaction.
It loads the configuration information needed for the payment transaction, which is based on the source. There are two options available for this:
Inside PaymentConfiguration
The PaymentConfiguration is a JSON in which each parameter stands for a request object as per the Google Pay API.
Recommended by LinkedIn
Payment Method
This comprises 3 properties:
TokenizationSpecification
Note: The values for gateway depend on the supported gateway.
CardParameter
Note: We can only use these two under a TEST environment.
BillingAddressParameters
Payment Results
PaymentMethodData
If the selection is successful, the result is fulfilled with a PaymentData object that includes relevant information about the payment method selected:
The billing address is present in the form of an Address Object. CardDetails are present in a string that represents the details of the card. This value is commonly the last four digits of the selected payment account number.
CardNetwork is also a string that represents the payment card network of the selected payment. This value is one of the values present inside the format of allowedCardNetworks in CardParameters.
PaymentMethodTokentizationData
After Payment Result
You can now use this payment method information to perform the actual transaction. For instance, you can get the tokenizationData from the above response and extract the token from it.
Then, use this token to perform payment through your payment gateway. Check out the list of supported processors to find out more about specific implementation details on each processor.
UI Elements
GooglePayButton exposes one of the parameters called type (which is an enum) inside its definition. We can play with this param in order to display the Button as per the requirements. Following are the options provided:
If you prefer to have more control over each individual request separately from the button, you can instantiate a payment client and add the buttons to your layout independently using RawGooglePayButton.
The result of this button is:
Once the user clicks this button, you can initiate the request using Pay.withAssets, which is an alternative to a Pay object with a list of configurations in String format.
The response, which is the Google Pay token, can be sent to your server / PSP.
Use Pieces to Store Your Flutter Snippets
When developing Flutter applications, you may have tons of widgets you save that you want to reuse later, but you just do not have them in a safe place where you can access them. There also may be the scenario where you are combing through Flutter and Dart documentation, and you want to save examples that come in handy when implementing a new feature or figuring out which widget to use for different circumstances.
Pieces helps you save all your useful code snippets efficiently through a desktop application and integrations. Using Pieces, you can save any code snippets from StackOverflow with the click of a button using the chrome extension, have your code autosaved from locally-hosted ML algorithms that recognize your code patterns, auto-classify snippets by language, share code with others using generated links, and more! The Pieces’ suite is continuously being developed, and there’s some groundbreaking stuff that is being put together to share, reuse, and save code snippets.