及时更新卡券是与客户互动并打造良好体验的重要方法。
有两个资源可以更新:FlightClass
和 FlightObject
。
最佳做法
以下列表包含在更新登机牌类和对象时要考虑的实用信息:
- 如果要更新整个类或对象,请发送
update
请求。如果您要更新某个类或对象中的少量字段,请发送patch
请求。 - 当您发出
update
请求时,整个对象或类都会更新。这意味着请求中未包含的所有字段都将被清除。 在发送update
请求之前,我们建议您先发送GET
请求,以确保使用的是最新版本,且您的请求中包含所有预期字段。 - 在发出
patch
请求时,只有修补后的字段才会更新。在发送patch
请求之前,可以考虑选择性地发送GET
请求,将您的更改与最新版本进行比较。 - 发出更新数组的
patch
请求时,原始数组会替换为请求正文中包含的数组。您无法单独修改数组中的元素。 - 在某些情况下,您可能不知道何时会发生变化或何时触发更新。考虑定期为所有类和对象安排
update
或patch
请求。
更新卡券类
使用 Google 电子钱包商家控制台
卡券类(而非对象)可以直接在 Google Pay & Wallet Console 中进行修改。
- 前往控制台
- 选择 Google Wallet API
- 选择要修改的课程
- 选择修改
- 更新类属性
- 选择保存。
保存更改后,系统会自动为任何航班持有者更新该类。
使用 Google Wallet API
更新 FlightClass
会影响所有已使用此类配置登机牌的用户。例如,要更新登机牌的徽标,您可以通过以下任一端点向 Google Wallet API 提交 update
或 patch
请求。resourceId
的值将是类 ID (ISSUER_ID.CLASS_SUFFIX
)。
# Update
PUT https://walletobjects.googleapis.com/walletobjects/v1/flightclass/{resourceId}
# Patch
PATCH https://walletobjects.googleapis.com/walletobjects/v1/flightclass/{resourceId}
如需了解详情,请参阅 API 参考文档。
Java
如需开始使用 Java 进行集成,请参阅 GitHub 上的完整代码示例。
/** * Update a class. * * <p><strong>Warning:</strong> This replaces all existing class attributes! * * @param issuerId The issuer ID being used for this request. * @param classSuffix Developer-defined unique ID for this pass class. * @return The pass class ID: "{issuerId}.{classSuffix}" */ public String updateClass(String issuerId, String classSuffix) throws IOException { FlightClass updatedClass; // Check if the class exists try { updatedClass = service.flightclass().get(String.format("%s.%s", issuerId, classSuffix)).execute(); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Class does not exist System.out.printf("Class %s.%s not found!%n", issuerId, classSuffix); return String.format("%s.%s", issuerId, classSuffix); } else { // Something else went wrong... ex.printStackTrace(); return String.format("%s.%s", issuerId, classSuffix); } } // Class exists // Update the class by adding a homepage updatedClass.setHomepageUri( new Uri() .setUri("https://developers.google.com/wallet") .setDescription("Homepage description")); // Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates updatedClass.setReviewStatus("UNDER_REVIEW"); FlightClass response = service .flightclass() .update(String.format("%s.%s", issuerId, classSuffix), updatedClass) .execute(); System.out.println("Class update response"); System.out.println(response.toPrettyString()); return response.getId(); }
PHP
如要开始使用 PHP 进行集成,请参阅 GitHub 上的完整代码示例。
/** * Update a class. * * **Warning:** This replaces all existing class attributes! * * @param string $issuerId The issuer ID being used for this request. * @param string $classSuffix Developer-defined unique ID for this pass class. * * @return string The pass class ID: "{$issuerId}.{$classSuffix}" */ public function updateClass(string $issuerId, string $classSuffix) { // Check if the class exists try { $updatedClass = $this->service->flightclass->get("{$issuerId}.{$classSuffix}"); } catch (Google\Service\Exception $ex) { if (!empty($ex->getErrors()) && $ex->getErrors()[0]['reason'] == 'classNotFound') { // Class does not exist print("Class {$issuerId}.{$classSuffix} not found!"); return "{$issuerId}.{$classSuffix}"; } else { // Something else went wrong... print_r($ex); return "{$issuerId}.{$classSuffix}"; } } // Update the class by adding a homepage $updatedClass->setHomepageUri(new Uri([ 'uri' => 'https://developers.google.com/wallet', 'description' => 'Homepage description' ])); // Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates $updatedClass->setReviewStatus('UNDER_REVIEW'); $response = $this->service->flightclass->update("{$issuerId}.{$classSuffix}", $updatedClass); print "Class update response\n"; print_r($response); return $response->id; }
Python
如需开始使用 Python 进行集成,请参阅 GitHub 上的完整代码示例。
def update_class(self, issuer_id: str, class_suffix: str) -> str: """Update a class. **Warning:** This replaces all existing class attributes! Args: issuer_id (str): The issuer ID being used for this request. class_suffix (str): Developer-defined unique ID for this pass class. Returns: The pass class ID: f"{issuer_id}.{class_suffix}" """ # Check if the class exists try: response = self.client.flightclass().get(resourceId=f'{issuer_id}.{class_suffix}').execute() except HttpError as e: if e.status_code == 404: print(f'Class {issuer_id}.{class_suffix} not found!') return f'{issuer_id}.{class_suffix}' else: # Something else went wrong... print(e.error_details) return f'{issuer_id}.{class_suffix}' # Class exists updated_class = response # Update the class by adding a homepage updated_class['homepageUri'] = { 'uri': 'https://developers.google.com/wallet', 'description': 'Homepage description' } # Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates updated_class['reviewStatus'] = 'UNDER_REVIEW' response = self.client.flightclass().update( resourceId=f'{issuer_id}.{class_suffix}', body=updated_class).execute() print('Class update response') print(response) return f'{issuer_id}.{class_suffix}'
C#
如需开始使用 C# 进行集成,请参阅 GitHub 上的完整代码示例。
/// <summary> /// Update a class. /// <para /> /// <strong>Warning:</strong> This replaces all existing class attributes! /// </summary> /// <param name="issuerId">The issuer ID being used for this request.</param> /// <param name="classSuffix">Developer-defined unique ID for this pass class.</param> /// <returns>The pass class ID: "{issuerId}.{classSuffix}"</returns> public string UpdateClass(string issuerId, string classSuffix) { // Check if the class exists Stream responseStream = service.Flightclass .Get($"{issuerId}.{classSuffix}") .ExecuteAsStream(); StreamReader responseReader = new StreamReader(responseStream); JObject jsonResponse = JObject.Parse(responseReader.ReadToEnd()); if (jsonResponse.ContainsKey("error")) { if (jsonResponse["error"].Value<int>("code") == 404) { // Class does not exist Console.WriteLine($"Class {issuerId}.{classSuffix} not found!"); return $"{issuerId}.{classSuffix}"; } else { // Something else went wrong... Console.WriteLine(jsonResponse.ToString()); return $"{issuerId}.{classSuffix}"; } } // Class exists FlightClass updatedClass = JsonConvert.DeserializeObject<FlightClass>(jsonResponse.ToString()); // Update the class by adding a homepage updatedClass.HomepageUri = new Google.Apis.Walletobjects.v1.Data.Uri { UriValue = "https://developers.google.com/wallet", Description = "Homepage description" }; // Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates updatedClass.ReviewStatus = "UNDER_REVIEW"; responseStream = service.Flightclass .Update(updatedClass, $"{issuerId}.{classSuffix}") .ExecuteAsStream(); responseReader = new StreamReader(responseStream); jsonResponse = JObject.Parse(responseReader.ReadToEnd()); Console.WriteLine("Class update response"); Console.WriteLine(jsonResponse.ToString()); return $"{issuerId}.{classSuffix}"; }
Node.js
如需开始在 Node 中进行集成,请参阅 GitHub 上的完整代码示例。
/** * Update a class. * * **Warning:** This replaces all existing class attributes! * * @param {string} issuerId The issuer ID being used for this request. * @param {string} classSuffix Developer-defined unique ID for this pass class. * * @returns {string} The pass class ID: `${issuerId}.${classSuffix}` */ async updateClass(issuerId, classSuffix) { let response; // Check if the class exists try { response = await this.client.flightclass.get({ resourceId: `${issuerId}.${classSuffix}` }); } catch (err) { if (err.response && err.response.status === 404) { console.log(`Class ${issuerId}.${classSuffix} not found!`); return `${issuerId}.${classSuffix}`; } else { // Something else went wrong... console.log(err); return `${issuerId}.${classSuffix}`; } } // Class exists let updatedClass = response.data; // Update the class by adding a homepage updatedClass['homepageUri'] = { 'uri': 'https://developers.google.com/wallet', 'description': 'Homepage description' }; // Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates updatedClass['reviewStatus'] = 'UNDER_REVIEW'; response = await this.client.flightclass.update({ resourceId: `${issuerId}.${classSuffix}`, requestBody: updatedClass }); console.log('Class update response'); console.log(response); return `${issuerId}.${classSuffix}`; }
更新卡券对象
更新单个 FlightObject
只会影响已预配该特定对象的用户。您应定期更新各个登机牌,以反映会影响客户的变化并保持客户活跃。resourceId
的值将是对象 ID (ISSUER_ID.OBJECT_SUFFIX
)。
# Update
PUT https://walletobjects.googleapis.com/walletobjects/v1/flightobject/{resourceId}
# Patch
PATCH https://walletobjects.googleapis.com/walletobjects/v1/flightobject/{resourceId}
如需了解详情,请参阅 API 参考文档。
Java
如需开始使用 Java 进行集成,请参阅 GitHub 上的完整代码示例。
/** * Update an object. * * <p><strong>Warning:</strong> This replaces all existing object attributes! * * @param issuerId The issuer ID being used for this request. * @param objectSuffix Developer-defined unique ID for this pass object. * @return The pass object ID: "{issuerId}.{objectSuffix}" */ public String updateObject(String issuerId, String objectSuffix) throws IOException { FlightObject updatedObject; // Check if the object exists try { updatedObject = service.flightobject().get(String.format("%s.%s", issuerId, objectSuffix)).execute(); } catch (GoogleJsonResponseException ex) { if (ex.getStatusCode() == 404) { // Object does not exist System.out.printf("Object %s.%s not found!%n", issuerId, objectSuffix); return String.format("%s.%s", issuerId, objectSuffix); } else { // Something else went wrong... ex.printStackTrace(); return String.format("%s.%s", issuerId, objectSuffix); } } // Object exists // Update the object by adding a link Uri newLink = new Uri() .setUri("https://developers.google.com/wallet") .setDescription("New link description"); if (updatedObject.getLinksModuleData() == null) { // LinksModuleData was not set on the original object updatedObject.setLinksModuleData(new LinksModuleData().setUris(List.of(newLink))); } else { updatedObject.getLinksModuleData().getUris().add(newLink); } FlightObject response = service .flightobject() .update(String.format("%s.%s", issuerId, objectSuffix), updatedObject) .execute(); System.out.println("Object update response"); System.out.println(response.toPrettyString()); return response.getId(); }
PHP
如要开始使用 PHP 进行集成,请参阅 GitHub 上的完整代码示例。
/** * Update an object. * * **Warning:** This replaces all existing object attributes! * * @param string $issuerId The issuer ID being used for this request. * @param string $objectSuffix Developer-defined unique ID for this pass object. * * @return string The pass object ID: "{$issuerId}.{$objectSuffix}" */ public function updateObject(string $issuerId, string $objectSuffix) { // Check if the object exists try { $updatedObject = $this->service->flightobject->get("{$issuerId}.{$objectSuffix}"); } catch (Google\Service\Exception $ex) { if (!empty($ex->getErrors()) && $ex->getErrors()[0]['reason'] == 'resourceNotFound') { print("Object {$issuerId}.{$objectSuffix} not found!"); return "{$issuerId}.{$objectSuffix}"; } else { // Something else went wrong... print_r($ex); return "{$issuerId}.{$objectSuffix}"; } } // Update the object by adding a link $newLink = new Uri([ 'uri' => 'https://developers.google.com/wallet', 'description' => 'New link description' ]); $linksModuleData = $updatedObject->getLinksModuleData(); if (is_null($linksModuleData)) { // LinksModuleData was not set on the original object $linksModuleData = new LinksModuleData([ 'uris' => [] ]); } $uris = $linksModuleData->getUris(); array_push( $uris, $newLink ); $linksModuleData->setUris($uris); $updatedObject->setLinksModuleData($linksModuleData); $response = $this->service->flightobject->update("{$issuerId}.{$objectSuffix}", $updatedObject); print "Object update response\n"; print_r($response); return $response->id; }
Python
如需开始使用 Python 进行集成,请参阅 GitHub 上的完整代码示例。
def update_object(self, issuer_id: str, object_suffix: str) -> str: """Update an object. **Warning:** This replaces all existing object attributes! Args: issuer_id (str): The issuer ID being used for this request. object_suffix (str): Developer-defined unique ID for the pass object. Returns: The pass object ID: f"{issuer_id}.{object_suffix}" """ # Check if the object exists try: response = self.client.flightobject().get(resourceId=f'{issuer_id}.{object_suffix}').execute() except HttpError as e: if e.status_code == 404: print(f'Object {issuer_id}.{object_suffix} not found!') return f'{issuer_id}.{object_suffix}' else: # Something else went wrong... print(e.error_details) return f'{issuer_id}.{object_suffix}' # Object exists updated_object = response # Update the object by adding a link new_link = { 'uri': 'https://developers.google.com/wallet', 'description': 'New link description' } if not updated_object.get('linksModuleData'): updated_object['linksModuleData'] = {'uris': []} updated_object['linksModuleData']['uris'].append(new_link) response = self.client.flightobject().update( resourceId=f'{issuer_id}.{object_suffix}', body=updated_object).execute() print('Object update response') print(response) return f'{issuer_id}.{object_suffix}'
C#
如需开始使用 C# 进行集成,请参阅 GitHub 上的完整代码示例。
/// <summary> /// Update an object. /// <para /> /// <strong>Warning:</strong> This replaces all existing class attributes! /// </summary> /// <param name="issuerId">The issuer ID being used for this request.</param> /// <param name="objectSuffix">Developer-defined unique ID for this pass object.</param> /// <returns>The pass object ID: "{issuerId}.{objectSuffix}"</returns> public string UpdateObject(string issuerId, string objectSuffix) { // Check if the object exists Stream responseStream = service.Flightobject .Get($"{issuerId}.{objectSuffix}") .ExecuteAsStream(); StreamReader responseReader = new StreamReader(responseStream); JObject jsonResponse = JObject.Parse(responseReader.ReadToEnd()); if (jsonResponse.ContainsKey("error")) { if (jsonResponse["error"].Value<int>("code") == 404) { // Object does not exist Console.WriteLine($"Object {issuerId}.{objectSuffix} not found!"); return $"{issuerId}.{objectSuffix}"; } else { // Something else went wrong... Console.WriteLine(jsonResponse.ToString()); return $"{issuerId}.{objectSuffix}"; } } // Object exists FlightObject updatedObject = JsonConvert.DeserializeObject<FlightObject>(jsonResponse.ToString()); // Update the object by adding a link Google.Apis.Walletobjects.v1.Data.Uri newLink = new Google.Apis.Walletobjects.v1.Data.Uri { UriValue = "https://developers.google.com/wallet", Description = "New link description" }; if (updatedObject.LinksModuleData == null) { // LinksModuleData was not set on the original object updatedObject.LinksModuleData = new LinksModuleData { Uris = new List<Google.Apis.Walletobjects.v1.Data.Uri>() }; } updatedObject.LinksModuleData.Uris.Add(newLink); responseStream = service.Flightobject .Update(updatedObject, $"{issuerId}.{objectSuffix}") .ExecuteAsStream(); responseReader = new StreamReader(responseStream); jsonResponse = JObject.Parse(responseReader.ReadToEnd()); Console.WriteLine("Object update response"); Console.WriteLine(jsonResponse.ToString()); return $"{issuerId}.{objectSuffix}"; }
Node.js
如需开始在 Node 中进行集成,请参阅 GitHub 上的完整代码示例。
/** * Update an object. * * **Warning:** This replaces all existing object attributes! * * @param {string} issuerId The issuer ID being used for this request. * @param {string} objectSuffix Developer-defined unique ID for the pass object. * * @returns {string} The pass object ID: `${issuerId}.${objectSuffix}` */ async updateObject(issuerId, objectSuffix) { let response; // Check if the object exists try { response = await this.client.flightobject.get({ resourceId: `${issuerId}.${objectSuffix}` }); } catch (err) { if (err.response && err.response.status === 404) { console.log(`Object ${issuerId}.${objectSuffix} not found!`); return `${issuerId}.${objectSuffix}`; } else { // Something else went wrong... console.log(err); return `${issuerId}.${objectSuffix}`; } } // Object exists let updatedObject = response.data; // Update the object by adding a link let newLink = { 'uri': 'https://developers.google.com/wallet', 'description': 'New link description' } if (updatedObject['linksModuleData'] === undefined) { updatedObject['linksModuleData'] = { 'uris': [newLink] }; } else { updatedObject['linksModuleData']['uris'].push(newLink); } response = await this.client.flightobject.update({ resourceId: `${issuerId}.${objectSuffix}`, requestBody: updatedObject }); console.log('Object update response'); console.log(response); return `${issuerId}.${objectSuffix}`; }
航班动态数据源
如果 class.localScheduledDepartureDateTime
给出的时间是过去 24 小时内的时间或接下来 48 小时内的时间,则系统会向用户显示航班动态卡片。在这种情况下,Google 钱包可以显示来自 Google 机票的数据或 Google 钱包卡券中提供的信息。使用哪个来源取决于以下因素:
-
如果您未提供
class.localEstimatedOrActualDepartureDateTime
,则系统会使用 Google 机票。在这种情况下,系统会忽略您设置的任何class.flightStatus
。例如,如果航班延迟,用户会在 Google 钱包应用的“首页”标签页下看到一张显示新的出发时间的卡片。“卡券”标签页下也会向用户显示类似的延迟卡片。
- 如果您提供了
class.localEstimatedOrActualDepartureDateTime
,但未提供class.flightStatus
,则所提供的时间将用于确定航班是否发生了延迟。然后,系统会根据以下逻辑通过卡片向用户显示航班状态:- 如果
class.localEstimatedOrActualDepartureDateTime
晚于class.localScheduledDepartureDateTime
,则向用户显示说明航班延误的卡片。 - 如果
class.localEstimatedOrActualDepartureDateTime
不大于class.localScheduledDepartureDateTime
,则向用户显示包含航班信息但没有任何状态消息的卡片。
- 如果
如果您不想使用 Google 机票作为航班信息来源,请务必提供 FlightClass
的 flightStatus
、localScheduledDepartureDateTime
和 localEstimatedOrActualDepartureDateTime
。而只会使用您的数据。另外,如果您在 FlightClass
中使用 ICAO 机场代码,而不是 IATA 代码,则系统不会将 Google 机票用作航班信息来源。
当某些字段发生更改时,用户会收到有关更改的推送通知。如需了解详情,请参阅接收航班动态通知。