コード スニペット

次のいずれかのパスカテゴリを選択して、使用方法の詳細をご覧ください。


このセクションでは、クーポンのコード スニペットについて説明します。次のクラスとオブジェクトを扱います。

JSON OfferClass

OfferClass を定義したら、次の REST URI に対する POST リクエストを行って挿入します。

https://walletobjects.googleapis.com/walletobjects/v1/offerClass

strict=true パラメータを REST URI に追加して厳密なエラー解析を有効にし、重複している ID フィールドなどの追加のエラーを検出します。

https://walletobjects.googleapis.com/walletobjects/v1/offerClass?strict=true

次の例は、OfferClass を表す JSON リソースを示しています。JSON は POST リクエストの本文に含めて送信する必要があります。次のコードサンプルは、OfferClass を定義して挿入する方法を具体的に示しています。

リソース

{
  "kind": "walletobjects#offerClass",
  "id": "#{issuerId}.#{classId}",
  "issuerName": "Baconrista Coffee",
  "title": "20% off on one bacon fat latte",
  "redemptionChannel": "both",
  "provider": "Baconrista Deals",
  "titleImage": {
    "kind": "walletobjects#image",
    "sourceUri": {
      "kind": "walletobjects#uri",
      "uri": "https://farm4.staticflickr.com/3723/11177041115_6e6a3b6f49_o.jpg"
    }
  },
  "locations": [{
    "kind": "walletobjects#latLongPoint",
    "latitude": 37.424015499999996,
    "longitude": -122.09259560000001
    },{
    "kind": "walletobjects#latLongPoint",
    "latitude": 37.424354,
    "longitude": -122.09508869999999
    },{
    "kind": "walletobjects#latLongPoint",
    "latitude": 37.7901435,
    "longitude": -122.39026709999997
    },{
    "kind": "walletobjects#latLongPoint",
    "latitude": 40.7406578,
    "longitude": -74.00208940000002
  }],
  "reviewStatus": "underReview",
  "review": {
    "comments": "Real auto approval by system"
  },
  "textModulesData": [
    {
      "header": "Details",
      "body": "20% off one cup of coffee at all Baconrista Coffee locations. " +
                "Only one can be used per visit."
    },
    {
      "header": "About Baconrista",
      "body": "Since 2013, Baconrista Coffee has been committed to making high " +
                "quality bacon coffee. Visit us in our stores or online at www.baconrista.com"
    }
  ],
  "linksModuleData": {
    "uris": [
      {
        "kind": "walletobjects#uri",
        "uri": "https://maps.google.com/map?q=google",
        "description": "Nearby Locations"
      },
      {
        "kind": "walletobjects#uri",
        "uri": "tel:6505555555",
        "description": "Call Customer Service"
      }
    ]
  },
  "imageModulesData": [
    {
      "mainImage": {
        "kind": "walletobjects#image",
        "sourceUri": {
          "kind": "walletobjects#uri",
          "uri": "https://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg",
          "description": "Coffee beans"
        }
      }
    }
  ],
  "hexBackgroundColor": "#ffffff",
  "heroImage": {
   "kind": "walletobjects#image",
   "sourceUri": {
     "kind": "walletobjects#uri",
     "uri": "https://farm8.staticflickr.com/7302/11177240353_115daa5729_o.jpg"
    }
  }
}

Java

// Define the Image Module Data
List<ImageModuleData> imageModuleData = new ArrayList<ImageModuleData>();

ImageModuleData image = new ImageModuleData().setMainImage(
    new Image().setSourceUri(
        new ImageUri().setUri("http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg")));

imageModuleData.add(image);

// Define Links Module Data
List<Uri> uris = new ArrayList<Uri>();
Uri uri1 = new Uri().setDescription("Nearby Locations").setUri("http://maps.google.com/?q=google");
Uri uri2 = new Uri().setDescription("Call Customer Service").setUri("tel:6505555555");

uris.add(uri1);
uris.add(uri2);

LinksModuleData linksModuleData = new LinksModuleData().setUris(uris);

// Define Text Areas
List<TextModuleData> textModulesData = new ArrayList<TextModuleData>();

