In order to achieve this, there are some core components that are provided to us by the Android Pay API. These really help to make it simple to implement the payment flow into our application, so let’s take a look.

The WalletFragment Class

Being a core part of the application payment implementation, there’s going to be a few mentions of the WalletFragment class over the next few sections. There are several different parts that make up the WalletFragment, so it would probably be helpful if we take a look at the functionality of them.

WalletFragment

This class is provided by the Android Pay API and is used to:

Handle user events in regards to the Android Pay components

Automate the Android Pay purchase lifecycle

There are two core components which an application would use an instance of the WalletFragment for, these are the:

Purchase Fragment—This instance of the fragment displays a purchase button to the user and sets up a MaskedWalletRequest which is to be sent to the Android Pay API. This fragment mode is instantiated using the WalletFragmentMode.BUY_BUTTON mode.

Confirmation Fragment — This instance of the fragment displays a set of change controls so that the user can change any details contained in the MaskedWallet. If any changes are made to the displayed details then the MaskedWallet is set with this new information. This fragment mode is instantiated using the WalletFragmentMode.SELECTION_DETAILS mode.

We can create a new instance of the WalletFragment either programatically:

When creating the instance programatically, we simply use the setMode() method with the Builder to state whether we wish the fragment to be of type BUY_BUTTON or SELECTION_DETAILS.

We can also achieve the same effect in our XML layout file:

When declaring the fragment instance via XML, we simply use the fragmentMode attribute to state whether we wish the fragment to be of type buyButton or selectionDetails.

Note: WalletFragment supports API version 12 and above. If you need to support lower versions, be sure to use the SupportWalletFragment.

WalletFragmentOptions & WalletFragmentStyle

When creating the WalletFragment programatically via the newInstance() method, we are required to pass in a WalletFragmentOptions instance to set the options for the fragment (such as styling, displayed controls etc). We can use the WalletFragmentOptions Builder to create a new instance like so:

As you can see there are several different properties we’re setting here, these are:

setEnvironment() — The environment to be used by the Wallet, this defaults to ENVIRONMENT_TEST and should be left at that until you have been granted access to the Android Pay production environment.

The environment to be used by the Wallet, this defaults to ENVIRONMENT_TEST and should be left at that until you have been granted access to the Android Pay production environment. setFragmentStyle() — This sets the style to be used for the fragment. This can be done either by calling setFragmentStyle() passing in a WalletFragmentStyle instance or the resource ID of the style to be used.

setTheme() — Sets the theme to be used by the WalletFragment, this can be either THEME_DARK or THEME_LIGHT .

or . setMode() — Sets the mode to be used for the WalletFragment, this can be either BUY_BUTTON or SELECTION_DETAILS.

As mentioned when using the setFragmentStyle() method of the Builder, we can pass in an instance of the WalletFragmentStyle class to set the style of the fragment. We can create a new instance of this like so:

Again, you can see there are several different builder methods being used here to set different properties on the WalletFragmentStyle instance:

setBuyButtonText() — Sets the text to be displayed on the button, this can be set to either BUY_WITH , DONATE_WITH or LOGO_ONLY .

, or . setBuyButtonAppearance() — Used to set the appearance of the Buy Button. This can be either ANDROID_PAY_DARK , ANDROID_PAY_LIGHT or ANDROID_PAY_LIGHT_WITH_BORDER .

, or . setBuyButtonWidth() — Used to set the width of the displayed Buy Button. We can set this to either MATCH_PARENT or WRAP_CONTENT.

There’s plenty of other methods belonging to the WalletFragmentStyle class that we can use when constructing the style for the fragment. Be sure to check them out in the full documentation here.

Now our WalletFragmentStyle instance has been setup to style our fragment, remember to pass it in when calling setFragmentStyle() method to build the WalletFragmentOptions instance!

WalletFragment Initialisation

Finally, before we can display the WalletFragment to our user we must initialize it using the initialize() method.

When doing so, we must prepare an WalletFragmentInitParams instance to pass in during initialization. Here we are required to set several different properties for use when initialization takes place: