Skip to main content

Google Pay with Mobile SDK

To enable Google Pay in PayU | Mobile you should have configurated POS with Google Pay enabled on PayU backend as well as implemented two modules:

payment-library-google-pay-module - supports Google Pay payments in PayU on Android platform. To fully utilize this feature please contact PayU support to turn on Google Pay payment on PosId assigned to you.

payment-library-google-pay-adapter - should be used to enable Google Pay payments on the payment method list. (This module is strongly dependent on payment-library-chooser-module and payment-library-google-pay-module and cannot be used without them.)

Use of Google Pay requires:

  • Android version >=19,
  • Google Play services version >=11.4.x,
  • Google account linked on given device.

Integration

GooglePayService class is the main entry point for Integration with Google Pay Android API. There are three main steps that should be performed:

To perform sample test payment, proper google account with at least one production card added to Google Pay (payments.google.com) should be logged in the test device. For payment process this card should be selected. When sandbox environment is selected in the configuration, Google Pay SDK returns test token with no real information about card, so the card will never be charged.

<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="TypographyDashes">
<string name="payu_language">auto</string> // This will not be required in future releases. Keep it for now
<string name="payu_environment">sandbox</string>
</resources>

Checking if Google Pay payment can be performed on the device.

This step should be performed prior to showing Google Pay as payment method. Google Pay should only be shown as possible payment method after receiving confirmation that Google Pay can be used on the device.

To check if Google Pay can be used on the device GooglePayService#isReadyToPay method should be called.

public void isReadyToPay(@NonNull final GooglePayVerificationListener isGooglePayPossibleListener) {
googlePayHandler.isReadyToPay(isGooglePayPossibleListener);
}

Param isGooglePayPossibleListener handle verifying device response from Google API.

Requirements

See Google requirements for the device.

GooglePayVerificationListener passed as an argument to this method is responsible for passing results to the the application. This listener has two methods for the merchant to implement:

  • GooglePayVerificationListener#onVerificationCompleted,
  • GooglePayVerificationListener#onException
public interface GooglePayVerificationListener {
/**
* Method called when Google Pay status has been obtained
*
* @param verificationStatus - The status of the verification. Google Pay payments should be performed only
on
* {@linkplain GooglePayVerificationStatus#SUCCESS}
*/
void onVerificationCompleted(GooglePayVerificationStatus verificationStatus);
/**
* There was an exception when trying to connect to Google API
*/
void onException(@NonNull Exception exception);
}

This listener should be implemented by merchant and should be passed to GooglePayService#isReadyToPay(GooglePayVerificationListener)

Google Pay Docs

For more information check: Google Pay documentation isReadyToPay API

Enum with Google Pay verification statuses. Only SUCCESS should enable Google Pay payment.

public enum GooglePayVerificationStatus {
/**
* Verification of Google Pay availability was successful. GooglePay can be used as Payment method
*/
SUCCESS,
/**
* Android version is to low. Google Pay is available from Android 4.4+
*/
ERROR_API_VERSION,
/**
* Google Play services version is to low. User need to upgrade it before continuing
*/
ERROR_GOOGLE_PLAY_SERVICES_VERSION,
/**
* Google Play services are not available. User need to install them before continuing
*/
ERROR_GOOGLE_PLAY_SERVICES_UNAVAILABLE,
/**
* Unknown error is preventing user from making Google Pay payment
*/
ERROR_UNKNOWN
}

payment-library-google-pay-adapter

This module ads GP payment on Payment-library-chooser-module and should be visible on main select screen. To integrate it follow integration steps for payment-library-chooser-module. Make sure that there is Google Pay payment method in the retrieve call.

To enable Google Pay on Payment method list use following code in the Activity#onCreate:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
...
googlePayService = new GooglePayService(this);
paymentChooserWidget.setPaymentMethodsAdapter(new GooglePayAdapter(googlePayService));
...
}

Requesting Google Pay payment

Requesting Google Pay payment is the most important method of this module. Parameters passed as the arguments are used for Google Pay token creation. PosId has to be passed as well in order assign payment to proper POS.

Cart object should be created using Cart.Builder class as shown below. The price is positive integer. Supported currencies are:

  • enum Currency.PLN,
  • enum Currency.CZK,
  • enum Currency.EUR,
  • enum Currency.GBP,
  • enum Currency.USD,
  • enum Currency.DKK,
  • enum Currency.NOK,
  • enum Currency.SEK.
private Cart createCart() {
return new Cart.Builder()
.withTotalPrice(1000) //10.00 as an integer
.withCurrency(Currency.PLN)
.build();
}

To start payment process call GooglePayService#requestGooglePayCard() method. This method takes Cart and PosId as arguments and can take boolean shouldCardPickerBeDisplayed - sets whether the UI you show the payment method or shipping address selection is required to be shown in the purchase flow, default value is set to true, if you would like to hide Google Pay selector UI check this page.

