alexandercerutti / passkit-generator

The easiest way to generate custom Apple Wallet passes in Node.js
MIT License
897 stars 109 forks source link

Error with instance generator #160

Closed sharkydeveloping closed 1 year ago

sharkydeveloping commented 1 year ago

Running OS

Windows 10 Version 22H2 for x64-based Systems (KB5029331)

Running Node Version

v20.4.0

Description

I'm trying to create a pkpass.pack(). I tried doing it with pkpass.from(), but there was no result, since it seems it only works with an instance of pkpass. When I try to generate this instance, I get an error that headerFields does not exist, so I don't know what's happening. Perhaps I'm creating the pass instance incorrectly, since I want to create my pass from a template, pass.json.

I'm sending part of the code; thank you in advance.

Code

Function to create the instance of the pass


const pass = async (data: PassData) => {
  const pass = new PKPass.(
    {},
    {
      wwdr: "./src/controllers/ios/certs/wwdr.pem",
      signerCert: "./src/controllers/ios/certs/signerCert.pem",
      signerKey: "./src/controllers/ios/certs/signerKey.pem",
      signerKeyPassphrase: "Ticketclub",
    },
    {
      serialNumber: randomUUID(),
      description: "TicketClub",
      organizationName: "TicketClub",
      teamIdentifier: "",
      passTypeIdentifier: "pass.com.ticketclub.pass",
      backgroundColor: "rgb(0,55,102)",
      foregroundColor: "rgb(255,255,255)",
      labelColor: "rgb(255,255,255)",
      formatVersion: 1,
    }
  );

  pass.setBarcodes("https://ticketclub.cl");

  pass.headerFields.push({
    key: "fecha",
    label: "Fecha",
    value: data.event_date.toISOString().split("T")[0],
  });

  pass.auxiliaryFields.push(
    {
      value: data.username,
      label: "Nombre",
      key: "user_name",
    },
    {
      value: data.isCourtesy ? "Si" : "No",
      label: "Cortesía",
      key: "courtesy",
    }
  );

  pass.secondaryFields.push(
    {
      value: data.ticket_type,
      label: "Tipo de Ticket",
      key: "ticket_type",
    },
    {
      value: data.preventa_name,
      label: "Preventa",
      key: "preventa",
    }
  );

  pass.primaryFields.push({
    value: data.event_name,
    label: "Evento",
    key: "event_name",
  });

  pass.backFields.push({
    value: data.event_time,
    label: "Hora",
    key: "event_time",
    });

  return pass;
};

Function to create the passpack

export const createPasses = async (data: PassData[]) => {
  const passes = await Promise.all(
    data.map(async (passData) => {
      return await pass(passData);
    })
  );

  const passesPacked = PKPass.pack(passes);
  const passname = "ticket-" + Date.now() + ".pkpass";

return passesPacked
};
alexandercerutti commented 1 year ago

Hi @sharkydeveloping, thanks for using Passkit-generator.

I can guess the error is due to the fact that you are not specifying any pass type through its setter or through pass.json. Fields getters are meant to crash if no type is available.

Anyway, an issue that for sure you will meet later is that you are passing the certificates paths instead of their contents.

Let me know if this helps!

sharkydeveloping commented 1 year ago

I will try! Can you help me with an example code please?

alexandercerutti commented 1 year ago

@sharkydeveloping take a look here!

https://github.com/alexandercerutti/passkit-generator/blob/master/examples/self-hosted/src/scratch.ts#L54

sharkydeveloping commented 1 year ago

Ok, i will have a look! Thanks mate!

alexandercerutti commented 1 year ago

Keep me updated on your advancements, so in case we can close the issue :)

sharkydeveloping commented 1 year ago

Hi!, i got this error Error: Cannot pack passes. Only PKPass instances allowed

  var passes: PKPass[] = [];

  data.forEach(async (pass_data) => {
    const pkpass = await pass(pass_data);
    passes.push(pkpass);
  });

  const passesPacked = PKPass.pack(passes);

when i do this, there is no problem is just when i use an array of data.


  const passesPacked = PKPass.pack(pass(data[0]), pass(data[1]));
sharkydeveloping commented 1 year ago

Update! I figure it out.

 const passesPacked = PKPass.pack(...passes);
alexandercerutti commented 1 year ago

Glad you solved it! If you could leave a ⭐ on the project, that would be very helpful! Thank you!