Add Google Pay button to custom view in your Android app

  Last updated: 

 

Once you have configured the Google Pay manager, you can choose to add the Google Pay button in a custom view by adding the following code to your activity/fragment xml file:

//Add google pay custom view button to layout 
<com.trustpayments.mobile.ui.GooglePayButton
android:id="@+id/googlePayButton"
android:layout_width="match_parent"
android:layout_margin="30dp"
android:layout_height="wrap_content" />

 

For further customisation of the Google Pay button you can set the following options.

The first value shown is the default value (e.g. the buttonColor would default to black).

<com.trustpayments.mobile.ui.GooglePayButton
...
app:withShadow="true|false"
app:withText="true|false"
app:buttonColor="black|white"/>

 

Verify that Google Pay is supported by customer device & configuration is valid

Please ensure that you call the following method to verify that Google Pay is correctly configured and that the Google Pay button should be made visible to the customer:

tpGooglePayManager.possiblyShowGooglePayButton(object : TPGooglePayManager.ShowGooglePayButtonCallback {
override fun canShowButton(boolean: Boolean) {
if(boolean){
googlePayButton.visibility = View.VISIBLE
} else{
googlePayButton.visibility = View.GONE
}
}
})

 

Request a Google Wallet Token

Set up the Google Pay button to correctly display the Google Pay window:

googlePayButton.setOnClickListener {
tpGooglePayManager.requestPayment(valueInCents)
}

 

Get a Google Pay wallet token. The token is needed in the subsequent steps when performing a payment authorisation request.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == tpGooglePayManager.requestCode){
val googlePayWalletToken = tpGooglePayManager.onActivityResult(requestCode, resultCode, data)
//Use googlePayWalletToken for processing a payment authorisation
}
}

 

  Please proceed to the Requesting a payment authorisation page that details the approaches you can take to process the payment.

Was this article helpful?
0 out of 0 found this helpful