Apple and Google Wallet passes from Laravel
What the docs skip when you generate signed Apple Wallet and Google Wallet passes from a Laravel app, learned building NFC digital business cards.
For a digital business card product I built "Add to Apple Wallet" and "Add to Google Wallet" buttons, so every card owner could keep their card on their phone and share it with a QR code. The two platforms work very differently. Here's the short version of what I wish I'd known.
Apple Wallet: a signed zip
An Apple pass is a .pkpass file: a zip containing pass.json, your images, a manifest.json with the SHA-1 hash of every file, and a signature of that manifest.
$manifest = [];
foreach ($files as $name => $contents) {
$manifest[$name] = sha1($contents);
}
file_put_contents("$dir/manifest.json", json_encode($manifest));
The signature is a detached PKCS#7 signature made with your Pass Type ID certificate, and it must include Apple's WWDR intermediate certificate. The clearest way to see what's needed is the OpenSSL command; PHP's openssl_pkcs7_sign() does the same thing with the PKCS7_BINARY | PKCS7_DETACHED flags.
openssl smime -binary -sign \
-certfile AppleWWDRCAG4.pem \
-signer pass-cert.pem -inkey pass-key.pem \
-in manifest.json -out signature -outform DER
Things that cost me time:
passTypeIdentifierandteamIdentifierinpass.jsonmust match the certificate exactly, or the pass silently fails to open.icon.pngis required, even if you never see it.- Every pass needs a unique
serialNumber. Reusing one makes iOS treat a new pass as an old one. - Serve it with
Content-Type: application/vnd.apple.pkpass, or Safari downloads a zip instead of opening Wallet.
Google Wallet: a signed link
Google doesn't want a file. You describe the pass as JSON, sign it as a JWT with a service account key, and send the user to a "save" URL. For business cards the generic pass type fits well.
use Firebase\JWT\JWT;
$claims = [
'iss' => $serviceAccount['client_email'],
'aud' => 'google',
'typ' => 'savetowallet',
'payload' => ['genericObjects' => [$passObject]],
];
$jwt = JWT::encode($claims, $serviceAccount['private_key'], 'RS256');
$saveUrl = "https://pay.google.com/gp/v/save/{$jwt}";
Create the pass class once (the template shared by every card), then create one object per user. Keep object IDs stable, in the form issuerId.something-unique, so you can update a card later instead of issuing a new one.
Updating passes later
This is where the platforms differ most. Google objects can be updated through the API and the change appears on the phone. Apple passes need a small web service that the device registers with, plus a push notification to tell the device to fetch the new version. If people will change their card details, plan for that from day one.
The takeaway
Budget most of the time for certificates, identifiers and testing on real devices, not for the code. Once signing works, generating passes for thousands of users is just another queued job.