-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstampPass.js
More file actions
113 lines (99 loc) · 3.38 KB
/
stampPass.js
File metadata and controls
113 lines (99 loc) · 3.38 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
require("dotenv").config();
const { PKPass } = require("passkit-generator");
const axios = require("axios");
const fs = require("fs");
/**
* @typedef {object} StampPassProperties
* @property {string} programName
* @property {string} organizationName
* @property {string} logoUri
* @property {string} qrCodeLink
* @property {string} accountId
* @property {string} fullName
* @property {string} authenticationToken
* @property {string?} stampImageUri
*/
class StampPass {
constructor() {
/**
* The certificates used to sign the pass. Environment
* variable: PASS_PHRASE.
*/
this.certificates = {
wwdr: fs.readFileSync("./.certificates/wwdr.pem"),
signerCert: fs.readFileSync("./.certificates/signerCert.pem"),
signerKey: fs.readFileSync("./.certificates/signerKey.pem"),
signerKeyPassphrase: process.env.PASS_PHRASE,
};
}
/**
* Create a stamp pass.
*
* @param {string} serialNumber - The unique serial number of the pass.
* @param {StampPassProperties} stampPassProperties - The properties for the stamp pass to create.
*
* @returns {Promise<void>}
*/
async createPass(serialNumber, stampPassProperties) {
// First we get the image from the URL
const logoResp = await axios.get(
stampPassProperties.logoUri,
{ responseType: "arraybuffer" });
// Then we convert the image to a buffer
const buffer = Buffer.from(logoResp.data, "uft-8");
const stampImageResp = await axios.get(
stampPassProperties.stampImageUri,
{ responseType: "arraybuffer" });
const stampBuffer = Buffer.from(stampImageResp.data, "uft-8");
// We create the pass from the model
const pass = await PKPass.from(
{
model: "./models/StoreCard.pass",
certificates: {
wwdr: this.certificates.wwdr,
signerCert: this.certificates.signerCert,
signerKey: this.certificates.signerKey,
signerKeyPassphrase: this.certificates.signerKeyPassphrase,
},
},
{
serialNumber,
authenticationToken: stampPassProperties.authenticationToken,
webServiceURL: process.env.WEB_SERVICE_URL,
organizationName: stampPassProperties.organizationName,
description: stampPassProperties.organizationName,
logoText: stampPassProperties.programName,
}
);
// We add the images to the pass
pass.addBuffer("logo.png", buffer);
pass.addBuffer("logo@2x.png", buffer);
pass.addBuffer("icon@.png", buffer);
pass.addBuffer("icon@2x.png", buffer);
pass.addBuffer("strip.png", stampBuffer);
pass.addBuffer("strip@2x.png", stampBuffer);
// We set the barcode and fields of the pass
pass.setBarcodes({
"message": stampPassProperties.qrCodeLink,
"format": "PKBarcodeFormatQR",
"messageEncoding": "iso-8859-1",
"altText": stampPassProperties.accountId,
});
// We set the fields of the pass
pass.secondaryFields.push({
"key": "fullName",
"label": "Name",
"value": stampPassProperties.fullName,
});
pass.secondaryFields.push({
"key": "accountId",
"label": "",
"value": stampPassProperties.accountId,
"textAlignment": "PKTextAlignmentRight"
});
const bufferPass = pass.getAsBuffer();
// We can save the pass to the file system
fs.writeFileSync("YourPassName.pkpass", bufferPass);
}
}
module.exports = { StampPass };