Added working examples for barcode, localization, fields and expiration with working pass models and webserver

This commit is contained in:
alexandercerutti
2018-08-29 18:38:22 +02:00
parent b9ede238c2
commit 458ef1c481
25 changed files with 498 additions and 0 deletions

91
examples/barcode.js Normal file
View File

@@ -0,0 +1,91 @@
/**
* .barcode(), autocomplete and backward() methods example
* Here we set the barcode. To see all the results, you can
* both unzip .pkpass file or check the properties before
* generating the whole bundle
*
* Pass ?alt=true as querystring to test a barcode generate
* by a string
*/
const app = require("./webserver");
const { Pass } = require("../index");
app.all(function manageRequest(request, response) {
let passName = request.params.modelName + "_" + (new Date()).toISOString().split('T')[0].replace(/-/ig, "");
let pass = new Pass({
model: `./models/${request.params.modelName}`,
certificates: {
wwdr: "../certificates/WWDR.pem",
signerCert: "../certificates/passcertificate.pem",
signerKey: {
keyFile: "../certificates/passkey.pem",
passphrase: "123456"
}
},
overrides: request.body || request.params || request.query,
});
let bc;
if (request.query.alt === true) {
// After this, pass.props["barcodes"] will have support for all the formats
// while pass.props["barcode"] will be the first of barcodes.
bc = pass.barcode("Thank you for using this package <3");
} else {
// After this, pass.props["barcodes"] will have support for just two of three
// of the passed format (the valid ones) and pass.props["barcode"] the first of barcodes.
// if not specified, altText is automatically the message
bc = pass.barcode([{
message: "Thank you for using this package <3",
format: "PKBarcodeFormatCode128"
}, {
message: "Thank you for using this package <3",
format: "PKBarcodeFormatPDF417"
}, {
message: "Thank you for using this package <3",
format: "PKBarcodeFormatMock44617"
}]);
}
// You can change the format chosen for barcode prop support by calling .backward()
// or cancel the support by calling empty .backward
// like bc.backward().
// If the property passed does not exists, things does not change.
bc.backward("PKBarcodeFormatPDF417");
// If your barcode structures got not autogenerated yet (as happens with string
// parameter of barcode) you can call .autocomplete() to generate the support
// to all the structures. Please beware that this will overwrite ONLY barcodes and not barcode.
if (!request.query.alt) {
// String generated barcode returns autocomplete as empty function
bc.autocomplete();
}
console.log("Barcode property is now:", pass.props["barcode"]);
console.log("Barcodes support is autocompleted:", pass.props["barcodes"]);
pass.generate().then(function(stream) {
response.set({
"Content-type": "application/vnd.apple.pkpass",
"Content-disposition": `attachment; filename=${passName}.pkpass`
});
stream.pipe(response);
}).catch(err => {
console.log(err);
response.set({
"Content-type": "text/html",
});
response.send(err.message);
});
});

62
examples/expiration.js Normal file
View File

@@ -0,0 +1,62 @@
/**
* .void() and .expiration() methods example
* To check if a ticket is void, look at the barcode;
* If it is grayed, the ticket is voided. May not be showed on macOS.
*
* To check if a ticket has an expiration date, you'll
* have to wait two minutes.
*/
const app = require("./webserver");
const { Pass } = require("../index");
app.all(function manageRequest(request, response) {
if (!request.query.fn) {
response.send("<a href='?fn=void'>Generate a voided pass.</a><br><a href='?fn=expiration'>Generate a pass with expiration date</a>");
return;
}
let passName = request.params.modelName + "_" + (new Date()).toISOString().split('T')[0].replace(/-/ig, "");
let pass = new Pass({
model: `./models/${request.params.modelName}`,
certificates: {
wwdr: "../certificates/WWDR.pem",
signerCert: "../certificates/passcertificate.pem",
signerKey: {
keyFile: "../certificates/passkey.pem",
passphrase: "123456"
}
},
overrides: request.body || request.params || request.query,
});
if (request.query.fn === "void") {
pass.void();
} else if (request.query.fn === "expiration") {
// 2 minutes later...
let d = new Date();
d.setMinutes(d.getMinutes()+2);
// setting the expiration
pass.expiration(d.toLocaleString());
}
pass.generate().then(function(stream) {
response.set({
"Content-type": "application/vnd.apple.pkpass",
"Content-disposition": `attachment; filename=${passName}.pkpass`
});
stream.pipe(response);
}).catch(err => {
console.log(err);
response.set({
"Content-type": "text/html",
});
response.send(err.message);
});
});

164
examples/fields.js Normal file
View File

