forked from Intel-Inc/wallet-samples-1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo_offer.php
539 lines (498 loc) · 18.5 KB
/
demo_offer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<?php
/*
* Copyright 2022 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require __DIR__ . '/vendor/autoload.php';
// Download the PHP client library from the following URL
// https://developers.google.com/wallet/generic/resources/libraries
require __DIR__ . '/lib/Walletobjects.php';
// [START setup]
// [START imports]
use Firebase\JWT\JWT;
use Google\Auth\Credentials\ServiceAccountCredentials;
use Google\Client as Google_Client;
// [END imports]
/** Demo class for creating and managing Offers in Google Wallet. */
class DemoOffer
{
/**
* Path to service account key file from Google Cloud Console. Environment
* variable: GOOGLE_APPLICATION_CREDENTIALS.
*/
public string $keyFilePath;
/**
* Service account credentials for Google Wallet APIs.
*/
public ServiceAccountCredentials $credentials;
/**
* Google Wallet service client.
*/
public Google_Service_Walletobjects $service;
public function __construct()
{
$this->keyFilePath = getenv('GOOGLE_APPLICATION_CREDENTIALS') ?: '/path/to/key.json';
}
// [END setup]
// [START auth]
/**
* Create authenticated HTTP client using a service account file.
*/
public function auth()
{
$scope = 'https://www.googleapis.com/auth/wallet_object.issuer';
$this->credentials = new ServiceAccountCredentials(
$scope,
$this->keyFilePath
);
// Initialize Google Wallet API service
$this->client = new Google_Client();
$this->client->setApplicationName('APPLICATION_NAME');
$this->client->setScopes($scope);
$this->client->setAuthConfig($this->keyFilePath);
$this->service = new Google_Service_Walletobjects($this->client);
}
// [END auth]
// [START class]
/**
* Create a class via the API. This can also be done in the Google Pay and Wallet console.
*
* @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 createOfferClass(string $issuerId, string $classSuffix)
{
// See link below for more information on required properties
// https://developers.google.com/wallet/retail/offers/rest/v1/offerclass
$offerClass = new Google_Service_Walletobjects_OfferClass([
'id' => "{$issuerId}.{$classSuffix}",
'issuerName' => 'Issuer name',
'reviewStatus' => 'UNDER_REVIEW',
'provider' => 'Provider name',
'title' => 'Offer title',
'redemptionChannel' => 'ONLINE',
]);
try {
$response = $this->service->offerclass->insert($offerClass);
print "Class insert response\n";
print_r($response);
return $response->id;
} catch (Google\Service\Exception $ex) {
if ($ex->getCode() == 409) {
print "Class {$issuerId}.{$classSuffix} already exists";
return;
}
// Something else went wrong
print $ex->getTraceAsString();
}
}
// [END class]
// [START object]
/**
* Create an object via the API.
*
* @param string $issuerId The issuer ID being used for this request.
* @param string $classSuffix Developer-defined unique ID for this pass class.
* @param string $userId Developer-defined user ID for this pass object.
*
* @return string The pass object ID: "{issuer_id}.{user_id}"
*/
public function createOfferObject(string $issuerId, string $classSuffix, string $userId)
{
// Generate the object ID
// Should only include alphanumeric characters, '.', '_', or '-'
$newUserId = preg_replace('/[^\w.-]/i', '_', $userId);
$objectId = "{$issuerId}.{$newUserId}";
// See link below for more information on required properties
// https://developers.google.com/wallet/retail/offers/rest/v1/offerobject
$offerObject = new Google_Service_Walletobjects_OfferObject([
'id' => "{$objectId}",
'classId' => "{$issuerId}.{$classSuffix}",
'state' => 'ACTIVE',
'heroImage' => new Google_Service_Walletobjects_Image([
'sourceUri' => new Google_Service_Walletobjects_ImageUri([
'uri' => 'https://farm4.staticflickr.com/3723/11177041115_6e6a3b6f49_o.jpg',
]),
'contentDescription' => new Google_Service_Walletobjects_LocalizedString([
'defaultValue' => new Google_Service_Walletobjects_TranslatedString([
'language' => 'en-US',
'value' => 'Hero image description',
]),
]),
]),
'textModulesData' => [
new Google_Service_Walletobjects_TextModuleData([
'header' => 'Text module header',
'body' => 'Text module body',
'id' => 'TEXT_MODULE_ID',
]),
],
'linksModuleData' => new Google_Service_Walletobjects_LinksModuleData([
'uris' => [
new Google_Service_Walletobjects_Uri([
'uri' => 'http://maps.google.com/',
'description' => 'Link module URI description',
'id' => 'LINK_MODULE_URI_ID',
]),
new Google_Service_Walletobjects_Uri([
'uri' => 'tel:6505555555',
'description' => 'Link module tel description',
'id' => 'LINK_MODULE_TEL_ID',
]),
],
]),
'imageModulesData' => [
new Google_Service_Walletobjects_ImageModuleData([
'mainImage' => new Google_Service_Walletobjects_Image([
'sourceUri' => new Google_Service_Walletobjects_ImageUri([
'uri' => 'http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg',
]),
'contentDescription' => new Google_Service_Walletobjects_LocalizedString([
'defaultValue' => new Google_Service_Walletobjects_TranslatedString([
'language' => 'en-US',
'value' => 'Image module description',
]),
]),
]),
'id' => 'IMAGE_MODULE_ID',
])
],
'barcode' => new Google_Service_Walletobjects_Barcode([
'type' => 'QR_CODE',
'value' => 'QR code value',
]),
'locations' => [
new Google_Service_Walletobjects_LatLongPoint([
'latitude' => 37.424015499999996,
'longitude' => -122.09259560000001,
]),
],
'validTimeInterval' => new Google_Service_Walletobjects_TimeInterval([
'start' => new Google_Service_Walletobjects_DateTime([
'date' => '2023-06-12T23:20:50.52Z',
]),
'end' => new Google_Service_Walletobjects_DateTime([
'date' => '2023-12-12T23:20:50.52Z',
]),
]),
]);
try {
$response = $this->service->offerobject->insert($offerObject);
print "Object insert response\n";
print_r($response);
return $response->id;
} catch (Google\Service\Exception $ex) {
if ($ex->getCode() == 409) {
print "Object {$objectId} already exists";
return;
}
// Something else went wrong
print $ex->getTraceAsString();
}
}
// [END object]
// [START jwt]
/**
* Generate a signed JWT that creates a new pass class and object.
*
* When the user opens the "Add to Google Wallet" URL and saves the pass to
* their wallet, the pass class and object defined in the JWT are
* created.This allows you to create multiple pass classes and objects in
* one API call when the user saves the pass to their wallet.
*
* @param string $issuerId The issuer ID being used for this request.
* @param string $classSuffix Developer-defined class ID for this class.
* @param string $userId Developer-defined user ID for this object.
*
* @return string An "Add to Google Wallet" link.
*/
public function createJwtSaveUrl(string $issuerId, string $classSuffix, string $userId)
{
// Generate the object ID
// Should only include alphanumeric characters, '.', '_', or '-'
$newUserId = preg_replace('/[^\w.-]/i', '_', $userId);
$objectId = "{$issuerId}.{$newUserId}";
// See link below for more information on required properties
// https://developers.google.com/wallet/retail/offers/rest/v1/offerclass
$offerClass = new Google_Service_Walletobjects_OfferClass([
'id' => "{$issuerId}.{$classSuffix}",
'issuerName' => 'Issuer name',
'reviewStatus' => 'UNDER_REVIEW',
'provider' => 'Provider name',
'title' => 'Offer title',
'redemptionChannel' => 'ONLINE',
]);
// See link below for more information on required properties
// https://developers.google.com/wallet/retail/offers/rest/v1/offerobject
$offerObject = new Google_Service_Walletobjects_OfferObject([
'id' => "{$objectId}",
'classId' => "{$issuerId}.{$classSuffix}",
'state' => 'ACTIVE',
'heroImage' => new Google_Service_Walletobjects_Image([
'sourceUri' => new Google_Service_Walletobjects_ImageUri([
'uri' => 'https://farm4.staticflickr.com/3723/11177041115_6e6a3b6f49_o.jpg',
]),
'contentDescription' => new Google_Service_Walletobjects_LocalizedString([
'defaultValue' => new Google_Service_Walletobjects_TranslatedString([
'language' => 'en-US',
'value' => 'Hero image description',
]),
]),
]),
'textModulesData' => [
new Google_Service_Walletobjects_TextModuleData([
'header' => 'Text module header',
'body' => 'Text module body',
'id' => 'TEXT_MODULE_ID',
]),
],
'linksModuleData' => new Google_Service_Walletobjects_LinksModuleData([
'uris' => [
new Google_Service_Walletobjects_Uri([
'uri' => 'http://maps.google.com/',
'description' => 'Link module URI description',
'id' => 'LINK_MODULE_URI_ID',
]),
new Google_Service_Walletobjects_Uri([
'uri' => 'tel:6505555555',
'description' => 'Link module tel description',
'id' => 'LINK_MODULE_TEL_ID',
]),
],
]),
'imageModulesData' => [
new Google_Service_Walletobjects_ImageModuleData([
'mainImage' => new Google_Service_Walletobjects_Image([
'sourceUri' => new Google_Service_Walletobjects_ImageUri([
'uri' => 'http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg',
]),
'contentDescription' => new Google_Service_Walletobjects_LocalizedString([
'defaultValue' => new Google_Service_Walletobjects_TranslatedString([
'language' => 'en-US',
'value' => 'Image module description',
]),
]),
]),
'id' => 'IMAGE_MODULE_ID',
])
],
'barcode' => new Google_Service_Walletobjects_Barcode([
'type' => 'QR_CODE',
'value' => 'QR code value',
]),
'locations' => [
new Google_Service_Walletobjects_LatLongPoint([
'latitude' => 37.424015499999996,
'longitude' => -122.09259560000001,
]),
],
'validTimeInterval' => new Google_Service_Walletobjects_TimeInterval([
'start' => new Google_Service_Walletobjects_DateTime([
'date' => '2023-06-12T23:20:50.52Z',
]),
'end' => new Google_Service_Walletobjects_DateTime([
'date' => '2023-12-12T23:20:50.52Z',
]),
]),
]);
// Create the JWT as an array of key/value pairs
$serviceAccount = json_decode(file_get_contents($this->keyFilePath), true);
$claims = [
'iss' => $serviceAccount['client_email'],
'aud' => 'google',
'origins' => ['www.example.com'],
'typ' => 'savetowallet',
'payload' => [
'offerClasses' => [
$offerClass,
],
'offerObjects' => [
$offerObject,
],
],
];
// The service account credentials are used to sign the JWT
$token = JWT::encode(
$claims,
$serviceAccount['private_key'],
'RS256'
);
print "Add to Google Wallet link\n";
print "https://pay.google.com/gp/v/save/{$token}";
return "https://pay.google.com/gp/v/save/{$token}";
}
// [END jwt]
// [START createIssuer]
/**
* Create a new Google Wallet issuer account.
*
* @param string $issuerName The issuer's name.
* @param string $issuerEmail The issuer's email address.
*/
public function createIssuerAccount(string $issuerName, string $issuerEmail)
{
// New issuer information
$issuer = new Google_Service_Walletobjects_Issuer([
'name' => $issuerName,
'contactInfo' => new Google_Service_Walletobjects_IssuerContactInfo([
'email' => $issuerEmail,
]),
]);
$response = $this->service->issuer->insert($issuer);
print "Issuer insert response\n";
print_r($response);
}
// [END createIssuer]
// [START updatePermissions]
/**
* Update permissions for an existing Google Wallet issuer account.
* **Warning:** This operation overwrites all existing permissions!
*
* Example permissions list argument below. Copy the entry as
* needed for each email address that will need access. Supported
* values for role are: 'READER', 'WRITER', and 'OWNER'
*
* $permissions = array(
* new Google_Service_Walletobjects_Permission([
* 'emailAddress' => 'email-address',
* 'role' => 'OWNER',
* ]),
* );
*
* @param string $issuerId The issuer ID being used for this request.
* @param array $permissions The list of email addresses and roles to assign.
*/
public function updateIssuerAccountPermissions(string $issuerId, array $permissions)
{
// Make the PUT request
$response = $this->service->permissions->update(
$issuerId,
new Google_Service_Walletobjects_Permissions([
'issuerId' => $issuerId,
'permissions' => $permissions,
])
);
print "Permissions update response\n";
print_r($response);
}
// [END updatePermissions]
// [START batch]
/**
* Batch create Google Wallet objects from an existing class.
*
* @param string $issuerId The issuer ID being used for this request.
* @param string $classSuffix Developer-defined class ID for this class.
*/
public function batchCreateOfferObjects(string $issuerId, string $classSuffix)
{
// Update the client to enable batch requests
$this->client->setUseBatch(true);
$batch = $this->service->createBatch();
// Example: Generate three new pass objects
for ($i = 0; $i < 3; $i++) {
// Generate a random user ID
$userId = preg_replace('/[^\w.-]/i', '_', uniqid());
// Generate a random object ID with the user ID
// Should only include alphanumeric characters, '.', '_', or '-'
$objectId = "{$issuerId}.{$userId}";
// See link below for more information on required properties
// https://developers.google.com/wallet/retail/offers/rest/v1/offerobject
$offerObject = new Google_Service_Walletobjects_OfferObject([
'id' => "{$objectId}",
'classId' => "{$issuerId}.{$classSuffix}",
'state' => 'ACTIVE',
'heroImage' => new Google_Service_Walletobjects_Image([
'sourceUri' => new Google_Service_Walletobjects_ImageUri([
'uri' => 'https://farm4.staticflickr.com/3723/11177041115_6e6a3b6f49_o.jpg',
]),
'contentDescription' => new Google_Service_Walletobjects_LocalizedString([
'defaultValue' => new Google_Service_Walletobjects_TranslatedString([
'language' => 'en-US',
'value' => 'Hero image description',
]),
]),
]),
'textModulesData' => [
new Google_Service_Walletobjects_TextModuleData([
'header' => 'Text module header',
'body' => 'Text module body',
'id' => 'TEXT_MODULE_ID',
]),
],
'linksModuleData' => new Google_Service_Walletobjects_LinksModuleData([
'uris' => [
new Google_Service_Walletobjects_Uri([
'uri' => 'http://maps.google.com/',
'description' => 'Link module URI description',
'id' => 'LINK_MODULE_URI_ID',
]),
new Google_Service_Walletobjects_Uri([
'uri' => 'tel:6505555555',
'description' => 'Link module tel description',
'id' => 'LINK_MODULE_TEL_ID',
]),
],
]),
'imageModulesData' => [
new Google_Service_Walletobjects_ImageModuleData([
'mainImage' => new Google_Service_Walletobjects_Image([
'sourceUri' => new Google_Service_Walletobjects_ImageUri([
'uri' => 'http://farm4.staticflickr.com/3738/12440799783_3dc3c20606_b.jpg',
]),
'contentDescription' => new Google_Service_Walletobjects_LocalizedString([
'defaultValue' => new Google_Service_Walletobjects_TranslatedString([
'language' => 'en-US',
'value' => 'Image module description',
]),
]),
]),
'id' => 'IMAGE_MODULE_ID',
])
],
'barcode' => new Google_Service_Walletobjects_Barcode([
'type' => 'QR_CODE',
'value' => 'QR code value',
]),
'locations' => [
new Google_Service_Walletobjects_LatLongPoint([
'latitude' => 37.424015499999996,
'longitude' => -122.09259560000001,
]),
],
'validTimeInterval' => new Google_Service_Walletobjects_TimeInterval([
'start' => new Google_Service_Walletobjects_DateTime([
'date' => '2023-06-12T23:20:50.52Z',
]),
'end' => new Google_Service_Walletobjects_DateTime([
'date' => '2023-12-12T23:20:50.52Z',
]),
]),
]);
$batch->add($this->service->offerobject->insert($offerObject));
}
// Make the batch request
$batchResponse = $batch->execute();
print "Batch insert response\n";
foreach ($batchResponse as $key => $value) {
if ($value instanceof Google_Service_Exception) {
print_r($value->getErrors());
continue;
}
print "{$value->getId()}\n";
}
}
// [END batch]
}