TextModuleData details = new TextModuleData().setHeader("Details").setBody(
    "20% off one cup of coffee at all Baconrista Coffee locations.  Only one can be used per visit.");
TextModuleData finePrint = new TextModuleData().setHeader("About Baconrista").setBody(
    "Since 2013, Baconrista Coffee has been committed to making high quality bacon coffee. Visit us in our stores or online at www.baconrista.com");

textModulesData.add(details);
textModulesData.add(finePrint);

// Define Geofence locations
List<LatLongPoint> locations = new ArrayList<LatLongPoint>();
locations.add(new LatLongPoint().setLatitude(37.422601).setLongitude(
    -122.085286));
locations.add(new LatLongPoint().setLatitude(37.424354).setLongitude(
    -122.09508869999999));
locations.add(new LatLongPoint().setLatitude(40.7406578).setLongitude(
    -74.00208940000002));

OfferClass wobClass = new OfferClass()
    .setId('2945482443380251551.ExampleOfferClass1')
    .setIssuerName("Baconrista Coffee")
    .setTitle("20% off one bacon fat latte")
    .setProvider("Baconrista Deals")
    .setTitleImage(
        new Image().setSourceUri(new ImageUri()
            .setUri("http://farm4.staticflickr.com/3723/11177041115_6e6a3b6f49_o.jpg")))
    .setRedemptionChannel("both")
    .setReviewStatus("underReview")
    .setLinksModuleData(linksModuleData)
    .setImageModulesData(imageModuleData)
    .setTextModulesData(textModulesData)
    .setLocations(locations).setMultipleDevicesAndHoldersAllowedStatus("multipleHolders");

OfferClass response = client.offerclass().insert(wobClass).execute();

PHP

// A list of locations at which the Wallet Class can be used.
$locations = array(
    array(
        'kind' => 'walletobjects#latLongPoint',
        'latitude' => 37.424015499999996,
        'longitude' => -122.09259560000001
    ),
    array(
        'kind' => 'walletobjects#latLongPoint',
        'latitude' => 37.424354,
        'longitude' => -122.09508869999999
    ),
    array(
        'kind' => 'walletobjects#latLongPoint',
        'latitude' => 37.7901435,
        'longitude' => -122.39026709999997
    ),
    array(
        'kind' => 'walletobjects#latLongPoint',
        'latitude' => 40.7406578,
        'longitude' => -74.00208940000002
    )
);
// Source uri of title image.
$uriTitleImageInstance = new Google_Service_Walletobjects_ImageUri();
$imageTitleImageInstance = new Google_Service_Walletobjects_Image();
$uriTitleImageInstance->setUri(
    'http://farm4.staticflickr.com/3723/11177041115_6e6a3b6f49_o.jpg'
);
$imageTitleImageInstance->setSourceUri($uriTitleImageInstance);
// Define text module data.
$textModulesData = array(
    array(
        'header' => 'Details',
        'body' => '20% off one cup of coffee at all Baconrista Coffee locations. ' .
            'Only one can be used per visit.'
    ),
    array(
        'header' => 'About Baconrista',
        'body' => 'Since 2013, Baconrista Coffee has been committed to making high ' .
            'quality bacon coffee. Visit us in our stores or online at www.baconrista.com'
    )
);
// Define links module data.
$linksModuleData = new Google_Service_Walletobjects_LinksModuleData();
$uris = array (
    array(
        'uri' => 'http://maps.google.com/map?q=google',
        'kind' => 'walletobjecs#uri',
        'description' => 'Nearby Locations'
    ),
    array(
        'uri' => 'tel:6505555555',
        'kind' => 'walletobjecs#uri',
        'description' => 'Call Customer Service'
    )
);
$linksModuleData->setUris($uris);