@@ -0,0 +1,164 @@
/**
* Fields pushing dimostration
* To see all the included Fields, just open the pass
* Refer to https://apple.co/2Nvshvn to see how passes
* have their fields disposed.
*
* In this example we are going to imitate an EasyJet boarding pass
*
* @Author: Alexander P. Cerutti
*/
const app = require("./webserver");
const { Pass } = require("../index");
app.all(function manageRequest(request, response) {
let passName = "exampleBooking" + "_" + (new Date()).toISOString().split('T')[0].replace(/-/ig, "");
let pass = new Pass({
model: `./models/exampleBooking`,
certificates: {
wwdr: "../certificates/WWDR.pem",
signerCert: "../certificates/passcertificate.pem",
signerKey: {
keyFile: "../certificates/passkey.pem",
passphrase: "123456"
}
},
overrides: request.body || request.params || request.query,
});
pass.transitType = "PKTransitTypeAir";
pass.headerFields.push({
"key": "header1",
"label": "Data",
"value": "25 mag",
"textAlignment": "PKTextAlignmentCenter"
}, {
"key": "header2",
"label": "Volo",
"value": "EZY997",
"textAlignment": "PKTextAlignmentCenter"
});
pass.primaryFields.push({
key: "IATA-source",
value: "NAP",
label: "Napoli",
textAlignment: "PKTextAlignmentLeft"
}, {
key: "IATA-destination",
value: "VCE",
label: "Venezia Marco Polo",
textAlignment: "PKTextAlignmentRight"
});
pass.secondaryFields.push({
"key": "secondary1",
"label": "Imbarco chiuso",
"value": "18:40",
"textAlignment": "PKTextAlignmentCenter",
}, {
"key": "sec2",
"label": "Partenze",
"value": "19:10",
"textAlignment": "PKTextAlignmentCenter"
}, {
"key": "sec3",
"label": "SB",
"value": "Sì",
"textAlignment": "PKTextAlignmentCenter"
}, {
"key": "sec4",
"label": "Imbarco",
"value": "Anteriore",
"textAlignment": "PKTextAlignmentCenter"
});
pass.auxiliaryFields.push({
"key": "aux1",
"label": "Passeggero",
"value": "MR. WHO KNOWS",
"textAlignment": "PKTextAlignmentLeft"
}, {
"key": "aux2",
"label": "Posto",
"value": "1A*",
"textAlignment": "PKTextAlignmentCenter"
});
pass.backFields.push({
"key": "document number",
"label": "Numero documento:",
"value": "- -",
"textAlignment": "PKTextAlignmentLeft"
}, {
"key": "You're checked in, what next",
"label": "Hai effettuato il check-in, Quali sono le prospettive",
"value": "",
"textAlignment": "PKTextAlignmentLeft"
}, {
"key": "Check In",
"label": "1. check-in✓",
"value": "",
"textAlignment": "PKTextAlignmentLeft"
}, {
"key": "checkIn",
"label": "",
"value": "Le uscite d'imbarco chiudono 30 minuti prima della partenza, quindi sii puntuale. In questo aeroporto puoi utilizzare la corsia Fast Track ai varchi di sicurezza.",
"textAlignment": "PKTextAlignmentLeft"
}, {
"key": "2. Bags",
"label": "2. Bagaglio",
"value": "",
"textAlignment": "PKTextAlignmentLeft"
}, {
"key": "Require special assistance",
"label": "Assistenza speciale",
"value": "Se hai richiesto assistenza speciale, presentati a un membro del personale nell'area di Consegna bagagli almeno 90 minuti prima del volo.",
"textAlignment": "PKTextAlignmentLeft"
}, {
"key": "3. Departures",
"label": "3. Partenze",
"value": "",
"textAlignment": "PKTextAlignmentLeft"
}, {
"key": "photoId",
"label": "Un documento didentità corredato di fotografia",
"value": "è obbligatorio su TUTTI i voli. Per un viaggio internazionale è necessario un passaporto valido o, dove consentita, una carta didentità.",
"textAlignment": "PKTextAlignmentLeft"
}, {
"key": "yourSeat",
"label": "Il tuo posto:",
"value": "verifica il tuo numero di posto nella parte superiore. Durante limbarco utilizza le scale anteriori e posteriori: per le file 1-10 imbarcati dalla parte anteriore; per le file 11-31 imbarcati dalla parte posteriore. Colloca le borse di dimensioni ridotte sotto il sedile davanti a te.",
"textAlignment": "PKTextAlignmentLeft"
}, {
"key": "Pack safely",
"label": "Bagaglio sicuro",
"value": "Fai clic http://easyjet.com/it/articoli-pericolosi per maggiori informazioni sulle merci pericolose oppure visita il sito CAA http://www.caa.co.uk/default.aspx?catid=2200",
"textAlignment": "PKTextAlignmentLeft"
}, {
"key": "Thank you for travelling easyJet",
"label": "Grazie per aver viaggiato con easyJet",
"value": "",
"textAlignment": "PKTextAlignmentLeft"
});
pass.generate().then(function(stream) {
response.set({
"Content-type": "application/vnd.apple.pkpass",
"Content-disposition": `attachment; filename=${passName}.pkpass`
});
stream.pipe(response);
}).catch(err => {
console.log(err);
response.set({
"Content-type": "text/html"
});
response.send(err.message);
});
});

73
examples/localization.js Normal file
View File

