Old Android SDK

This document includes instructions for the previous version of the Android SDK. For information on how to access and use the current version, read Get access to the Android SDK and Use the Android SDK.

Get access to the Android SDK (old version)

After you completed the steps required to access the current Android SDK, you needed to tie the Android API to your Google Pay API for Passes account.

Your Google Pay API for Passes account should have been created for you by your Google point of contact. The Google Pay API for Passes Merchant Center is a website that you can use to manage your account and all of your associated classes and objects. To allow the Android API to access your account, share the account with the email sugar-s2w-prod@system.gserviceaccount.com, as shown in the following image:


Save passes to Google Pay with the old Android SDK

The Android API facilitates saving Pass in Google Pay. By integrating the Save to Google Pay button in your app, you make it easy for your customers to save their Pass to Google Pay.

The following steps outline how to add the Save to Google Pay button for the Loyalty Pass.

1. Add the Save to Google Pay button to your UI

The first step is to add the Save to Google Pay button to your app. Google Pay provides an Android SDK button for you to integrate into your app. The button assets are available in the Brand guidelines.

This toolkit contains vector images of the buttons.

To incorporate a button in your application, copy over the button image from the toolkit into the res folder of your application and add the following code to your Android layout file. Note that each button requires its unique contentDescription string and minWidth value in addition to the correct value for src.

<ImageButton
             android:layout_width="match_parent"
             android:layout_height="48dp"
             android:minWidth="200dp"
             android:clickable="true"
             android:src="@drawable/s2ap" />

The layout_height for the button is 48 dp and minWidth must be 200 dp.

2. Create a loyalty class

A class can be created from the Google Pay API for Passes Merchant Center. From the Classes page in the Merchant Center, select Create Class to create the loyaltyclass. Input fields with a red outline are required, while the remaining are optional. For any fields with a URL, make sure the link is publicly accessible. The created class will be referenced when creating the valuable object.

3. Create a loyalty object

The next step is to create the loyalty object. Following is an example of creating a loyalty object using the builder pattern:

// Define Points
LoyaltyPoints points = LoyaltyPoints.newBuilder()
    .setLabel("Points")
    .setType("points")
    .setBalance(LoyaltyPointsBalance.newBuilder().setString("500").build()).build();

// Define Text Module Data
List textModulesData = new ArrayList();
TextModuleData textModuleData = new TextModuleData("Jane's Baconrista Rewards", "Save more at your local Mountain View store Jane.  You get 1 bacon fat latte for every 5 coffees purchased.  Also just for you, 10% off all pastries in the Mountain View store.");
textModulesData.add(textModuleData);

// Define Links Module Data
List uris = new ArrayList();
UriData uri1 = new UriData("http://www.baconrista.com/myaccount?id=1234567890","My Baconrista Account");
uris.add(uri1);

List imageUris = new ArrayList();
UriData uri2 = new UriData("http://examplesite/images/exampleimage2.jpg", "Image Description");
imageUris.add(uri2);

// Define Info Module
List row0cols = new ArrayList();
LabelValue row0col0 = new LabelValue("Next Reward in","2 coffees");
LabelValue row0col1 = new LabelValue("Member Since", "01/15/2013");
row0cols.add(row0col0);
row0cols.add(row0col1);

List row1cols = new ArrayList();
LabelValue row1col0 = new LabelValue("Local Store", "Mountain View");
row1cols.add(row1col0);

List rows = new ArrayList();
LabelValueRow row0 = LabelValueRow.newBuilder().addColumns(row0cols).build();
LabelValueRow row1 = LabelValueRow.newBuilder().addColumns(row1cols).build();

rows.add(row0);
rows.add(row1);

// Define general messages
List messages = new ArrayList();
WalletObjectMessage message =  WalletObjectMessage.newBuilder()
    .setHeader("Hi Jane!")
    .setBody("Thanks for joining our program. Show this message to " +
        "our barista for your first free coffee on us!")
    .build();
messages.add(message);

// Define Geolocations

LatLng location = new LatLng(37.422601, -122.085286);

List locations = new ArrayList();
locations.add(location);

LoyaltyWalletObject wob = LoyaltyWalletObject
    .newBuilder()
    .setClassId("2967745143867465930.LoyaltyClass")
    .setId("2967745143867465930.LoyaltyObject")
    .setState(WalletObjectsConstants.State.ACTIVE)
    .setAccountId("1234567890")
    .setAccountName("Jane Doe")
    .setIssuerName("Baconrista")
    .setProgramName("Baconrista Rewards")
    .setBarcodeType("qrCode")
    .setBarcodeValue("28343E3")
    .setBarcodeAlternateText("12345")
    .setLoyaltyPoints(points)
    .addTextModulesData(textModulesData)
    .addLinksModuleDataUris(uris)
    .addInfoModuleDataLabelValueRows(rows)
    .addImageModuleDataMainImageUris(imageUris)
    .addMessages(messages)
    .addLocations(locations)
    .build();

