Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHPでGoogle Walletにチケットを追加する

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

PHPでGoogle Walletにチケットを追加する

Avatar for muno92

muno92 PRO

October 25, 2023
Tweet

Resources

More Decks by muno92

Other Decks in Programming

Transcript

  1. (2023/11/3 追記) 汎用パスの使用用途 Google Wallet API を使用すると、あらかじめ定義されたさまざまなタイプのパス(ポ イントカード、クーポン、ギフトカード、イベント チケット、乗車券、搭乗券、ワクチ ンカード)でユーザーにアプローチできます。それぞれのパスには、ユースケース別の

    フィールドや機能が含まれています。 Google は、既存の 7 つのパスタイプがすべてのユースケースに適しているとは限らな いことを理解しており、汎用パスタイプを作成しました。汎用パスタイプは、その名前 が示すように、ユースケースが他の専用タイプに当てはまらない場合に使用します。 https://developers.google.com/wallet/generic?hl=ja イベント用途ならイベントチケットを使用しなければいけない (Googleの審査で指摘あり) 9
  2. 実装の大まかな流れ 1. 事前準備で作成した情報を使用して認証 2. Google Walletの「クラス」を作成し、登録 3. 各参加者情報を元にGoogle Walletの「パスオブジェクト」を作成し、登録 4.

    Google Walletに追加するためのURL https://pay.google.com/gp/v/save/{JWT} の JWTを生成 5. ブランドガイドラインからダウンロードした画像を使って「Add to Google Wallet」ボ タンを設置 ※ 独自にボタンを作ってはいけない 11
  3. 汎用パスクラスの作成・登録 汎用パスなら一意なクラスID以外の指定は不要 (むしろ、テンプレートとして項目を指定しても特に反映されず) // 自分は` 発行者アカウントの発行者ID(Issuer ID). アプリケーション名` を使用しました $classId

    = " 一意な値"; $newClass = new Google_Service_Walletobjects_GenericClass([ // クラス名・・・? 'id' => $classId ]); try { $this->service->genericclass->get($classId); return $newClass; } catch (\Google\Service\Exception $ex) { // なぜtry-catch で分岐・・・? if (empty($ex->getErrors()) || $ex->getErrors()[0]['reason'] != 'classNotFound') { // 何かしらのエラー処理 } } $this->service->genericclass->insert($newClass); 13
  4. 汎用パスオブジェクトの作成 (ソースコード) $newObject = new Google_Service_Walletobjects_GenericObject([ 'id' => ' オブジェクトID',

    'classId' => ' クラスID', 'heroImage' => new Google_Service_Walletobjects_Image(['sourceUri' => new Google_Service_Walletobjects_ImageUri([ 'uri' => ' カンファレンスバナーのURL', ])]), 'barcode' => new Google_Service_Walletobjects_Barcode(['type' => 'QR_CODE', 'value' => ' 受付用QR に埋め込む文字列']), 'cardTitle' => new Google_Service_Walletobjects_LocalizedString([ 'defaultValue' => new Google_Service_Walletobjects_TranslatedString([ 'language' => 'ja-JP', 'value' => ' カンファレンス名' ]) ]), 'header' => new Google_Service_Walletobjects_LocalizedString([ 'defaultValue' => new Google_Service_Walletobjects_TranslatedString([ 'language' => 'ja-JP', 'value' => ' 参加者名' ]) ]), 'subheader' => new Google_Service_Walletobjects_LocalizedString([ 'defaultValue' => new Google_Service_Walletobjects_TranslatedString([ 'language' => 'ja-JP', 'value' => " イベント種別 チケット番号" ]) ]), 'hexBackgroundColor' => ' チケット上部の背景色', // 未指定の場合はロゴの主要な色が自動設定される 'logo' => new Google_Service_Walletobjects_Image(['sourceUri' => new Google_Service_Walletobjects_ImageUri([ 'uri' => ' カンファレンスロゴのURL', ])]) ]); // 登録の流れはクラスと同じ 14
  5. JWTの生成 $serviceAccount = json_decode(file_get_contents('JSON 形式で作成したサービスアカウントの鍵ファイル'), true); $claims = [ 'iss'

    => $serviceAccount['client_email'], 'aud' => 'google', 'origins' => [], 'typ' => 'savetowallet', 'payload' => [ 'genericClasses' => [ // 作成したクラス ], 'genericObjects' => [ // 作成したオブジェクト ] ] ]; return JWT::encode( $claims, $serviceAccount['private_key'], 'RS256' ); 17
  6. 補足1: 使用ライブラリ https://github.com/google-wallet/rest-samples/tree/main/php compsoerで以下のライブラリを追加 { "require": { "google/auth": "^1.18", "guzzlehttp/guzzle":

    "*", "google/apiclient": "^2.12" } } Google Wallet API クライアント をダウンロード・任意のパスに設置(!) JavaやC#も同様だったので、Google社内で何か事情があるのかも 1ファイルにWallet関係のクラスが詰まっている・・・ それ以外の言語のサンプルは素のテキスト・オブジェクトを使っているので、その 方式を選択するのもアリ 21
  7. 補足3: クラス・オブジェクトの存在確認 try { $this->service->genericclass->get($classId); return $newClass; } catch (\Google\Service\Exception

    $ex) { // なぜtry-catch で分岐・・・? if (empty($ex->getErrors()) || $ex->getErrors()[0]['reason'] != 'classNotFound') { // 何かしらのエラー処理 } } ↑コレ getメソッドの結果 存在する -> クラスやオブジェクトが返ってくる 存在しない -> 例外発生 なので、try-catchでの分岐が必要 23