パスはさまざまな用途で利用できますが、それらすべてに共通する使用方法があります。たとえば、ポイントカード、ギフトカード、クーポン、イベント チケット、搭乗券、乗車券はすべて、次の方法で Google Pay アプリに追加できます。
ウェブボタンを追加する
貴社のウェブサイトに [Google Pay に保存] ボタンを追加し、ユーザーがポイントカード、ギフトカード、クーポン、イベント チケット、搭乗券、乗車券を Google Pay アプリに保存できるようにします。
ユーザーがこのボタンをクリックすると、該当するパスを表す JSON Web Token(JWT)が Google サーバーに送信されます。Google サーバーはその JWT に基づいて Object
リソースを作成し、そのリソースをユーザーのアカウントに接続します。
[Google Pay に保存] ボタンは、次の 3 つの手順でウェブサイトに統合できます。
- オブジェクトを拡張する
Class
を定義する。 Object
を表す JWT を生成する。- [Google Pay に保存] ボタンをウェブページに追加する。
次のステップではポイントパスを使用しますが、プロセスはすべてのパスで共通です。
1. オブジェクトを拡張するクラスを定義する
最初に LoyaltyClass
を定義します。LoyaltyClass
を挿入するには、次の REST URI に POST
リクエストを送信します。
https://walletobjects.googleapis.com/walletobjects/v1/loyaltyClass
厳密なエラー解析を有効にして ID フィールド重複などのエラーも検出するには、次の例のように strict=true
パラメータを REST URI に追加します。
https://walletobjects.googleapis.com/walletobjects/v1/loyaltyClass?strict=true
次の例は、LoyaltyClass
を表す JSON リソースを示しています。JSON は POST
リクエストの本文に含めて送信する必要があります。次のコードサンプルは、LoyaltyClass
を定義して挿入する方法を具体的に示しています。
リソース
{ "accountIdLabel": "Member Id", "accountNameLabel": "Member Name", "id": "2945482443380251551.ExampleClass1", "issuerName": "Baconrista", "kind": "walletobjects#loyaltyClass", "textModulesData": [ { "header": "Rewards details", "body": "Welcome to Baconrista rewards. Enjoy your rewards for being a loyal customer. " + "10 points for every dollar spent. Redeem your points for free coffee, bacon and more!" } ], "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" } } } ], "messages": [{ "header": "Welcome to Banconrista Rewards!", "body": "Featuring our new bacon donuts.", "kind": "walletobjects#walletObjectMessage" }], "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 }], "programLogo": { "kind": "walletobjects#image", "sourceUri": { "kind": "walletobjects#uri", "uri": "https://farm8.staticflickr.com/7340/11177041185_a61a7f2139_o.jpg" } }, "programName": "Baconrista Rewards", "rewardsTier": "Gold", "rewardsTierLabel": "Tier", "reviewStatus": "underReview", "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 Text Module Data List<TextModuleData> textModulesData = new ArrayList<TextModuleData>(); TextModuleData textModuleData = new TextModuleData().setHeader("Rewards details") .setBody( "Welcome to Baconrista rewards. Enjoy your rewards for being a loyal customer. 10 points for ever dollar spent. Redeem your points for free coffee, bacon and more!"); textModulesData.add(textModuleData); // 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 general messages List<Message> messages = new ArrayList<Message>(); Message message = new Message() .setHeader("Welcome to Baconrista") .setBody("Featuring our new bacon donuts."); messages.add(message); // 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)); // Create class LoyaltyClass wobClass = new LoyaltyClass() .setId('2945482443380251551.ExampleClass1') .setIssuerName("Baconrista") .setProgramName("Baconrista Rewards") .setProgramLogo( new Image().setSourceUri(new ImageUri() .setUri("http://farm8.staticflickr.com/7340/11177041185_a61a7f2139_o.jpg"))) .setRewardsTierLabel("Tier").setRewardsTier("Gold") .setImageModulesData(imageModuleData) .setTextModulesData(textModulesData) .setLinksModuleData(linksModuleData) .setAccountNameLabel("Member Name").setAccountIdLabel("Member Id") .setMessages(messages) .setReviewStatus("underReview").setMultipleDevicesAndHoldersAllowedStatus("multipleHolders") .setLocations(locations); LoyaltyClass response = client.loyaltyclass().insert(wobClass).execute();
PHP
// Define text module data. $textModulesData = array( array( 'header' => 'Rewards details', 'body' => 'Welcome to Baconrista rewards. Enjoy your rewards for being a loyal customer. ' . '10 points for every dollar spent. Redeem your points for free coffee, bacon and more!' ) ); // 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://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.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); // Messages to be displayed to all users of Wallet Objects. $messages = array(array( 'header' => 'Welcome to Banconrista Rewards!', 'body' => 'Featuring our new bacon donuts.', 'kind' => 'walletobjects#walletObjectMessage' )); $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 program logo. $uriInstance = new Google_Service_Walletobjects_ImageUri(); $imageInstance = new Google_Service_Walletobjects_Image(); $uriInstance->setUri( 'http://farm8.staticflickr.com/7340/11177041185_a61a7f2139_o.jpg' ); $imageInstance->setSourceUri($uriInstance); // Create wallet class. $wobClass = new Google_Service_Walletobjects_LoyaltyClass(); $wobClass->setId('2945482443380251551.ExampleClass1'); $wobClass->setIssuerName('Baconrista'); $wobClass->setProgramName('Baconrista Rewards'); $wobClass->setProgramLogo($imageInstance); $wobClass->setRewardsTierLabel('Tier'); $wobClass->setRewardsTier('Gold'); $wobClass->setAccountNameLabel('Member Name'); $wobClass->setAccountIdLabel('Member Id'); $wobClass->setLinksModuleData($linksModuleData); $wobClass->setTextModulesData($textModulesData); $wobClass->setImageModulesData($imagesModuleDataArr); $wobClass->setMessages($messages); $wobClass->setReviewStatus('underReview'); $wobClass->setMultipleDevicesAndHoldersAllowedStatus('multipleHolders'); $wobClass->setLocations($locations); $service->loyaltyclass->insert($wobClass);
Python
loyalty_class = { 'accountIdLabel': 'Member Id', 'accountNameLabel': 'Member Name', 'multipleDevicesAndHoldersAllowedStatus': 'multipleHolders', 'id': '2945482443380251551.ExampleClass1', 'issuerName': 'Baconrista', 'kind': 'walletobjects#loyaltyClass', '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 }], 'textModulesData': [{ 'header': 'Rewards details', 'body': 'Welcome to Baconrista rewards. Enjoy your rewards for being a loyal customer. ' + '10 points for ever dollar spent. Redeem your points for free coffee, bacon and more! ' }], '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' } } } ], 'messages': [{ 'header': 'Welcome to Banconrista Rewards!', 'body': 'Featuring our new bacon donuts.', 'kind': 'walletobjects#walletObjectMessage' }], 'programLogo': { 'kind': 'walletobjects#image', 'sourceUri': { 'kind': 'walletobjects#uri', 'uri': 'http://farm8.staticflickr.com/7340/11177041185_a61a7f2139_o.jpg' } }, 'programName': 'Baconrista Rewards', 'rewardsTier': 'Gold', 'rewardsTierLabel': 'Tier', 'reviewStatus': 'underReview', } api_request = service.loyaltyclass().insert(body=loyalty_class) api_response = api_request.execute()
すべての LoyaltyClass
フィールドの一覧については、LoyaltyClass のリファレンスをご覧ください。
2. オブジェクトを表す JWT を生成する
最初に、次のスニペットに示すように LoyaltyObject
を定義します。
リソース
{ "classId": "2945482443380251551.ExampleClass1", "id": "2945482443380251551.ExampleObject1", "accountId": "1234567890", "accountName": "Jane Doe", "barcode": { "alternateText": "12345", "type": "qrCode", "value": "28343E3" }, "textModulesData": [{ "header": "Jane's Baconrista Rewards", "body": "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." }], "linksModuleData": { "uris": [ { "kind": "walletobjects#uri", "uri": "https://www.baconrista.com/myaccount?id=1234567890", "description": "My Baconrista Account" }] }, "infoModuleData": { "labelValueRows": [{ "columns": [{ "label": "Next Reward in", "value": "2 coffees" }, { "label": "Member Since", "value": "01/15/2013" }] }, { "columns": [{ "label": "Local Store", "value": "Mountain View" }] }], "showLastUpdateTime": "true" }, "loyaltyPoints": { "balance": { "string": "5000" }, "label": "Points", "pointsType": "points" }, "messages": [{ "header": "Jane, welcome to Banconrista Rewards!", "body": "Thanks for joining our program. Show this message to " + "our barista for your first free coffee on us!" }], "state": "active" }
Java
// Define Barcode Barcode barcode = new Barcode().setType("qrCode") .setValue("28343E3") .setAlternateText("12345"); // Define Points LoyaltyPoints points = new LoyaltyPoints() .setLabel("Points") .setPointsType("points") .setBalance(new LoyaltyPointsBalance().setString("500")); // Define Text Module Data List<TextModuleData> textModulesData = new ArrayList<TextModuleData>(); TextModuleData textModuleData = new TextModuleData() .setHeader("Jane's Baconrista Rewards") .setBody( "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<Uri> uris = new ArrayList<Uri>(); Uri uri1 = new Uri().setDescription("My Baconrista Account") .setUri("http://www.baconrista.com/myaccount?id=1234567890"); uris.add(uri1); LinksModuleData linksModuleData = new LinksModuleData().setUris(uris); // Define Info Module List<LabelValue> row0cols = new ArrayList<LabelValue>(); LabelValue row0col0 = new LabelValue().setLabel("Next Reward in") .setValue("2 coffees"); LabelValue row0col1 = new LabelValue().setLabel("Member Since") .setValue("01/15/2013"); row0cols.add(row0col0); row0cols.add(row0col1); List<LabelValue> row1cols = new ArrayList<LabelValue>(); LabelValue row1col0 = new LabelValue().setLabel("Local Store") .setValue("Mountain View"); row1cols.add(row1col0); List<LabelValueRow> rows = new ArrayList<LabelValueRow>(); LabelValueRow row0 = new LabelValueRow().setColumns(row0cols); LabelValueRow row1 = new LabelValueRow().setColumns(row1cols); rows.add(row0); rows.add(row1); InfoModuleData infoModuleData = new InfoModuleData() .setShowLastUpdateTime(true) .setLabelValueRows(rows); // Define general messages List<Message> messages = new ArrayList<Message>(); Message message = new Message() .setHeader("Hi Jane!") .setBody("Thanks for joining our program. Show this message to " + "our barista for your first free coffee on us!"); messages.add(message); // Define Wallet Instance LoyaltyObject object = new LoyaltyObject() .setClassId('2945482443380251551.ExampleClass1').setId('2945482443380251551.ExampleObject1') .setState("active").setBarcode(barcode).setInfoModuleData(infoModuleData) .setAccountName("Jane Doe").setTextModulesData(textModulesData) .setMessages(messages).setLinksModuleData(linksModuleData) .setAccountId("1234567890").setLoyaltyPoints(points);
PHP
$barcode = new Google_Service_Walletobjects_Barcode(); $barcode->setAlternateText('12345'); $barcode->setType('qrCode'); $barcode->setValue('28343E3'); // Define text module data. $textModulesData = array( array( 'header' => 'Janes Baconrista Rewards', 'body' => '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.' ) ); // Define links module data. $linksModuleData = new Google_Service_Walletobjects_LinksModuleData(); $uris = array ( array( 'uri' => 'http://www.baconrista.com/myaccount?id=1234567890', 'kind' => 'walletobjecs#uri', 'description' => 'My Baconrista Account' ) ); $linksModuleData->setUris($uris); // Define label values. $labelValueRows = array( array( 'columns' => array( array( 'label' => 'Next Reward in', 'value' => '2 coffees' ), array( 'label' => 'Member Since', 'value' => '01/15/2013' ) ) ), array( 'columns' => array( array( 'label' => 'Local Store', 'value' => 'Mountain View' ) ) ) ); // Define info module data. $infoModuleData = new Google_Service_Walletobjects_InfoModuleData(); $infoModuleData->setShowLastUpdateTime(true); $infoModuleData->setLabelValueRows($labelValueRows); // Messages to be displayed. $messages = array(array( 'header' => 'Jane, welcome to Banconrista Rewards!', 'body' => 'Thanks for joining our program. Show this message to '. 'our barista for your first free coffee on us!', 'kind' => 'walletobjects#walletObjectMessage' )); // Reward points a user has. $points = new Google_Service_Walletobjects_LoyaltyPoints(); $balance = new Google_Service_Walletobjects_LoyaltyPointsBalance(); $balance->setString('500'); $points->setBalance($balance); $points->setLabel('Points'); $points->setPointsType('points'); // Create wallet object. $wobObject = new Google_Service_Walletobjects_LoyaltyObject(); $wobObject->setClassId('2945482443380251551.ExampleClass1'); $wobObject->setId('2945482443380251551.ExampleObject1'); $wobObject->setState('active'); $wobObject->setBarcode($barcode); $wobObject->setInfoModuleData($infoModuleData); $wobObject->setLinksModuleData($linksModuleData); $wobObject->setTextModulesData($textModulesData); $wobObject->setAccountName('Jane Doe'); $wobObject->setAccountId('1234567890'); $wobObject->setLoyaltyPoints($points); $wobObject->setMessages($messages);
Python
loyalty_object = { 'classId' : '2945482443380251551.ExampleClass1', 'id' : '2945482443380251551.ExampleObject1', 'accountId': '1234567890', 'accountName': 'Jane Doe', 'barcode': { 'alternateText' : '12345', 'type' : 'qrCode', 'value' : '28343E3' }, 'textModulesData': [{ 'header': 'Jane\'s Baconrista Rewards', 'body': '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.' }], 'linksModuleData': { 'uris': [ { 'kind': 'walletobjects#uri', 'uri': 'http://www.baconrista.com/myaccount?id=1234567890', 'description': 'My Baconrista Account' }] }, 'infoModuleData': { 'labelValueRows': [{ 'columns': [{ 'label': 'Next Reward in', 'value': '2 coffees' }, { 'label': 'Member Since', 'value': '01/15/2013' }] },{ 'columns': [{ 'label': 'Local Store', 'value': 'Mountain View' }] }], 'showLastUpdateTime': 'true' }, 'messages': [{ 'header': 'Jane, welcome to Banconrista Rewards', 'body': 'Thanks for joining our program. Show this message to ' + 'our barista for your first free coffee on us!', 'kind': 'walletobjects#walletObjectMessage' }], 'loyaltyPoints': { 'balance': { 'string': '500' }, 'label': 'Points', 'pointsType': 'points' }, 'state': 'active' }
OAuth 2.0 サービス アカウントの秘密鍵を使用して、LoyaltyObject
を JWT にエンコードします。次のスニペットは、JWT をさまざまな言語でエンコードする方法を示しています。プロトコルタブにはエンコードされていない JWT が示されています。JWT のフィールドの説明については、Google Pay API for Passes JWT をご覧ください。
プロトコル
{ "iss": "example_service_account@developer.gserviceaccount.com", "aud": "google", "typ": "savetoandroidpay", "iat": 1368029586, "payload": { "eventTicketClasses": [{ ... //Event ticket Class JSON }], "eventTicketObjects": [{ ... //Event ticket Object JSON }], "flightClasses": [{ ... //Flight Class JSON }], "flightObjects": [{ ... //Flight Object JSON }], "giftCardClasses": [{ ... //Gift card Class JSON }], "giftCardObjects": [{ ... //Gift card Object JSON }], "loyaltyClasses": [{ ... //Loyalty Class JSON }], "loyaltyObjects": [{ ... //Loyalty Object JSON }], "offerClasses": [{ ... //Offer Class JSON }], "offerObjects": [{ ... //Offer Object JSON }], "transitClasses": [{ ... //Transit Class JSON }], "transitObjects": [{ ... //Transit Object JSON }] }, "origins": ["http://baconrista.com", "https://baconrista.com"] }
Java
WobCredentials credentials = null; WobUtils utils = null; // Instantiate the WobUtils class which contains handy functions // Wob utils can be found in the quickstart sample try { credentials = new WobCredentials( ServiceAccountEmailAddress, ServiceAccountPrivateKeyPath, ApplicationName, IssuerId); utils = new WobUtils(credentials); } catch (GeneralSecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // Add valid domains for the Save to Wallet button List<String> origins = new ArrayList<String>(); origins.add("http://baconrista.com"); origins.add("https://baconrista.com"); origins.add(req.getScheme() + "://" + req.getServerName() + ":" + req.getLocalPort()); //Generate Objects and Classes here //........ WobPayload payload = new WobPayload(); payload.addObject({WalletObject/WalletClass}); // Convert the object into a Save to Android Pay Jwt String jwt = null; try { jwt = utils.generateSaveJwt(payload, origins); } catch (SignatureException e) { e.printStackTrace(); }
PHP
$requestBody = [ "iss"=> SERVICE_ACCOUNT_EMAIL_ADDRESS, "aud" => "google", "typ" => "savetoandroidpay", "iat"=> time(), "payload" => { "eventTicketClasses" => [ ], # Event ticket classes "eventTicketObjects" => [ ], # Event ticket objects "flightClasses" => [ ], # Flight classes "flightObjects" => [ ], # Flight objects "giftCardClasses" => [ ], # Gift card classes "giftCardObjects" => [ ], # Gift card objects "loyaltyClasses" => [ ], # Loyalty classes "loyaltyObjects" => [ ], # Loyalty objects "offerClasses" => [ ], # Offer classes "offerObjects" => [ ], # Offer objects "transitClasses" => [ ], # Transit classes "transitObjects" => [ ] # Transit objects }, "origins" => ["http://baconrista.com", "https://baconrista.com"] ] // Generate the Save to Android Pay Jwt echo $jwt = $assertObj->makeSignedJwt($requestBody, $client);
Python
jwt = { 'iss': config.SERVICE_ACCOUNT_EMAIL_ADDRESS, 'aud': 'google', 'typ': 'savetoandroidpay', 'iat': int(time.time()), 'payload': { 'webserviceResponse': { 'result': 'approved', 'message': 'Success.' }, 'eventTicketClasses': [], # Event ticket classes 'eventTicketObjects': [], # Event ticket objects 'flightClasses': [], # Flight classes 'flightObjects': [], # Flight objects 'giftCardClasses': [], # Gift card classes 'giftCardObjects': [], # Gift card objects 'loyaltyClasses': [], # Loyalty classes 'loyaltyObjects': [], # Loyalty objects 'offerClasses': [], # Offer classes 'offerObjects': [], # Offer objects 'transitClasses': [], # Transit classes 'transitObjects': [] # Transit objects }, 'origins' : ['http://baconrista.com', 'https://baconrista.com'] } // Generate the Save to Android Pay Jwt signer = crypt.Signer.from_string(app_key) signed_jwt = crypt.make_signed_jwt(signer, jwt) response = webapp2.Response(signed_jwt)
エンコードされた JWT の安全な長さは 1,800 文字までです。この制限を超えないようにしてください。1,800 文字を超えると、ウェブブラウザによる切り捨てが発生し、保存できないことがあります。JWT でエンコードされるオブジェクトは小さくし、ユーザー固有のデータのみが含まれるようにします。JWT を作成する前に、できる限りのデータをオブジェクトのクラスに保存してください。制限を超える大きなオブジェクトについては、REST API を使用してオブジェクトを作成し、オブジェクト ID のみを JWT で送信するようにしてください。
3. [Google Pay に保存] ボタンを追加する
[Google Pay に保存] ボタンを表示するページに次のスクリプトを挿入します。
<script src="https://apis.google.com/js/platform.js" type="text/javascript"></script>
次に、[Google Pay に保存] ボタンの配置と属性を定義する g:savetoandroidpay
名前空間タグを挿入します。以前に生成した JWT を挿入します。
<g:savetoandroidpay jwt="{jwt_generated}" onsuccess="successHandler" onfailure="failureHandler" size="small" theme="light" ></g:savetoandroidpay>
これで、[Google Pay に保存] ボタンがウェブページに表示されます。
JavaScript ウェブボタンの詳細については、Google Pay API for Passes for Web API リファレンスのドキュメントをご覧ください。
メールまたは SMS にリンクを追加する
メールを使用してパスを Google Pay に保存するには、ディープリンクが設定されたボタンをメールに追加します。
[Google Pay に保存] ボタンは、次の 3 つの手順でメールまたは SMS に統合できます。
次のステップではポイントパスを使用しますが、プロセスはすべてのパスで共通です。
1. オブジェクトを拡張するクラスを定義する
最初に LoyaltyClass
を定義します。LoyaltyClass
を挿入するには、次の REST URI に POST
リクエストを送信します。
https://walletobjects.googleapis.com/walletobjects/v1/loyaltyClass
厳密なエラー解析を有効にして ID フィールド重複などのエラーも検出するには、次の例のように strict=true
パラメータを REST URI に追加します。
https://walletobjects.googleapis.com/walletobjects/v1/loyaltyClass?strict=true
次の例は、LoyaltyClass
を表す JSON リソースを示しています。JSON は POST
リクエストの本文に含めて送信する必要があります。次のコードサンプルは、LoyaltyClass
を定義して挿入する方法を具体的に示しています。
リソース
{ "accountIdLabel": "Member Id", "accountNameLabel": "Member Name", "id": "2945482443380251551.ExampleClass1", "issuerName": "Baconrista", "kind": "walletobjects#loyaltyClass", "textModulesData": [ { "header": "Rewards details", "body": "Welcome to Baconrista rewards. Enjoy your rewards for being a loyal customer. " + "10 points for every dollar spent. Redeem your points for free coffee, bacon and more!" } ], "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" } } } ], "messages": [{ "header": "Welcome to Banconrista Rewards!", "body": "Featuring our new bacon donuts.", "kind": "walletobjects#walletObjectMessage" }], "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 }], "programLogo": { "kind": "walletobjects#image", "sourceUri": { "kind": "walletobjects#uri", "uri": "https://farm8.staticflickr.com/7340/11177041185_a61a7f2139_o.jpg" } }, "programName": "Baconrista Rewards", "rewardsTier": "Gold", "rewardsTierLabel": "Tier", "reviewStatus": "underReview", "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 Text Module Data List<TextModuleData> textModulesData = new ArrayList<TextModuleData>(); TextModuleData textModuleData = new TextModuleData().setHeader("Rewards details") .setBody( "Welcome to Baconrista rewards. Enjoy your rewards for being a loyal customer. 10 points for ever dollar spent. Redeem your points for free coffee, bacon and more!"); textModulesData.add(textModuleData); // 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 general messages List<Message> messages = new ArrayList<Message>(); Message message = new Message() .setHeader("Welcome to Baconrista") .setBody("Featuring our new bacon donuts."); messages.add(message); // 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)); // Create class LoyaltyClass wobClass = new LoyaltyClass() .setId('2945482443380251551.ExampleClass1') .setIssuerName("Baconrista") .setProgramName("Baconrista Rewards") .setProgramLogo( new Image().setSourceUri(new ImageUri() .setUri("http://farm8.staticflickr.com/7340/11177041185_a61a7f2139_o.jpg"))) .setRewardsTierLabel("Tier").setRewardsTier("Gold") .setImageModulesData(imageModuleData) .setTextModulesData(textModulesData) .setLinksModuleData(linksModuleData) .setAccountNameLabel("Member Name").setAccountIdLabel("Member Id") .setMessages(messages) .setReviewStatus("underReview").setMultipleDevicesAndHoldersAllowedStatus("multipleHolders") .setLocations(locations); LoyaltyClass response = client.loyaltyclass().insert(wobClass).execute();
PHP
// Define text module data. $textModulesData = array( array( 'header' => 'Rewards details', 'body' => 'Welcome to Baconrista rewards. Enjoy your rewards for being a loyal customer. ' . '10 points for every dollar spent. Redeem your points for free coffee, bacon and more!' ) ); // 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://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.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); // Messages to be displayed to all users of Wallet Objects. $messages = array(array( 'header' => 'Welcome to Banconrista Rewards!', 'body' => 'Featuring our new bacon donuts.', 'kind' => 'walletobjects#walletObjectMessage' )); $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 program logo. $uriInstance = new Google_Service_Walletobjects_ImageUri(); $imageInstance = new Google_Service_Walletobjects_Image(); $uriInstance->setUri( 'http://farm8.staticflickr.com/7340/11177041185_a61a7f2139_o.jpg' ); $imageInstance->setSourceUri($uriInstance); // Create wallet class. $wobClass = new Google_Service_Walletobjects_LoyaltyClass(); $wobClass->setId('2945482443380251551.ExampleClass1'); $wobClass->setIssuerName('Baconrista'); $wobClass->setProgramName('Baconrista Rewards'); $wobClass->setProgramLogo($imageInstance); $wobClass->setRewardsTierLabel('Tier'); $wobClass->setRewardsTier('Gold'); $wobClass->setAccountNameLabel('Member Name'); $wobClass->setAccountIdLabel('Member Id'); $wobClass->setLinksModuleData($linksModuleData); $wobClass->setTextModulesData($textModulesData); $wobClass->setImageModulesData($imagesModuleDataArr); $wobClass->setMessages($messages); $wobClass->setReviewStatus('underReview'); $wobClass->setMultipleDevicesAndHoldersAllowedStatus('multipleHolders'); $wobClass->setLocations($locations); $service->loyaltyclass->insert($wobClass);
Python
loyalty_class = { 'accountIdLabel': 'Member Id', 'accountNameLabel': 'Member Name', 'multipleDevicesAndHoldersAllowedStatus': 'multipleHolders', 'id': '2945482443380251551.ExampleClass1', 'issuerName': 'Baconrista', 'kind': 'walletobjects#loyaltyClass', '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 }], 'textModulesData': [{ 'header': 'Rewards details', 'body': 'Welcome to Baconrista rewards. Enjoy your rewards for being a loyal customer. ' + '10 points for ever dollar spent. Redeem your points for free coffee, bacon and more! ' }], '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' } } } ], 'messages': [{ 'header': 'Welcome to Banconrista Rewards!', 'body': 'Featuring our new bacon donuts.', 'kind': 'walletobjects#walletObjectMessage' }], 'programLogo': { 'kind': 'walletobjects#image', 'sourceUri': { 'kind': 'walletobjects#uri', 'uri': 'http://farm8.staticflickr.com/7340/11177041185_a61a7f2139_o.jpg' } }, 'programName': 'Baconrista Rewards', 'rewardsTier': 'Gold', 'rewardsTierLabel': 'Tier', 'reviewStatus': 'underReview', } api_request = service.loyaltyclass().insert(body=loyalty_class) api_response = api_request.execute()
すべての LoyaltyClass
フィールドの一覧については、LoyaltyClass のリファレンスをご覧ください。
2. オブジェクトを表す JWT を生成する
最初に、次のスニペットに示すように LoyaltyObject
を定義します。
リソース
{ "classId": "2945482443380251551.ExampleClass1", "id": "2945482443380251551.ExampleObject1", "accountId": "1234567890", "accountName": "Jane Doe", "barcode": { "alternateText": "12345", "type": "qrCode", "value": "28343E3" }, "textModulesData": [{ "header": "Jane's Baconrista Rewards", "body": "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." }], "linksModuleData": { "uris": [ { "kind": "walletobjects#uri", "uri": "https://www.baconrista.com/myaccount?id=1234567890", "description": "My Baconrista Account" }] }, "infoModuleData": { "labelValueRows": [{ "columns": [{ "label": "Next Reward in", "value": "2 coffees" }, { "label": "Member Since", "value": "01/15/2013" }] }, { "columns": [{ "label": "Local Store", "value": "Mountain View" }] }], "showLastUpdateTime": "true" }, "loyaltyPoints": { "balance": { "string": "5000" }, "label": "Points", "pointsType": "points" }, "messages": [{ "header": "Jane, welcome to Banconrista Rewards!", "body": "Thanks for joining our program. Show this message to " + "our barista for your first free coffee on us!" }], "state": "active" }
Java
// Define Barcode Barcode barcode = new Barcode().setType("qrCode") .setValue("28343E3") .setAlternateText("12345"); // Define Points LoyaltyPoints points = new LoyaltyPoints() .setLabel("Points") .setPointsType("points") .setBalance(new LoyaltyPointsBalance().setString("500")); // Define Text Module Data List<TextModuleData> textModulesData = new ArrayList<TextModuleData>(); TextModuleData textModuleData = new TextModuleData() .setHeader("Jane's Baconrista Rewards") .setBody( "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<Uri> uris = new ArrayList<Uri>(); Uri uri1 = new Uri().setDescription("My Baconrista Account") .setUri("http://www.baconrista.com/myaccount?id=1234567890"); uris.add(uri1); LinksModuleData linksModuleData = new LinksModuleData().setUris(uris); // Define Info Module List<LabelValue> row0cols = new ArrayList<LabelValue>(); LabelValue row0col0 = new LabelValue().setLabel("Next Reward in") .setValue("2 coffees"); LabelValue row0col1 = new LabelValue().setLabel("Member Since") .setValue("01/15/2013"); row0cols.add(row0col0); row0cols.add(row0col1); List<LabelValue> row1cols = new ArrayList<LabelValue>(); LabelValue row1col0 = new LabelValue().setLabel("Local Store") .setValue("Mountain View"); row1cols.add(row1col0); List<LabelValueRow> rows = new ArrayList<LabelValueRow>(); LabelValueRow row0 = new LabelValueRow().setColumns(row0cols); LabelValueRow row1 = new LabelValueRow().setColumns(row1cols); rows.add(row0); rows.add(row1); InfoModuleData infoModuleData = new InfoModuleData() .setShowLastUpdateTime(true) .setLabelValueRows(rows); // Define general messages List<Message> messages = new ArrayList<Message>(); Message message = new Message() .setHeader("Hi Jane!") .setBody("Thanks for joining our program. Show this message to " + "our barista for your first free coffee on us!"); messages.add(message); // Define Wallet Instance LoyaltyObject object = new LoyaltyObject() .setClassId('2945482443380251551.ExampleClass1').setId('2945482443380251551.ExampleObject1') .setState("active").setBarcode(barcode).setInfoModuleData(infoModuleData) .setAccountName("Jane Doe").setTextModulesData(textModulesData) .setMessages(messages).setLinksModuleData(linksModuleData) .setAccountId("1234567890").setLoyaltyPoints(points);
PHP
$barcode = new Google_Service_Walletobjects_Barcode(); $barcode->setAlternateText('12345'); $barcode->setType('qrCode'); $barcode->setValue('28343E3'); // Define text module data. $textModulesData = array( array( 'header' => 'Janes Baconrista Rewards', 'body' => '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.' ) ); // Define links module data. $linksModuleData = new Google_Service_Walletobjects_LinksModuleData(); $uris = array ( array( 'uri' => 'http://www.baconrista.com/myaccount?id=1234567890', 'kind' => 'walletobjecs#uri', 'description' => 'My Baconrista Account' ) ); $linksModuleData->setUris($uris); // Define label values. $labelValueRows = array( array( 'columns' => array( array( 'label' => 'Next Reward in', 'value' => '2 coffees' ), array( 'label' => 'Member Since', 'value' => '01/15/2013' ) ) ), array( 'columns' => array( array( 'label' => 'Local Store', 'value' => 'Mountain View' ) ) ) ); // Define info module data. $infoModuleData = new Google_Service_Walletobjects_InfoModuleData(); $infoModuleData->setShowLastUpdateTime(true); $infoModuleData->setLabelValueRows($labelValueRows); // Messages to be displayed. $messages = array(array( 'header' => 'Jane, welcome to Banconrista Rewards!', 'body' => 'Thanks for joining our program. Show this message to '. 'our barista for your first free coffee on us!', 'kind' => 'walletobjects#walletObjectMessage' )); // Reward points a user has. $points = new Google_Service_Walletobjects_LoyaltyPoints(); $balance = new Google_Service_Walletobjects_LoyaltyPointsBalance(); $balance->setString('500'); $points->setBalance($balance); $points->setLabel('Points'); $points->setPointsType('points'); // Create wallet object. $wobObject = new Google_Service_Walletobjects_LoyaltyObject(); $wobObject->setClassId('2945482443380251551.ExampleClass1'); $wobObject->setId('2945482443380251551.ExampleObject1'); $wobObject->setState('active'); $wobObject->setBarcode($barcode); $wobObject->setInfoModuleData($infoModuleData); $wobObject->setLinksModuleData($linksModuleData); $wobObject->setTextModulesData($textModulesData); $wobObject->setAccountName('Jane Doe'); $wobObject->setAccountId('1234567890'); $wobObject->setLoyaltyPoints($points); $wobObject->setMessages($messages);
Python
loyalty_object = { 'classId' : '2945482443380251551.ExampleClass1', 'id' : '2945482443380251551.ExampleObject1', 'accountId': '1234567890', 'accountName': 'Jane Doe', 'barcode': { 'alternateText' : '12345', 'type' : 'qrCode', 'value' : '28343E3' }, 'textModulesData': [{ 'header': 'Jane\'s Baconrista Rewards', 'body': '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.' }], 'linksModuleData': { 'uris': [ { 'kind': 'walletobjects#uri', 'uri': 'http://www.baconrista.com/myaccount?id=1234567890', 'description': 'My Baconrista Account' }] }, 'infoModuleData': { 'labelValueRows': [{ 'columns': [{ 'label': 'Next Reward in', 'value': '2 coffees' }, { 'label': 'Member Since', 'value': '01/15/2013' }] },{ 'columns': [{ 'label': 'Local Store', 'value': 'Mountain View' }] }], 'showLastUpdateTime': 'true' }, 'messages': [{ 'header': 'Jane, welcome to Banconrista Rewards', 'body': 'Thanks for joining our program. Show this message to ' + 'our barista for your first free coffee on us!', 'kind': 'walletobjects#walletObjectMessage' }], 'loyaltyPoints': { 'balance': { 'string': '500' }, 'label': 'Points', 'pointsType': 'points' }, 'state': 'active' }
OAuth 2.0 サービス アカウントの秘密鍵を使用して、LoyaltyObject
を JWT にエンコードします。次のスニペットは、JWT をさまざまな言語でエンコードする方法を示しています。プロトコルタブにはエンコードされていない JWT が示されています。JWT のフィールドの説明については、Google Pay API for Passes JWT をご覧ください。
プロトコル
{ "iss": "example_service_account@developer.gserviceaccount.com", "aud": "google", "typ": "savetoandroidpay", "iat": 1368029586, "payload": { "eventTicketClasses": [{ ... //Event ticket Class JSON }], "eventTicketObjects": [{ ... //Event ticket Object JSON }], "flightClasses": [{ ... //Flight Class JSON }], "flightObjects": [{ ... //Flight Object JSON }], "giftCardClasses": [{ ... //Gift card Class JSON }], "giftCardObjects": [{ ... //Gift card Object JSON }], "loyaltyClasses": [{ ... //Loyalty Class JSON }], "loyaltyObjects": [{ ... //Loyalty Object JSON }], "offerClasses": [{ ... //Offer Class JSON }], "offerObjects": [{ ... //Offer Object JSON }], "transitClasses": [{ ... //Transit Class JSON }], "transitObjects": [{ ... //Transit Object JSON }] }, "origins": ["http://baconrista.com", "https://baconrista.com"] }
Java
WobCredentials credentials = null; WobUtils utils = null; // Instantiate the WobUtils class which contains handy functions // Wob utils can be found in the quickstart sample try { credentials = new WobCredentials( ServiceAccountEmailAddress, ServiceAccountPrivateKeyPath, ApplicationName, IssuerId); utils = new WobUtils(credentials); } catch (GeneralSecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // Add valid domains for the Save to Wallet button List<String> origins = new ArrayList<String>(); origins.add("http://baconrista.com"); origins.add("https://baconrista.com"); origins.add(req.getScheme() + "://" + req.getServerName() + ":" + req.getLocalPort()); //Generate Objects and Classes here //........ WobPayload payload = new WobPayload(); payload.addObject({WalletObject/WalletClass}); // Convert the object into a Save to Android Pay Jwt String jwt = null; try { jwt = utils.generateSaveJwt(payload, origins); } catch (SignatureException e) { e.printStackTrace(); }
PHP
$requestBody = [ "iss"=> SERVICE_ACCOUNT_EMAIL_ADDRESS, "aud" => "google", "typ" => "savetoandroidpay", "iat"=> time(), "payload" => { "eventTicketClasses" => [ ], # Event ticket classes "eventTicketObjects" => [ ], # Event ticket objects "flightClasses" => [ ], # Flight classes "flightObjects" => [ ], # Flight objects "giftCardClasses" => [ ], # Gift card classes "giftCardObjects" => [ ], # Gift card objects "loyaltyClasses" => [ ], # Loyalty classes "loyaltyObjects" => [ ], # Loyalty objects "offerClasses" => [ ], # Offer classes "offerObjects" => [ ], # Offer objects "transitClasses" => [ ], # Transit classes "transitObjects" => [ ] # Transit objects }, "origins" => ["http://baconrista.com", "https://baconrista.com"] ] // Generate the Save to Android Pay Jwt echo $jwt = $assertObj->makeSignedJwt($requestBody, $client);
Python
jwt = { 'iss': config.SERVICE_ACCOUNT_EMAIL_ADDRESS, 'aud': 'google', 'typ': 'savetoandroidpay', 'iat': int(time.time()), 'payload': { 'webserviceResponse': { 'result': 'approved', 'message': 'Success.' }, 'eventTicketClasses': [], # Event ticket classes 'eventTicketObjects': [], # Event ticket objects 'flightClasses': [], # Flight classes 'flightObjects': [], # Flight objects 'giftCardClasses': [], # Gift card classes 'giftCardObjects': [], # Gift card objects 'loyaltyClasses': [], # Loyalty classes 'loyaltyObjects': [], # Loyalty objects 'offerClasses': [], # Offer classes 'offerObjects': [], # Offer objects 'transitClasses': [], # Transit classes 'transitObjects': [] # Transit objects }, 'origins' : ['http://baconrista.com', 'https://baconrista.com'] } // Generate the Save to Android Pay Jwt signer = crypt.Signer.from_string(app_key) signed_jwt = crypt.make_signed_jwt(signer, jwt) response = webapp2.Response(signed_jwt)
エンコードされた JWT の安全な長さは 1,800 文字までです。この制限を超えないようにしてください。1,800 文字を超えると、ウェブブラウザによる切り捨てが発生し、保存できないことがあります。JWT でエンコードされるオブジェクトは小さくし、ユーザー固有のデータのみが含まれるようにします。JWT を作成する前に、できる限りのデータをオブジェクトのクラスに保存してください。制限を超える大きなオブジェクトについては、REST API を使用してオブジェクトを作成し、オブジェクト ID のみを JWT で送信するようにしてください。
3. [Google Pay に保存] ボタンをメールまたは SMS に追加する
生成された JWT とクラス ID を入力します。
https://pay.google.com/gp/v/save/{jwt_generated}
URL の最後に必ずスラッシュを付けてからパラメータを続けて入力してください。
ディープリンク URL の安全な長さは 1,800 文字までです。この制限を超えないようにしてください。1,800 文字を超えると、ウェブブラウザによる切り捨てが発生し、保存できないことがあります。JWT でエンコードされるオブジェクトは小さくし、ユーザー固有のデータのみが含まれるようにします。JWT を作成する前に、できる限りのデータをオブジェクトのクラスに保存してください。制限を超える大きなオブジェクトについては、REST API を使用してオブジェクトを作成し、オブジェクト ID のみを JWT で送信するようにしてください。
ブランド ガイドラインをご覧になり、[Google Pay に保存] ボタンの画像をダウンロードしてリンクに正しく適用してください。ディープリンクと [Google Pay に保存] ボタンについて詳しくは、Introduction to Save to Android Pay Deep Links(Save to Android Pay ディープリンクの紹介)をご覧ください。
Gmail では、Go-To アクションを使用してトップレベルのボタンをメールの件名に表示できます。たとえば、[Google Pay に保存] ボタンをメールの件名に表示すると、ユーザーは自分の受信トレイからクーポンを直接保存できるようになります。この機能を有効にするには、メールに View アクションを追加し、上記のディープリンクを設定します。
ネイティブの Android アプリから利用する
[Google Pay に保存] ボタンは、次の方法でネイティブの Android アプリに追加できます。
JWT リンクとインテント メソッドを使用する
次の手順に沿って、アプリから Google Pay に Pass を保存します。
- [Google Pay に保存] ボタンをメールまたは SMS に追加するの手順を完了します。
ACTION_VIEW
インテントを使用して、[Google Pay に保存] ボタンからディープリンクを開きます。インテントをトリガーするボタンがブランドのガイドラインに沿っていることを確認してください。
以下の例は、フローの概要を示したものです。
- パスが保存される前のある時点で、バックエンドで REST API を使用してクラスが作成されます。
- エンドユーザーがパスの保存を要求すると、サーバーのバックエンドでオブジェクトを表す
JWT
が Android クライアント アプリに送信されます。 - Android クライアント アプリには、Google のブランド ガイドラインに沿った [Google Pay に保存] ボタンが含まれています。これをクリックすると
ACTION_VIEW
インテントが開きます。このインテントには、パスに JWT を含む URI が設定されています。次に例を示します。https://pay.google.com/gp/v/save/{jwt_generated}
JWT POST リクエスト メソッドを使用する
JWT の POST
リクエスト メソッドは、Android アプリ用のフライトまたはイベント チケットのクラスおよびオブジェクトを作成するための代替メソッドです。このメソッドが使用されるのは、オブジェクトを保存する前にクラスの作成と挿入に必要なバックエンド作業を実装するのが難しい場合です。このメソッドは、イベント チケットや搭乗券(時間の経過とともに多くのクラスが作成される可能性があるパス)を扱う場合にとても便利です。このフローを簡単に説明すると、次のようになります。
- エンドユーザーがフライトのチェックインを行うか、またはイベント チケットを利用するときに、サーバーのバックエンドでクラスとオブジェクトの両方を含む JWT が Android クライアント アプリにレンダリングされます。
- Android クライアント アプリには、Google のブランド ガイドラインに沿った [Google に保存] ボタンが含まれています。ボタンをクリックすると、次のようになります。
POST
リクエストにより、JWT が HTTPS を通じて Google エンドポイントに送信されます。- 作成された HTTP レスポンスの本文の URI が返されます。この URI を使用して
ACTION_VIEW
インテントを開く必要があります。
JWT POST
リクエスト メソッドには、API キーも必要です。これは、REST API 呼び出しにクエリ パラメータとして追加されます。
クラスの作成
Google のバックエンドで新しいクラスが作成されるのは、過去に一度も保存されたことのない class.id
が指定された場合のみです。したがって、JWT を介してクラスの詳細を Google に複数回渡した可能性がある場合、バックエンドではクラスがすでに保存されていることが認識され、搭乗券が保存されるたびに新しいクラスが作成されることはありません。
クラスの更新
最初の搭乗券の後、オブジェクトはクラスと一緒に保存されます。REST API で想定される class.id
を使用して、ADDMESSAGE
、GET
、LIST
、PATCH
、UPDATE
のオペレーションを想定どおりに実行できます。
クラスの詳細を変更するには、Class Update API を使用する必要があります。他のクラスの詳細をいくつか含む class.id
=XYZ のクラスを作成してから、異なるクラスの詳細を含む class.id
=XYZ のクラスを作成しようとすると、元のクラスが維持され、変更は適用されません。
JWT の形式
送信する JWT の形式は、Google Pay API for Passes JWT に関するリファレンス ドキュメントで詳しく説明されています。この payload
では、作成するオブジェクトを表すオブジェクトのエントリを 1 つと、作成したクラスが含まれるクラスのエントリを 1 つ渡します。
HTTP リクエスト
INSERT
メソッドを使用して、JWT で指定されたクラスやオブジェクトを挿入できます。API キーはクエリ パラメータとして設定する必要があります。
JWT INSERT メソッド
JWT が指定された場合、INSERT
メソッドを実行すると、JWT で指定されたクラスとオブジェクトが挿入されます。挿入が成功すると、200 HTTP レスポンスが返されます。
HTTP リクエスト
POST https://walletobjects.googleapis.com/walletobjects/v1/jwt/
承認
このリクエストに承認は不要です。ただし、JWT は RSA-SHA256 で署名する必要があります。署名鍵は OAuth サービス アカウントで生成された鍵です。
リクエストの本文
リクエストの本文には、以下の構造を使用してデータを指定してください。
{ “jwt” : string }
レスポンスの本文
このメソッドが成功すると、次の構造を含むレスポンスの本文が返されます。
{ "saveUri": string, "resources": { "eventTicketClasses": [ eventTicketClass resource, ... ], "eventTicketObjects": [ eventTicketObject resource, ... ], "flightClasses": [ flightClass resource, ... ], "flightObjects": [ flightObject resource, ... ], "giftCardClasses": [ giftCardClass resource, ... ], "giftCardObjects": [ giftCardObject resource, ... ], "loyaltyClasses": [ loyaltyClass resource, ... ], "loyaltyObjects": [ loyaltyObject resource, ... ], "offerClasses": [ offerClass resource, ... ], "offerObjects": [ offerObject resource, ... ], "transitClasses": [ transitClass resource, ... ], "transitObjects": [ transitObject resource, ... ] } }
saveUri
は、エンドユーザーが JWT で識別されたオブジェクトを自分の Google アカウントに保存するための URI です。この URI は、返された時点から 1 週間のみ有効です。
詳細については、JWT エンドポイントのリファレンスをご覧ください。
フロー図
フロー図については、典型的な API フローをご覧ください。
ネイティブの Android SDK を使用する
Android API を使用して Pass を Google Pay に保存できます。[Google Pay に保存] ボタンをアプリに統合すると、お客様が Pass を Google Pay に簡単に保存できるようになります。
次の手順では、ポイントパスの [Google Pay に保存] ボタンを追加する方法について説明します。
1. [Google Pay に保存] ボタンを UI に追加する
最初に、[Google Pay に保存] ボタンをアプリに追加します。Google Pay には、アプリに統合するための Android SDK ボタンが用意されています。ボタンアセットはブランド ガイドラインから入手できます。
このツールキットには、ボタンのベクター画像が含まれています。
ボタンをアプリに組み込むには、ツールキットのボタン画像をアプリケーションの res
フォルダにコピーして、次のコードを Android レイアウト ファイルに追加します。src
の正しい値に加えて、ボタンごとに固有の contentDescription
文字列と minWidth
値が必要になります。
<ImageButton android:layout_width="match_parent" android:layout_height="48dp" android:minWidth="200dp" android:clickable="true" android:src="@drawable/s2ap" />
ボタンの layout_height
は 48 dp、minWidth
は 200 dp にする必要があります。
2. loyalty クラスを作成する
クラスは Google Pay API for Passes Merchant Center から作成できます。Merchant Center の [クラス] ページで、[クラスを作成] を選択して loyaltyclass
を作成します。赤いアウトラインの入力フィールドは必須ですが、残りは省略可能です。URL のフィールドを指定する際には、リンクが一般公開されておりアクセス可能であることを確認してください。作成されたクラスは、実際のポイントを扱うオブジェクトの作成時に参照されます。
3. loyalty オブジェクトを作成する
次に loyalty オブジェクトを作成します。ここでは、Builder パターンを使用して loyalty オブジェクトを作成する例を示します。
// Define Points LoyaltyPoints points = LoyaltyPoints.newBuilder() .setLabel("Points") .setType("points") .setBalance(LoyaltyPointsBalance.newBuilder().setString("500").build()).build(); // Define Text Module Data ListtextModulesData = 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();
このオブジェクトは、次のセクションで説明する createWalletObject
呼び出しの最初のパラメータになります。
4.loyalty オブジェクトを保存するリクエストを初期化する
walletObjectsClient
クラスは、ウォレット オブジェクト機能のエントリ ポイントとして機能します。
次のコード スニペットは、アプリでクライアントをインスタンス化してオブジェクトを 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); Tasktask = walletObjectsClient.createWalletObjects(request); AutoResolveHelper.resolveTask(task, this, SAVE_TO_ANDROID);
GoogleApiClient を使用してリクエストを初期化することもできます。
Wallet.WalletObjects.createWalletObjects(googleApiClient, request, SAVE_TO_ANDROID);
5. onActivityResult を処理する
次のように onActivityResult
を定義して、操作の成功、取り消し、失敗に対応します。
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; }