mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 17:25:21 +00:00
Fixed a few things in example
This commit is contained in:
@@ -1,28 +1,29 @@
|
|||||||
const functions = require("firebase-functions");
|
const functions = require("firebase-functions");
|
||||||
const admin = require("firebase-admin");
|
const admin = require("firebase-admin");
|
||||||
const { PKPass } = require("passkit-generator");
|
const { PKPass } = require("passkit-generator");
|
||||||
var fs = require("node:fs");
|
const fs = require("node:fs");
|
||||||
var path = require("node:path");
|
const path = require("node:path");
|
||||||
var axios = require("axios");
|
const axios = require("axios");
|
||||||
var os = require("node:os");
|
const os = require("node:os");
|
||||||
|
|
||||||
// Firebase init
|
// Firebase init
|
||||||
admin.initializeApp({
|
admin.initializeApp({
|
||||||
credential: admin.credential.cert(require("CERTIFICATE_PATH")),
|
credential: admin.credential.cert(require("CERTIFICATE_PATH")),
|
||||||
storageBucket: "STORAGE_BUCKET_URL",
|
storageBucket: "STORAGE_BUCKET_URL",
|
||||||
});
|
});
|
||||||
var storageRef = admin.storage().bucket();
|
|
||||||
|
|
||||||
exports.pass = functions.https.onRequest((request, response) => {
|
const storageRef = admin.storage().bucket();
|
||||||
PKPass.from(
|
|
||||||
|
exports.pass = functions.https.onRequest(async (request, response) => {
|
||||||
|
const newPass = await PKPass.from(
|
||||||
{
|
{
|
||||||
// Get relevant pass model from model folder (see passkit-generator/examples/models/)
|
// Get relevant pass model from model folder (see passkit-generator/examples/models/)
|
||||||
model: `./model/${request.body.passType}.pass`,
|
model: `./model/${request.body.passType}.pass`,
|
||||||
certificates: {
|
certificates: {
|
||||||
// Assigning certificates from certs folder (you will need to provide these yourself)
|
// Assigning certificates from certs folder (you will need to provide these yourself)
|
||||||
wwdr: fs.fs.readFileSync("./certs/wwdr.pem"),
|
wwdr: fs.readFileSync("./certs/wwdr.pem"),
|
||||||
signerCert: fs.fs.readFileSync("./certs/signerCert.pem"),
|
signerCert: fs.readFileSync("./certs/signerCert.pem"),
|
||||||
signerKey: fs.fs.readFileSync("./certs/signerKey.pem"),
|
signerKey: fs.readFileSync("./certs/signerKey.pem"),
|
||||||
signerKeyPassphrase: "PASSPHRASE",
|
signerKeyPassphrase: "PASSPHRASE",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -34,171 +35,171 @@ exports.pass = functions.https.onRequest((request, response) => {
|
|||||||
backgroundColor: request.body.backgroundColor,
|
backgroundColor: request.body.backgroundColor,
|
||||||
labelColor: request.body.labelColor,
|
labelColor: request.body.labelColor,
|
||||||
},
|
},
|
||||||
).then(async (newPass) => {
|
);
|
||||||
let currentPassType = request.body.passType;
|
|
||||||
|
|
||||||
if (currentPassType == "boardingPass") {
|
let currentPassType = request.body.passType;
|
||||||
newPass.transitType = `PKTransitType${request.body.transitType}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.body.relevantDate !== "Blank") {
|
if (currentPassType == "boardingPass") {
|
||||||
newPass.setRelevantDate(new Date(request.body.relevantDate));
|
newPass.transitType = `PKTransitType${request.body.transitType}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.body.expiryDate !== "Blank") {
|
if (request.body.relevantDate !== "Blank") {
|
||||||
newPass.setExpirationDate(new Date(request.body.expiryDate));
|
newPass.setRelevantDate(new Date(request.body.relevantDate));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (request.body.expiryDate !== "Blank") {
|
||||||
request.body.relevantLocationLat !== "Blank" &&
|
newPass.setExpirationDate(new Date(request.body.expiryDate));
|
||||||
request.body.relevantLocationLong !== "Blank"
|
}
|
||||||
) {
|
|
||||||
newPass.setLocations({
|
if (
|
||||||
latitude: request.body.relevantLocationLat,
|
request.body.relevantLocationLat !== "Blank" &&
|
||||||
longitude: request.body.relevantLocationLong,
|
request.body.relevantLocationLong !== "Blank"
|
||||||
|
) {
|
||||||
|
newPass.setLocations({
|
||||||
|
latitude: request.body.relevantLocationLat,
|
||||||
|
longitude: request.body.relevantLocationLong,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
request.body.header.forEach((field, index) => {
|
||||||
|
if (field.label !== "" || field.value !== "") {
|
||||||
|
newPass.headerFields.push({
|
||||||
|
key: `header${index}`,
|
||||||
|
label: field.label,
|
||||||
|
value: field.value,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
request.body.header.forEach((field, index) => {
|
request.body.primary.forEach((field, index) => {
|
||||||
if (field.label !== "" || field.value !== "") {
|
if (field.label !== "" || field.value !== "") {
|
||||||
newPass.headerFields.push({
|
newPass.primaryFields.push({
|
||||||
key: `header${index}`,
|
key: `primary${index}`,
|
||||||
label: field.label,
|
label: field.label,
|
||||||
value: field.value,
|
value:
|
||||||
});
|
currentPassType == "boardingPass"
|
||||||
}
|
? field.value.toUpperCase()
|
||||||
});
|
: field.value,
|
||||||
|
|
||||||
request.body.primary.forEach((field, index) => {
|
|
||||||
if (field.label !== "" || field.value !== "") {
|
|
||||||
newPass.primaryFields.push({
|
|
||||||
key: `primary${index}`,
|
|
||||||
label: field.label,
|
|
||||||
value:
|
|
||||||
currentPassType == "boardingPass"
|
|
||||||
? field.value.toUpperCase()
|
|
||||||
: field.value,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
request.body.secondary.forEach((field, index) => {
|
|
||||||
if (field.label !== "" || field.value !== "") {
|
|
||||||
newPass.secondaryFields.push({
|
|
||||||
key: `secondary${index}`,
|
|
||||||
label: field.label,
|
|
||||||
value: field.value,
|
|
||||||
textAlignment:
|
|
||||||
index === request.body.secondary.length - 2 ||
|
|
||||||
index === request.body.secondary.length - 1
|
|
||||||
? "PKTextAlignmentRight"
|
|
||||||
: "PKTextAlignmentLeft",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
request.body.auxiliary.forEach((field, index) => {
|
|
||||||
if (field.label !== "" || field.value !== "") {
|
|
||||||
newPass.auxiliaryFields.push({
|
|
||||||
key: `auxiliary${index}`,
|
|
||||||
label: field.label,
|
|
||||||
value: field.value,
|
|
||||||
textAlignment:
|
|
||||||
index === request.body.secondary.length - 2 ||
|
|
||||||
index === request.body.secondary.length - 1
|
|
||||||
? "PKTextAlignmentRight"
|
|
||||||
: "PKTextAlignmentLeft",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!request.body.codeAlt || request.body.codeAlt.trim() === "") {
|
|
||||||
newPass.setBarcodes({
|
|
||||||
message: request.body.qrText,
|
|
||||||
format: `PKBarcodeFormat${request.body.codeType}`,
|
|
||||||
messageEncoding: "iso-8859-1",
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
newPass.setBarcodes({
|
|
||||||
message: request.body.qrText,
|
|
||||||
format: `PKBarcodeFormat${request.body.codeType}`,
|
|
||||||
messageEncoding: "iso-8859-1",
|
|
||||||
altText: request.body.codeAlt,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Downloading thumbnail and logo files from Firebase Storage and adding to pass
|
request.body.secondary.forEach((field, index) => {
|
||||||
if (currentPassType == "generic" || currentPassType == "eventTicket") {
|
if (field.label !== "" || field.value !== "") {
|
||||||
const thumbnailFile = request.body.thumbnailFile;
|
newPass.secondaryFields.push({
|
||||||
const tempPath1 = path.join(os.tmpdir(), thumbnailFile);
|
key: `secondary${index}`,
|
||||||
try {
|
label: field.label,
|
||||||
await storageRef
|
value: field.value,
|
||||||
.file(`thumbnails/${thumbnailFile}`)
|
textAlignment:
|
||||||
.download({ destination: tempPath1 });
|
index === request.body.secondary.length - 2 ||
|
||||||
} catch (error) {
|
index === request.body.secondary.length - 1
|
||||||
console.error(error);
|
? "PKTextAlignmentRight"
|
||||||
}
|
: "PKTextAlignmentLeft",
|
||||||
let buffer = Buffer.alloc(0);
|
});
|
||||||
try {
|
|
||||||
buffer = fs.readFileSync(tempPath1);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
newPass.addBuffer("thumbnail.png", buffer);
|
|
||||||
newPass.addBuffer("thumbnail@2x.png", buffer);
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const logoFile = request.body.logoFile;
|
request.body.auxiliary.forEach((field, index) => {
|
||||||
const tempPath2 = path.join(os.tmpdir(), logoFile);
|
if (field.label !== "" || field.value !== "") {
|
||||||
|
newPass.auxiliaryFields.push({
|
||||||
|
key: `auxiliary${index}`,
|
||||||
|
label: field.label,
|
||||||
|
value: field.value,
|
||||||
|
textAlignment:
|
||||||
|
index === request.body.secondary.length - 2 ||
|
||||||
|
index === request.body.secondary.length - 1
|
||||||
|
? "PKTextAlignmentRight"
|
||||||
|
: "PKTextAlignmentLeft",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!request.body.codeAlt || request.body.codeAlt.trim() === "") {
|
||||||
|
newPass.setBarcodes({
|
||||||
|
message: request.body.qrText,
|
||||||
|
format: `PKBarcodeFormat${request.body.codeType}`,
|
||||||
|
messageEncoding: "iso-8859-1",
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
newPass.setBarcodes({
|
||||||
|
message: request.body.qrText,
|
||||||
|
format: `PKBarcodeFormat${request.body.codeType}`,
|
||||||
|
messageEncoding: "iso-8859-1",
|
||||||
|
altText: request.body.codeAlt,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Downloading thumbnail and logo files from Firebase Storage and adding to pass
|
||||||
|
if (currentPassType == "generic" || currentPassType == "eventTicket") {
|
||||||
|
const thumbnailFile = request.body.thumbnailFile;
|
||||||
|
const tempPath1 = path.join(os.tmpdir(), thumbnailFile);
|
||||||
try {
|
try {
|
||||||
await storageRef
|
await storageRef
|
||||||
.file(`logos/${logoFile}`)
|
.file(`thumbnails/${thumbnailFile}`)
|
||||||
.download({ destination: tempPath2 });
|
.download({ destination: tempPath1 });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
let buffer = Buffer.alloc(0);
|
let buffer = Buffer.alloc(0);
|
||||||
try {
|
try {
|
||||||
buffer = fs.readFileSync(tempPath2);
|
buffer = fs.readFileSync(tempPath1);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
newPass.addBuffer("logo.png", buffer);
|
newPass.addBuffer("thumbnail.png", buffer);
|
||||||
newPass.addBuffer("logo@2x.png", buffer);
|
newPass.addBuffer("thumbnail@2x.png", buffer);
|
||||||
|
}
|
||||||
|
|
||||||
const bufferData = newPass.getAsBuffer();
|
const logoFile = request.body.logoFile;
|
||||||
try {
|
const tempPath2 = path.join(os.tmpdir(), logoFile);
|
||||||
console.log("Pass was uploaded successfully.");
|
try {
|
||||||
response.set("Content-Type", newPass.mimeType);
|
await storageRef
|
||||||
response.status(200).send(bufferData);
|
.file(`logos/${logoFile}`)
|
||||||
|
.download({ destination: tempPath2 });
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
let buffer = Buffer.alloc(0);
|
||||||
|
try {
|
||||||
|
buffer = fs.readFileSync(tempPath2);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
newPass.addBuffer("logo.png", buffer);
|
||||||
|
newPass.addBuffer("logo@2x.png", buffer);
|
||||||
|
|
||||||
// Delete thumbnail file in Firebase Storage
|
const bufferData = newPass.getAsBuffer();
|
||||||
storageRef
|
try {
|
||||||
.file(`thumbnails/${thumbnailFile}`)
|
console.log("Pass was uploaded successfully.");
|
||||||
.delete()
|
response.set("Content-Type", newPass.mimeType);
|
||||||
.then(() => {
|
response.status(200).send(bufferData);
|
||||||
console.log("Thumbnail file deleted successfully");
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error(error);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Delete logo file in Firebase Storage
|
// Delete thumbnail file in Firebase Storage
|
||||||
storageRef
|
storageRef
|
||||||
.file(`logos/${logoFile}`)
|
.file(`thumbnails/${thumbnailFile}`)
|
||||||
.delete()
|
.delete()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log("Logo file deleted successfully");
|
console.log("Thumbnail file deleted successfully");
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.log("Error Uploading pass " + error);
|
|
||||||
response.send({
|
|
||||||
explanation: error.message,
|
|
||||||
result: "FAILED",
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
// Delete logo file in Firebase Storage
|
||||||
|
storageRef
|
||||||
|
.file(`logos/${logoFile}`)
|
||||||
|
.delete()
|
||||||
|
.then(() => {
|
||||||
|
console.log("Logo file deleted successfully");
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error(error);
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.log("Error Uploading pass " + error);
|
||||||
|
response.send({
|
||||||
|
explanation: error.message,
|
||||||
|
result: "FAILED",
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user