$uriModuleImageInstance = new Google_Service_Walletobjects_ImageUri();
$uriModuleImageInstance->setUri(
    'http://farm8.staticflickr.com/7401/11177116434_d8e600bba6_o.jpg'
);
$uriModuleImageInstance->setDescription('Coffee beans');
$imageModuleImageInstance = new Google_Service_Walletobjects_Image();
$imageModuleImageInstance->setSourceUri($uriModuleImageInstance);
$imagesModuleData = new Google_Service_Walletobjects_ImageModuleData();
$imagesModuleData->setMainImage($imageModuleImageInstance);
$imagesModuleDataArr = array ($imagesModuleData);

// Create wallet class.
$wobClass = new Google_Service_Walletobjects_OfferClass();
$offerClass->setId('2945482443380251551.OfferClass');
$wobClass->setIssuerName('Baconrista Coffee');
$wobClass->setTitle('20% off on one bacon fat latte');
$wobClass->setProvider('Baconrista Deals');
$wobClass->setTitleImage($imageTitleImageInstance);
$wobClass->setLinksModuleData($linksModuleData);
$wobClass->setTextModulesData($textModulesData);
$wobClass->setImageModulesData($imagesModuleDataArr);
$wobClass->setRedemptionChannel('both');
$wobClass->setReviewStatus('underReview');
$wobClass->setLocations($locations);
$wobClass->setMultipleDevicesAndHoldersAllowedStatus('multipleHolders');

$service->offerclass->insert($wobClass);

Python

api_object = generate_offer_class(
      '1234567', 'ExampleOfferClass')
api_request = service.offerclass().insert(body=api_object)
api_response = api_request.execute()

def generate_offer_class(issuer_id, class_id):
  offer_class = {
      'kind': 'walletobjects#offerClass',
      'id': '%s.%s' % (issuer_id, class_id),
      'issuerName': 'Baconrista Coffee',
      'title': '20% off one bacon fat latte',
      'redemptionChannel': 'both',
      'provider': 'Baconrista Deals',
      'titleImage': {
          'kind': 'walletobjects#image',
          'sourceUri': {
              'kind': 'walletobjects#uri',
              'uri': 'http://farm4.staticflickr.com/3723/11177041115_6e6a3b6f49_o.jpg'
          }
      },
      'multipleDevicesAndHoldersAllowedStatus': 'multipleHolders',
      'locations': [{
          'kind': 'walletobjects#latLongPoint',
          'latitude': 37.424015499999996,
          'longitude': -122.09259560000001
          },{
          'kind': 'walletobjects#latLongPoint',
          'latitude': 37.424354,
          'longitude': -122.09508869999999
          },{
          'kind': 'walletobjects#latLongPoint',
          'latitude': 37.7901435,
          'longitude': -122.39026709999997
          },{
          'kind': 'walletobjects#latLongPoint',
          'latitude': 40.7406578,
          'longitude': -74.00208940000002
      }],
      'reviewStatus': 'underReview',
      'review': {
          'comments': 'Real auto approval by system'
      },
      'textModulesData': [{
        'header': 'Details',
        'body': '20% off one cup of coffee at all Baconrista Coffee locations. ' +
                'Only one can be used per visit.'
       },{
        'header': 'About Baconrista',
        'body': 'Since 2013, Baconrista Coffee has been committed to making high ' +
                'quality bacon coffee. Visit us in our stores or online at www.baconrista.com'
      }],
      'linksModuleData': {
        'uris': [
          {
            'kind': 'walletobjects#uri',
            'uri': 'http://maps.google.com/map?q=google',
            'description': 'Nearby Locations'
          },{
            'kind': 'walletobjects#uri',
            'uri': 'tel:6505555555',
            'description': 'Call Customer Service'
          }]
      },
      'imageModulesData': [
        {
          'mainImage': {
            'kind': 'walletobjects#image',
            'sourceUri': {
              'kind': 'walletobjects#uri',
              'uri':  'http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg',
              'description': 'Coffee beans'
            }
          }
        }
      ],
    }
  return offer_class

JSON OfferObject

次のサンプルでは、OfferObject を定義します。

リソース