/**
* Requesting GooglePay Card. UI with selecting card will always be shown.
* Call this method after {@linkplain GooglePayService#isReadyToPay}
* Result will be passed to {@linkplain Activity#onActivityResult}
*
* @param cart - Cart object with the information about Currency and payment amount
* @param posId - PosId used for this payment. To learn more about POS please check
*/
public void requestGooglePayCard(@NonNull Cart cart, @NonNull String posId) {
}

Or

/**
* Requesting GooglePay Card.
* Call this method after {@linkplain GooglePayService#isReadyToPay}
* Result will be passed to {@linkplain Activity#onActivityResult}
*
* @param cart - Cart object with the information about Currency and payment amount
* @param posId - PosId used for this payment. To learn more about POS please check
*
* @param shouldCardPickerBeDisplayed - Sets whether the UI to show the payment method or shipping
* address selection is required to be shown in the purchase flow
*/
public void requestGooglePayCard(@NonNull Cart cart, @NonNull String posId, boolean shouldCardPickerBeDispl
ayed) {
}

PaymentDataRequest.Builder - Google official documentation.

Call example
googlePayService.requestGooglePayCard(createCart(), "posId");

Parsing Result to Receive Encoded Google Pay Token

Results (either successful, canceled or failed) of GooglePayService#requestGooglePayCard() call are passed to Activity#onActivityResult() method.

To proceed with Google Pay process override onActivityResult() in your calling activity. If requestCode is equal to GooglePayService#REQUEST_CODE_GOOGLE_PAY_PAYMENT, response can be parsed to obtain results.

Checking resultCode would help to identify whether the action has been performed successfully or not.

resultCode parameter values
Parameter NameDescription
Activity.RESULT_OK
This indicates successfully Google Pay card retrieval. Data intent should be passed to GooglePayService#handleGooglePayResultData.
Activity.RESULT_CANCELED
User has canceled Google Pay payment process.
GooglePayService.RESULT_ERROR
An error has occurred during payment process.

To obtain encoded token, data intent should be passed to GooglePayService#handleGooglePayResultData.

Success response
/**
* Call this method in {@linkplain Activity#onActivityResult} result for {@link #REQUEST_CODE_GOOGLE_PAY_PAY
MENT} request code.
* <p>
* return {@link GooglePayTokenResponse} with payment token or null, if there is no token in response
*/
@Nullable
public GooglePayTokenResponse handleGooglePayResultData(@NonNull Intent data) {
return googlePayHandler.handleGooglePay(data);
}

GooglePayTokenResponse would be returned, encapsulating encoded token. This token should be sent to PayU backend in the create order request.

PayU backend might ask for additional 3DS verification as a result of order creation. In this case please use WebPaymentModule to support this payment.

In case of receiving error response from Google Pay, ErrorStatus with additional data can be obtained by calling googlePayService#handleGooglePayErrorStatus.

Error Response
/**
* Handle error response from Google Pay API
*
* @param data - Intent data from {@linkplain Activity#onActivityResult} resultCode {@linkplain #RESULT_ERROR}
* @return ErrorStatus mapped from Google Pay Status.
*/
public ErrorStatus handleGooglePayErrorStatus(Intent data) {
Status statusFromIntent = AutoResolveHelper.getStatusFromIntent(data);
return ErrorStatus.fromGooglePayStatus(statusFromIntent);
}

ErrorStatus contains error codes from Google Pay Service. For more information about possible status codes please see StatusCodes.

Sample Code - Parse results
if (requestCode == GooglePayService.REQUEST_CODE_GOOGLE_PAY_PAYMENT) {
if (resultCode == Activity.RESULT_OK) {
GooglePayTokenResponse googlePayTokenResponse = googlePayService.handleGooglePayResultData(data);
} else if (resultCode == RESULT_CANCELED) {
//User has canceled payment
} else if (resultCode == GooglePayService.RESULT_ERROR) {
//Payment process has failed for the user
ErrorStatus status = googlePayService.handleGooglePayErrorStatus(data);
}
}

Production Environment

Google Pay payments won't work out of the box on production environment. It needs additional configuration:

  • configure production POS (please contact PayU support or your business partner) to utilize new payment gate
  • contact Google for verification of mobile application
  • additional information: gateway - PayU, Merchant_ID = Gateway_Merchant_ID = POS

In case when there is a dialog: "Realizacja żądania nie powiodła się. Ten sprzedawca nie obsługuje GooglePay" application is configured for production (property "production" was set on mobile device), but app did not pass Google certification process (please contact Google Support). There are some merchant that prefer only this kind of payments so with using only Google Pay payments module payment path is really simple.