Google Pay

Google Pay

The Google Pay offers a quick and easy path to enable secure, one touch payments in your app.
This guide explains how to process payments with our SDK.

Requirements

  • A Mobile SDK integration (Ready-to-Use UI or SDK & Your Own UI)
  • A Google account, when you're ready to deploy your Google Pay integration, sign-up to get access and test with production credentials.

Configuration

Open the build.gradle file in the app module and add the following to the dependencies block:

dependencies {
    implementation 'com.google.android.gms:play-services-wallet:16.0.1'
    implementation 'com.android.support:appcompat-v7:28.0.0'
}

Adding the Google Pay to your app must be done in one of two ways, depending on whether you are using the Ready-to-Use UI or the SDK & Your Own UI. These two ways are covered in the sections below. Please follow the instructions relevant to the approach you have chosen.

Ready-to-Use UI

Add GOOGLEPAY payment brand

Create the CheckoutSettings, and add the GOOGLEPAY to the payment brands list:

Set<String> paymentBrands = new HashSet<String>();
paymentBrands.add("GOOGLEPAY");

CheckoutSettings checkoutSettings = new CheckoutSettings(checkoutId, paymentBrands, Connect.ProviderMode.TEST);

If you integrate the Google Pay using our drop-in buttons, set the GOOGLEPAY payment brand in the PaymentButtonFragment.

paymentButtonFragment.setPaymentBrand("GOOGLEPAY");

Configure PaymentDataRequest

Create PaymentDataRequest to configure Google Pay widget with the transaction info, allowed payment methods and card networks.
Use GooglePayHelper method to easily create base PaymentDataRequest.Builder with all required parameters.

Then you can add some optional configurations, e.g. enable requesting shipping info in the widget. Please refer to the Google Pay Payments API documentation for more options.

Integer[] paymentMethods = new Integer[] {
        WalletConstants.PAYMENT_METHOD_CARD,
        WalletConstants.PAYMENT_METHOD_TOKENIZED_CARD
};

Integer[] cardNetworks = new Integer[] {
        WalletConstants.CARD_NETWORK_VISA,
        WalletConstants.CARD_NETWORK_MASTERCARD,
        WalletConstants.CARD_NETWORK_AMEX
};

PaymentDataRequest.Builder paymentDataRequestBuilder = GooglePayHelper.preparePaymentDataRequestBuilder(
        "100.00",
        "USD",
        YOUR_ENTITY_ID,
        paymentMethods,
        cardNetworks
);

Call build() method to get a final configuration object and pass it to the CheckoutSettings.

checkoutSettings.setGooglePayPaymentDataRequest(paymentDataRequestBuilder.build());

Collecting shopper information

You can collect additional information with the Google Pay widget before submitting the transaction, e.g. shipping information.

Configure payment data request

In addition to the previous steps complete configuration of the PaymentDataRequest.Builder object to request additional information from the shopper. Refer to the Payments API to see the full list of options.

paymentDataRequestBuilder.setShippingAddressRequired(true);
paymentDataRequestBuilder.setEmailRequired(true);

Configure receiving callbacks from the checkout

The CheckoutActivity may send the callback CheckoutActivity.ACTION_ON_BEFORE_SUBMIT when shopper submits the payment.
To receive it you should complete the following steps:

1. Create your broadcast receiver to listen the intents from CheckoutActivity.
See below how to receive PaymentData object with the requested data from the Google Pay widget. Refer to the Payments API for the class definition.

public class CheckoutBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (CheckoutActivity.ACTION_ON_BEFORE_SUBMIT.equals(action)) {
            String paymentBrand = intent.getStringExtra(CheckoutActivity.EXTRA_PAYMENT_BRAND);
            String checkoutId = intent.getStringExtra(CheckoutActivity.EXTRA_CHECKOUT_ID);
            
            /* Collect shopper information from the Google Pay */
            PaymentData paymentData = intent.getParcelableExtra(CheckoutActivity.EXTRA_GOOGLE_PAY_PAYMENT_DATA);

            ComponentName senderComponentName = intent.getParcelableExtra(
                    CheckoutActivity.EXTRA_SENDER_COMPONENT_NAME);

            /* Return control back to the CheckoutActivity to submit the payment */
            intent = new Intent(CheckoutActivity.ACTION_ON_BEFORE_SUBMIT);
            intent.setComponent(senderComponentName);
            intent.setPackage(senderComponentName.getPackageName());
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(CheckoutActivity.EXTRA_CHECKOUT_ID, checkoutId);

            context.startActivity(intent);
        }
    }
}

2. Declare your broadcast receiver in AndroidManifest.xml.

<receiver
    android:name=".CheckoutBroadcastReceiver"
    android:exported="false" />

NOTE: It is important to set android:exported="false" to your broadcast receiver. In this case the only messages the broadcast receiver can receive are those sent by components of the same application or applications with the same user ID.

3. To provide the best security our SDK uses directed broadcasts. This way our broadcast is received only by the specified BroadcastReceiver. For this reason you should add ComponentName of your receiver to CheckoutActivity intent.

CheckoutSettings settings = new CheckoutSettings(...);
ComponentName receiverComponentName = new ComponentName("yourPackageName", "yourReceiverClassName");
Intent intent = checkoutSettings.createCheckoutActivityIntent(this, receiverComponentName);

startActivityForResult(intent, CheckoutActivity.REQUEST_CODE_CHECKOUT);

SDK & Your Own UI

Follow this tutorial to integrate the Google Pay into your UI.
Use the following parameters:

gateway = "aciworldwide"
gatewayMerchantId = "YOUR_ENTITY_ID"
To make a transaction create the GooglePayPaymentParams with a received token:

PaymentParams paymentParams = new GooglePayPaymentParams(checkoutId, token);

And submit the transaction:

Transaction transaction = null;

try {
    transaction = new Transaction(paymentParams);
    binder.submitTransaction(transaction);
} catch (PaymentException ee) {
    /* error occurred */
}