將票證設為因邏輯分類而失效的票證 。開啟「Google 錢包」應用程式分頁出現「過期的票證」清單 部分。
票證會移至「過期的票證」至少必須看到下列其中一項 條件為 true:
- 當 `object.validTimeInterval.end.date 到期時,票證會移至「過期的票證」長達 24 小時不間斷
- 物件
object.state
欄位的狀態會標示為Expired
、Inactive
或Completed
。
下列程式碼範例示範如何使用 Google Cloud 控制台 Wallet API。
Java
如要開始在 Java 中整合,請參閱我們提供的完整 查看程式碼範例
/** * Expire an object. * * <p>Sets the object's state to Expired. If the valid time interval is already set, the pass will * expire automatically up to 24 hours after. * * @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 expireObject(String issuerId, String objectSuffix) throws IOException { // Check if the object exists try { service.offerobject().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); } } // Patch the object, setting the pass as expired OfferObject patchBody = new OfferObject().setState("EXPIRED"); OfferObject response = service .offerobject() .patch(String.format("%s.%s", issuerId, objectSuffix), patchBody) .execute(); System.out.println("Object expiration response"); System.out.println(response.toPrettyString()); return response.getId(); }
PHP
如要在 PHP 中開始整合,請參閱我們提供的完整 查看程式碼範例
/** * Expire an object. * * Sets the object's state to Expired. If the valid time interval is * already set, the pass will expire automatically up to 24 hours after. * * @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 expireObject(string $issuerId, string $objectSuffix) { // Check if the object exists try { $this->service->offerobject->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}"; } } // Patch the object, setting the pass as expired $patchBody = new OfferObject([ 'state' => 'EXPIRED' ]); $response = $this->service->offerobject->patch("{$issuerId}.{$objectSuffix}", $patchBody); print "Object expiration response\n"; print_r($response); return $response->id; }
Python
如要開始以 Python 進行整合,請參閱我們提供的完整 查看程式碼範例
def expire_object(self, issuer_id: str, object_suffix: str) -> str: """Expire an object. Sets the object's state to Expired. If the valid time interval is already set, the pass will expire automatically up to 24 hours after. 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.offerobject().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}' # Patch the object, setting the pass as expired patch_body = {'state': 'EXPIRED'} response = self.client.offerobject().patch( resourceId=f'{issuer_id}.{object_suffix}', body=patch_body).execute() print('Object expiration response') print(response) return f'{issuer_id}.{object_suffix}'
C#
如要開始在 C# 中整合,請參閱我們提供的完整 查看程式碼範例
/// <summary> /// Expire an object. /// <para /> /// Sets the object's state to Expired. If the valid time interval is already /// set, the pass will expire automatically up to 24 hours after. /// </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 ExpireObject(string issuerId, string objectSuffix) { // Check if the object exists Stream responseStream = service.Offerobject .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}"; } } // Patch the object, setting the pass as expired OfferObject patchBody = new OfferObject { State = "EXPIRED" }; responseStream = service.Offerobject .Patch(patchBody, $"{issuerId}.{objectSuffix}") .ExecuteAsStream(); responseReader = new StreamReader(responseStream); jsonResponse = JObject.Parse(responseReader.ReadToEnd()); Console.WriteLine("Object expiration response"); Console.WriteLine(jsonResponse.ToString()); return $"{issuerId}.{objectSuffix}"; }
Node.js
如要在節點環境中展開整合作業,請參閱我們提供的完整 查看程式碼範例
/** * Expire an object. * * Sets the object's state to Expired. If the valid time interval is * already set, the pass will expire automatically up to 24 hours after. * * @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 expireObject(issuerId, objectSuffix) { let response; // Check if the object exists try { response = await this.client.offerobject.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}`; } } // Patch the object, setting the pass as expired let patchBody = { 'state': 'EXPIRED' }; response = await this.client.offerobject.patch({ resourceId: `${issuerId}.${objectSuffix}`, requestBody: patchBody }); console.log('Object expiration response'); console.log(response); return `${issuerId}.${objectSuffix}`; }
Go
如要在 Go 中開始進行整合作業,請參閱 GitHub 上的完整程式碼範例 GitHub 上的程式碼範例。
// Expire an object. // // Sets the object's state to Expired. If the valid time interval is // already set, the pass will expire automatically up to 24 hours after. func (d *demoOffer) expireObject(issuerId, objectSuffix string) { offerObject := &walletobjects.OfferObject{ State: "EXPIRED", } res, err := d.service.Offerobject.Patch(fmt.Sprintf("%s.%s", issuerId, objectSuffix), offerObject).Do() if err != nil { log.Fatalf("Unable to patch object: %v", err) } else { fmt.Printf("Object expiration id:\n%s\n", res.Id) } }