This object is the first parameter taken by the createWalletObject call described in the next section.

4. Initialize the Request to save the loyalty object

The class walletObjectsClient serves as the entry point for Wallet objects functionality.

The following code snippet illustrates how an app can instantiate a client for saving an object to Google Pay:

CreateWalletObjectsRequest request = new CreateWalletObjectsRequest(wob);
Wallet.WalletOptions walletOptions = new Wallet.WalletOptions.Builder()
        .setTheme(WalletConstants.THEME_LIGHT)
        .setEnvironment(WalletConstants.ENVIRONMENT_PRODUCTION)
        .build();

walletObjectsClient = Wallet.getWalletObjectsClient(this, walletOptions);
Task task = walletObjectsClient.createWalletObjects(request);
AutoResolveHelper.resolveTask(task, this, SAVE_TO_ANDROID);

You can also use the GoogleApiClient to initialize a request:

Wallet.WalletObjects.createWalletObjects(googleApiClient, request, SAVE_TO_ANDROID);

5. Handle onActivityResult

Define onActivityResult to react to the success, cancellation, or failure of the operation as shown:

public void onActivityResult(int requestCode, int resultCode, Intent data){
  EditText textBox = (EditText) findViewById(R.id.s2wResponse);

  switch(requestCode){
    case SAVE_TO_ANDROID:
      switch (resultCode) {
        case Activity.RESULT_OK:
          textBox.setText("saved");
          break;
        case Activity.RESULT_CANCELED:
          textBox.setText("canceled");
          break;
        default:
          int errorCode =
              data.getIntExtra(
                  WalletConstants.EXTRA_ERROR_CODE, -1);
          textBox.setText("failed error code: " + errorCode);
          break;
      }

Old Android API quickstart for Google Pay API for Passes

The Android API for Google Pay API for Passes lets users save Valuable Objects to Google Pay with the click of a button. This sample quickstart provides easy setup for saving loyalty, gift card, and offer objects.

Follow the Android SDK guide and then the following steps to run the sample android app:

Step 1: Set up the Android App

The quickstart is an open source project that you can download from GitHub. To download this project:

  1. Visit the GitHub repo.
  2. Either download the zip or clone the repository by running git clone https://github.com/google-pay/s2gp-quickstart-android.

The sample app can then be opened with Android Studio as an existing project. The project may need to be synced to compile correctly. Once the sample app is built on Android Studio, it can be run an Android device or an emulator.

Step 2: Create a class

A class can be created from the Google Pay API for Passes Merchant. From the 'Classes' page in the Merchant Center, select 'Create Class' to create the class. Input fields with a red outline are required, while the remaining are optional. For any fields with a URL, make sure the link is publicly accessible. The created class will be referenced when creating the valuable object.

Step 3: Configure the Android App

To configure the sample app so it works properly, certain values must be adjusted. Within strings.xml found in app/res/values/:

  1. Replace the ISSUER_ID value with the your Google Pay API for Passes Issuer Id.
  2. Replace LOYALTY_CLASS_ID, GIFTCARD_CLASS_ID, or OFFER_CLASS_ID with the name of the class of interest already created in your Google Pay API for Passes Merchant Center.

Within MainActivity.java certain values for each object type must be configured so that they match the class the object references in the Merchant Center:

  1. For a GiftCardWalletObject in the generateGiftCardWalletObject method, setIssuerName and setTitle must match the values in the class.
  2. For a LoyaltyWalletObject in the generateLoyaltyWalletObject method, setIssuerName and setProgramName must match the values in the class.
  3. For a OfferWalletObject in the generateLoyaltyWalletObject method, setIssuerName and setTitle must match the values in the class.

Step 4: Test the Android App

The application can be run either through an emulator or a physical Android device. Once the gradle is synced properly, run the app through Android Studio and select desired deployment target. Once the deployment finishes, the sample app should automatically launch.


Old reference documentation

The following is a list of the relevant methods for Google Pay API for Passes:

Object names
CreateWalletObjectsRequest Parcelable representing request to create wallet objects.
GiftCardWalletObject Parcelable representing a gift card object.
LoyaltyWalletObject Parcelable representing a loyalty card object.
OfferWalletObject Parcelable representing a offer object.
Wallet The main entry point for Google Wallet integration. You need to build a GoogleApiClient using the API and the appropriate Wallet.WalletOptions.
WalletConstants Collection of constant values used by the ClientLibrary.