@@ -0,0 +1,73 @@
/**
* .localize() methods example
* To see all the included languages, you have to unzip the
* .pkpass file and check for .lproj folders
*/
const app = require("./webserver");
const { Pass } = require("../index");
app.all(function manageRequest(request, response) {
let passName = request.params.modelName + "_" + (new Date()).toISOString().split('T')[0].replace(/-/ig, "");
let pass = new Pass({
model: `./models/${request.params.modelName}`,
certificates: {
wwdr: "../certificates/WWDR.pem",
signerCert: "../certificates/passcertificate.pem",
signerKey: {
keyFile: "../certificates/passkey.pem",
passphrase: "123456"
}
},
overrides: request.body || request.params || request.query,
});
// For each language you include, an .lproj folder in pass bundle
// is created or included. You may not want to add translations but
// only images for a specific language. So you create manually
// an .lproj folder in your pass model then add the language here below.
// If no translations were added, the folder
// is included or created but without pass.strings file
// English, does not has an .lproj folder and no translation
// Text placeholders may not be showed for the english language
// (e.g. "Event" and "Location" as literal) and another language may be used instead
pass.localize("en");
// Italian, already has an .lproj which gets included
pass.localize("it", {
"EVENT": "Evento",
"LOCATION": "Dove"
});
// German, doesn't, so is created
pass.localize("de", {
"EVENT": "Ereignis",
"LOCATION": "Ort"
});
// This language does not exist but is still added as .lproj folder
pass.localize("zu", {});
console.log("Added languages", Object.keys(pass.l10n).join(", "))
pass.generate().then(function(stream) {
response.set({
"Content-type": "application/vnd.apple.pkpass",
"Content-disposition": `attachment; filename=${passName}.pkpass`
});
stream.pipe(response);
}).catch(err => {
console.log(err);
response.set({
"Content-type": "text/html",
});
response.send(err.message);
});
});

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

View File

@@ -0,0 +1,34 @@
{
"formatVersion": 1,
"passTypeIdentifier": "pass.com.example.myapp",
"serialNumber": "nmyuxofgna",
"teamIdentifier": "F53WB8AE67",
"webServiceURL": "https://192.168.1.254:80/",
"authenticationToken": "vxwxd7J8AlNNFPS8k0a0FfUFtq0ewzFdc",
"relevantDate": "2011-12-08T13:00-08:00",
"locations": [
{
"longitude": -122.3748889,
"latitude": 37.6189722
},
{
"longitude": -122.03118,
"latitude": 37.33182
}
],
"barcodes": [{
"message": "123456789",
"format": "PKBarcodeFormatQR",
"messageEncoding": "iso-8859-1"
}],
"barcode": {
"message": "123456789",
"format": "PKBarcodeFormatQR",
"messageEncoding": "iso-8859-1"
},
"organizationName": "Apple Inc.",
"description": "A Booking pass",
"foregroundColor": "rgb(255, 255, 255)",
"backgroundColor": "rgb(253, 123, 35)",
"boardingPass": {}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

@@ -0,0 +1,49 @@
{
"formatVersion": 1,
"passTypeIdentifier": "pass.com.example.myapp",
"serialNumber": "nmyuxofgna",
"teamIdentifier": "F53WB8AE67",
"webServiceURL": "https://192.168.1.254:80/",
"authenticationToken": "vxwxd7J8AlNNFPS8k0a0FfUFtq0ewzFdc",
"relevantDate": "2011-12-08T13:00-08:00",
"locations": [
{
"longitude": -122.3748889,
"latitude": 37.6189722
},
{
"longitude": -122.03118,
"latitude": 37.33182
}
],
"barcodes": [{
"message": "123456789",
"format": "PKBarcodeFormatQR",
"messageEncoding": "iso-8859-1"
}],
"barcode": {
"message": "123456789",
"format": "PKBarcodeFormatQR",
"messageEncoding": "iso-8859-1"
},
"organizationName": "Apple Inc.",
"description": "Apple Event Ticket",
"foregroundColor": "rgb(255, 255, 255)",
"backgroundColor": "rgb(60, 65, 76)",
"eventTicket": {
"primaryFields": [
{
"key": "event",
"label": "EVENT",
"value": "The Beat Goes On"
}
],
"secondaryFields": [
{
"key": "loc",
"label": "LOCATION",
"value": "Moscone West"
}
]
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

25
examples/webserver.js Normal file
View File

@@ -0,0 +1,25 @@
/*
* Generic webserver instance for the examples
* @Author Alexander P. Cerutti
*/
const express = require("express");
const app = express();
app.use(express.json());
app.listen(8080, "0.0.0.0", function(request, response) {
console.log("Webserver started.");
});
app.all("/", function (request, response) {
response.redirect("/gen/");
});
app.route("/gen")
.all((req, res) => {
res.set("Content-Type", "text/html");
res.send("Cannot generate a pass. Specify a modelName in the url to continue. <br/>Usage: /gen/<i>modelName</i>")
});
module.exports = app.route("/gen/:modelName");