选择以下一种卡券类别可详细了解其使用方式。
此部分包含优惠的代码段。它涵盖以下类和对象:
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 资源。必须在 POST
请求的正文中发送 JSON。以下代码示例详细演示了如何定义和插入 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
有两种方法。
第一种方法是向以下 REST URI 发送 POST
请求(在请求正文中定义对象):
https://walletobjects.googleapis.com/walletobjects/v1/offerObject
第二种方法是根据 OfferObject
定义创建 JWT,并将 JWT 放入保存到 Google Pay 按钮。如需了解详情,请参阅在网页上添加按钮。
使用 Android API 添加 OfferObject
您可以使用 Android API 电子钱包 SDK 软件包插入以下示例 OfferObject
:
// Define Text Module Data ListtextModulesData = 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。