The PKPass class is the main class for creating Apple Wallet passes. It handles the creation, signing, and packaging of passes as .pkpass files according to Apple's Wallet specifications.
- Constructor
- Configuration Methods
- Data Management
- File Management
- Localization
- Pass Creation
- Constants
Creates a new PKPass instance.
Parameters:
$certificatePath(string|bool, optional): Path to the P12 certificate file$certificatePassword(string|bool, optional): Password for the certificate
Example:
use PKPass\PKPass;
// Create with certificate
$pass = new PKPass('/path/to/certificate.p12', 'password');
// Create without certificate (set later)
$pass = new PKPass();Sets the path to the certificate file.
Parameters:
$path(string): Path to the P12 certificate file
Returns: bool - Always returns true
Example:
$pass->setCertificatePath('/path/to/certificate.p12');Sets the certificate from a string.
If specified, this overrides any previously set certificate path.
Parameters:
$p12_string(string): The P12 certificate content as a string
Returns: bool - Always returns true
Sets the certificate password.
Parameters:
$password(string): Password for the certificate
Returns: bool - Always returns true
Example:
$pass->setCertificatePassword('mypassword');Sets the path to the Apple WWDR Intermediate certificate.
Parameters:
$path(string): Path to the WWDR certificate
Returns: bool - Always returns true
Example:
$pass->setWwdrCertificatePath('/path/to/AppleWWDRCA.pem');Sets the path to the temporary directory for file operations.
Parameters:
$path(string): Path to temporary directory
Example:
$pass->setTempPath('/tmp/pkpass');Sets the pass data (the main JSON content of the pass).
Parameters:
$data(string|array|object): Pass data as JSON string, array, or object
Throws: PKPassException if data is invalid
Example:
$passData = [
'description' => 'My Store Card',
'formatVersion' => 1,
'organizationName' => 'My Company',
'passTypeIdentifier' => 'pass.com.mycompany.mypass',
'serialNumber' => '123456',
'teamIdentifier' => 'TEAM123456',
'storeCard' => [
'primaryFields' => [
[
'key' => 'balance',
'label' => 'Balance',
'value' => '$25.00'
]
]
]
];
$pass->setData($passData);Sets the filename for the generated pass.
Parameters:
$name(string): Filename (without extension)
Example:
$pass->setName('my-store-card');Gets the filename for the generated pass.
Returns: string - The filename with extension
Example:
$filename = $pass->getName(); // Returns 'my-store-card.pkpass'Adds a file to the pass package.
Parameters:
$path(string): Path to the file$name(string, optional): Name to use in the pass archive (defaults to basename of path)
Throws: PKPassException if file doesn't exist
Example:
$pass->addFile('/path/to/icon.png');
$pass->addFile('/path/to/logo.png', 'logo.png');Adds a file from a URL to the pass package.
Parameters:
$url(string): URL to the file$name(string, optional): Name to use in the pass archive (defaults to basename of URL)
Example:
$pass->addRemoteFile('https://example.com/icon.png');
$pass->addRemoteFile('https://example.com/logo.png', 'custom-logo.png');Adds a file from string content to the pass package.
Parameters:
$content(string): File content$name(string): Filename to use in the pass archive
Example:
$svgContent = '<svg>...</svg>';
$pass->addFileContent($svgContent, 'logo.svg');Adds localized strings for a specific language.
Parameters:
$language(string): Language code (e.g., 'en', 'fr', 'de')$strings(array): Key-value pairs of translation strings
Throws: PKPassException if strings are empty or not an array
Example:
$pass->addLocaleStrings('en', [
'BALANCE' => 'Balance',
'POINTS' => 'Points'
]);
$pass->addLocaleStrings('fr', [
'BALANCE' => 'Solde',
'POINTS' => 'Points'
]);Adds a localized file for a specific language.
Parameters:
$language(string): Language code$path(string): Path to the file$name(string, optional): Name to use in the pass archive
Throws: PKPassException if file doesn't exist
Example:
$pass->addLocaleFile('en', '/path/to/en/logo.png');
$pass->addLocaleFile('fr', '/path/to/fr/logo.png', 'logo.png');Adds a localized file from a URL for a specific language.
Parameters:
$language(string): Language code$url(string): URL to the file$name(string, optional): Name to use in the pass archive
Example:
$pass->addLocaleRemoteFile('en', 'https://example.com/en/logo.png');
$pass->addLocaleRemoteFile('fr', 'https://example.com/fr/logo.png', 'logo.png');Adds a localized file from string content for a specific language.
Parameters:
$language(string): Language code$content(string): File content$name(string): Filename to use in the pass archive
Example:
$frenchContent = 'Localized content in French';
$pass->addLocaleFileContent('fr', $frenchContent, 'terms.txt');Creates the actual .pkpass file.
Parameters:
$output(bool, optional): Whether to output directly to browser (true) or return as string (false)
Returns: string - The pass content as binary string (if $output is false)
Throws: PKPassException if pass creation fails
Example:
// Save to file
$passContent = $pass->create(false);
file_put_contents('mypass.pkpass', $passContent);
// Output directly to browser
$pass->create(true);FILE_TYPE- 'pass' - The type identifier for passesFILE_EXT- 'pkpass' - The file extension for passesMIME_TYPE- 'application/vnd.apple.pkpass' - The MIME type for passes
Example:
echo PKPass::MIME_TYPE; // 'application/vnd.apple.pkpass'Every pass must include an icon.png file. The class will throw a PKPassException if this file is missing.
Recommended files:
icon.png(required) - 29×29 pointsicon@2x.png- 58×58 pointsicon@3x.png- 87×87 pointslogo.png- 160×50 points (max)logo@2x.png- 320×100 points (max)logo@3x.png- 480×150 points (max)
All methods that can fail will throw a PKPassException. Always wrap critical operations in try-catch blocks:
try {
$pass->setData($passData);
$pass->addFile('/path/to/icon.png');
$passContent = $pass->create(false);
} catch (PKPassException $e) {
echo 'Error: ' . $e->getMessage();
}