{
  "kind": "walletobjects#offerObject",
  "classId": "2945482443380251551.OfferClass",
  "id": "2945482443380251551.OfferObject",
  "state": "active",
  "barcode": {
    "kind": "walletobjects#barcode",
    "type": "upcA",
    "value": "123456789012",
    "alternateText": "12345"
  },
  "validTimeInterval": {
    "kind": "walletobjects#timeInterval",
    "start": {
      "date": "2013-06-12T23:20:50.52Z"
      },
    "end": {
      "date": "2013-12-12T23:20:50.52Z"
      }
  }
}

Java

// Define Barcode
Barcode barcode = new Barcode().setType("upcA").setValue("123456789012")
    .setAlternateText("12345");

// Define Wallet Object
OfferObject object = new OfferObject()
	.setClassId('2945482443380251551.ExampleOfferClass1')
    .setId('2945482443380251551.ExampleOfferObject1')
    .setBarcode(barcode)
    .setValidTimeInterval(new TimeInterval().setEnd(new DateTime().setDate(new com.google.api.client.util.DateTime(
            new Date().getTime() + 263000000000L))))
    .setState("active");

PHP

$barcode = new Google_Service_Walletobjects_Barcode();
$barcode->setType('upcA');
$barcode->setValue('123456789012');
$barcode->setAlternateText('12345');

$validTimeInterval = new Google_TimeInterval();
$startDateTime = new Google_DateTime();
$startDateTime->setDate('2013-06-12T23:20:50.52Z');
$validTimeInterval->setStart($startDateTime);
$endDateTime = new Google_DateTime();
$endDateTime->setDate('2013-12-12T23:20:50.52Z');
$validTimeInterval->setEnd($endDateTime);

// Create wallet object.
$offerObject = new Google_Service_Walletobjects_OfferObject();
$offerObject->setClassId('2945482443380251551.OfferClass');
$offerObject->setId('2945482443380251551.OfferObject');
$offerObject->setBarcode($barcode);
$offerObject->setValidTimeInterval($validTimeInterval);
$offerObject->setState('active');

Python

offer_object = {
      'kind': 'walletobjects#offerObject',
      'classId': '1234567.ExampleOfferClass',
      'id': '1234567.ExampleOfferObject',
      'state': 'active',
      'barcode': {
          'kind': 'walletobjects#barcode',
          'type': 'upcA',
          'value': '123456789012',
          'alternateText': '12345'
      }
  }

OfferObject を挿入する方法は 2 つあります。

1 つ目の方法は、次の REST URI に対して(定義されたオブジェクトを本文に含めた)POST リクエストを行うことです。

https://walletobjects.googleapis.com/walletobjects/v1/offerObject

2 つ目の方法は、OfferObject 定義から JWT を作成し、その JWT を [Google Pay に保存] ボタンに配置することです。詳細については、ウェブボタンを追加するをご覧ください。

Android API で OfferObject を追加する

次の例では、Android API ウォレット SDK パッケージを使用して、OfferObject を挿入します。

// Define Text Module Data
List textModulesData = new ArrayList();
TextModuleData textModuleData = new TextModuleData("Details", "20% off one coffee beverage." +
        "Offer valid 30 days from date of issue.  Not applicable with other offers.  Offer" +
        " must be presented at time of purchase.  Offer not valid on non coffee based beverages");
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://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.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 being a great customer.")
    .build();
messages.add(message);

// Define Geolocations

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

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

OfferWalletObject wob = OfferWalletObject
    .newBuilder()
    .setClassId("2951238800106304306.OfferClassMonster3")
    .setId("2951238800106304306.OfferTestObject1")
    .setState(WalletObjectsConstants.State.ACTIVE)
    .setIssuerName("Baconrista")
    .setTitle("20% off coffee")
    .setValidTimeInterval(new TimeInterval(time.getTime(), time.getTime() + 144000000L))
    .setBarcodeType("qrCode")
    .setBarcodeValue("28343E3")
    .setBarcodeAlternateText("12345")
    .addTextModulesData(textModulesData)
    .addLinksModuleDataUris(uris)
    .addInfoModuleDataLabelValueRows(rows)
    .addImageModuleDataMainImageUris(imageUris)
    .addMessages(messages)
    .addLocations(locations)
    .build();

return wob;
}
}

詳細については、Google Pay にパスを保存するをご覧ください。