-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloyaltyPass.js
More file actions
391 lines (346 loc) · 11.6 KB
/
Copy pathloyaltyPass.js
File metadata and controls
391 lines (346 loc) · 11.6 KB
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
/*
* 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.
*/
const { walletobjects, auth: { GoogleAuth } } = require("@googleapis/walletobjects");
const jwt = require("jsonwebtoken");
/**
* @typedef {object} LoyaltyClass
* @typedef {object} LoyaltyClass
* @property {string} programName
* @property {string} issuerName
* @property {string} logoUri
*/
/**
* @typedef {object} LoyaltyObject
* @property {string} QrCodeLink
* @property {string} accountId
* @property {string} FullName
* @property {int} [points]
*/
class LoyaltyPass {
constructor() {
/**
* Path to service account key file from Google Cloud Console. Environment
* variable: GOOGLE_APPLICATION_CREDENTIALS.
*/
// COMENTARIO: SE PUEDE USAR EL ARCHIVO O PASAR CREDENCIALES EN EL AUTH, YO USO CREDENCIAL PARA USAR VARIABLES DE AMBIENTE Y EVITAR SUBIR ARCHIVOS, PARA TENER MAS SEGURIDAD
this.keyFile = process.env.GOOGLE_APPLICATION_CREDENTIALS || '/path/to/key.json';
this.issuerId = process.env.WALLET_ISSUER_ID;
this.credentials = {
type: process.env.TYPE,
client_email: process.env.CLIENT_EMAIL,
private_key: process.env.PRIVATE_KEY.replace(/\\n/g, '\n'),
private_key_id: process.env.PRIVATE_KEY_ID,
project_id: process.env.PROJECT_ID,
client_id: process.env.CLIENT_ID,
universe_domain: process.env.UNIVERSE_DOMAIN,
}
this.auth();
}
/**
* Create authenticated HTTP client using a service account file.
*/
auth() {
const auth = new GoogleAuth({
// COMENTARIO: ACA PUEDES USAR credentials: this.credentials, PARA PASAR LAS CREDENCIALES DIRECTAMENTE EN VEZ DE USAR EL ARCHIVO keyFile
credentials: this.credentials,
scopes: ["https://www.googleapis.com/auth/wallet_object.issuer"],
});
this.client = walletobjects({
version: "v1",
auth: auth,
});
}
/**
* Create a class.
*
* @param {string} classSuffix Developer-defined unique ID for this pass class.
* @param {LoyaltyClass} loyaltyClass The properties for the loyalty class to create.
*
* @returns {string} The pass class ID: `${issuerId}.${classSuffix}`
*/
async createClass(classSuffix, loyaltyClass) {
let response;
// Check if the class exists
try {
await this.client.loyaltyclass.get({
resourceId: `${this.issuerId}.${classSuffix}`,
});
console.log(`Class ${this.issuerId}.${classSuffix} already exists!`);
return `${this.issuerId}.${classSuffix}`;
} catch (err) {
if (err.response && err.response.status !== 404) {
// Something else went wrong...
console.log(err);
return `${this.issuerId}.${classSuffix}`;
}
}
// See link below for more information on required properties
// https://developers.google.com/wallet/retail/loyalty-cards/rest/v1/loyaltyclass
let newClass = {
id: `${this.issuerId}.${classSuffix}`,
issuerName: loyaltyClass.issuerName,
reviewStatus: "UNDER_REVIEW",
programName: loyaltyClass.programName,
programLogo: {
sourceUri: {
uri: loyaltyClass.logoUri,
},
},
};
response = await this.client.loyaltyclass.insert({
requestBody: newClass,
});
console.log("Class insert response status: ", response.status);
return `${this.issuerId}.${classSuffix}`;
}
/**
* Patch a class.
*
* The PATCH method supports patch semantics.
*
* @param {string} classSuffix Developer-defined unique ID for this pass class.
* @param {LoyaltyClass} loyaltyClass The properties for the loyalty class to patch.
*
* @returns {string} The pass class ID: `${issuerId}.${classSuffix}`
*/
async patchClass(classSuffix, loyaltyClass) {
let response;
// Check if the class exists
try {
response = await this.client.loyaltyclass.get({
resourceId: `${this.issuerId}.${classSuffix}`,
});
} catch (err) {
if (err.response && err.response.status === 404) {
console.log(`Class ${this.issuerId}.${classSuffix} not found!`);
return `${this.issuerId}.${classSuffix}`;
} else {
// Something else went wrong...
console.log(err);
return `${this.issuerId}.${classSuffix}`;
}
}
// Patch the class
let patchBody = {
id: `${this.issuerId}.${classSuffix}`,
issuerName: loyaltyClass.issuerName,
programName: loyaltyClass.programName,
programLogo: {
sourceUri: {
uri: loyaltyClass.logoUri,
},
},
// Note: reviewStatus must be 'UNDER_REVIEW' or 'DRAFT' for updates
reviewStatus: "UNDER_REVIEW",
};
response = await this.client.loyaltyclass.patch({
resourceId: `${this.issuerId}.${classSuffix}`,
requestBody: patchBody,
});
console.log("Class patch response status: ", response.status);
return `${this.issuerId}.${classSuffix}`;
}
/**
* Create an object.
*
* @param {string} classSuffix Developer-defined unique ID for the pass class.
* @param {string} objectSuffix Developer-defined unique ID for the pass object.
* @param {LoyaltyObject} loyaltyObject The properties for the loyalty object to create.
*
* @returns {string} The pass object ID: `${issuerId}.${objectSuffix}`
*/
async createObject(classSuffix, objectSuffix, loyaltyObject) {
let response;
// Check if the object exists
try {
await this.client.loyaltyobject.get({
resourceId: `${this.issuerId}.${objectSuffix}`
});
console.log(`Object ${this.issuerId}.${objectSuffix} already exists!`);
return `${this.issuerId}.${objectSuffix}`;
} catch (err) {
if (err.response && err.response.status !== 404) {
// Something else went wrong...
console.log(err);
return `${this.issuerId}.${objectSuffix}`;
}
}
// See link below for more information on required properties
// https://developers.google.com/wallet/retail/loyalty-cards/rest/v1/loyaltyobject
let newObject = {
'id': `${this.issuerId}.${objectSuffix}`,
'classId': `${this.issuerId}.${classSuffix}`,
'state': 'ACTIVE',
'barcode': {
'type': 'QR_CODE',
'value': loyaltyObject.QrCodeLink
},
'accountId': loyaltyObject.accountId,
'accountName': loyaltyObject.FullName,
// Left Column
'loyaltyPoints': {
'label': 'Code',
'balance': {
'string': loyaltyObject.accountId
}
},
// Right Column (Optional)
'secondaryLoyaltyPoints': {
'label': 'Puntos',
'balance': {
'int': 0
}
},
};
response = await this.client.loyaltyobject.insert({
requestBody: newObject
});
console.log('Object insert response status: ', response.status);
return `${this.issuerId}.${objectSuffix}`;
}
/**
* Patch an object.
*
* @param {string} objectSuffix Developer-defined unique ID for the pass object.
* @param {LoyaltyObject} loyaltyObject The properties for the loyalty object to patch.
*
* @returns {string} The pass object ID: `${issuerId}.${objectSuffix}`
*/
async patchObject(objectSuffix, loyaltyObject) {
let response;
// Check if the object exists
try {
response = await this.client.loyaltyobject.get({
resourceId: `${this.issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
console.log(`Object ${this.issuerId}.${objectSuffix} not found!`);
return `${this.issuerId}.${objectSuffix}`;
} else {
// Something else went wrong...
console.log(err);
return `${this.issuerId}.${objectSuffix}`;
}
}
let patchBody = {
'barcode': {
'type': 'QR_CODE',
'value': loyaltyObject.QrCodeLink
},
'accountId': loyaltyObject.accountId,
'accountName': loyaltyObject.FullName,
// Left Column
'loyaltyPoints': {
'label': 'Code',
'balance': {
'string': loyaltyObject.accountId
}
},
// Right Column (Optional)
'secondaryLoyaltyPoints': {
'label': 'Points',
'balance': {
'int': loyaltyObject.points
}
},
}
response = await this.client.loyaltyobject.patch({
resourceId: `${this.issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object patch response status: ', response.status);
return `${this.issuerId}.${objectSuffix}`;
}
/**
* 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} objectSuffix Developer-defined unique ID for the pass object.
*
* @returns {string} The pass object ID: `${issuerId}.${objectSuffix}`
*/
async expireObject(objectSuffix) {
let response;
// Check if the object exists
try {
response = await this.client.loyaltyobject.get({
resourceId: `${this.issuerId}.${objectSuffix}`
});
} catch (err) {
if (err.response && err.response.status === 404) {
console.log(`Object ${this.issuerId}.${objectSuffix} not found!`);
return `${this.issuerId}.${objectSuffix}`;
} else {
// Something else went wrong...
console.log(err);
return `${this.issuerId}.${objectSuffix}`;
}
}
// Patch the object, setting the pass as expired
let patchBody = {
'state': 'EXPIRED'
};
response = await this.client.loyaltyobject.patch({
resourceId: `${this.issuerId}.${objectSuffix}`,
requestBody: patchBody
});
console.log('Object expiration response status: ', response.status);
return `${this.issuerId}.${objectSuffix}`;
}
/**
* Generate a signed JWT that references an existing pass object.
*
* When the user opens the "Add to Google Wallet" URL and saves the pass to
* their wallet, the pass objects defined in the JWT are added to the
* user's Google Wallet app. This allows the user to save multiple pass
* objects in one API call.
*
* The objects to add must follow the below format:
*
* {
* 'id': 'ISSUER_ID.OBJECT_SUFFIX',
* 'classId': 'ISSUER_ID.CLASS_SUFFIX'
* }
*
* @param {string} classSuffix Developer-defined unique IDs for the pass classes.
* @param {string} objectSuffix Developer-defined unique IDs for the pass objects.
*
* @returns {string} An "Add to Google Wallet" link.
*/
createJwtExistingObject(classSuffix, objectSuffix) {
// Create the JWT claims
let claims = {
iss: this.credentials.client_email,
aud: 'google',
typ: 'savetowallet',
payload: {
loyaltyObjects: [{
'id': `${this.issuerId}.${objectSuffix}`,
'classId': `${this.issuerId}.${classSuffix}`
}]
}
};
// The service account credentials are used to sign the JWT
let token = jwt.sign(claims, this.credentials.private_key, { algorithm: 'RS256' });
console.log('Add to Google Wallet link');
console.log(`https://pay.google.com/gp/v/save/${token}`);
return `https://pay.google.com/gp/v/save/${token}`;
}
}
module.exports = { LoyaltyPass };