From 139d55ea99a97e1e2dd15393e527a173f9928a38 Mon Sep 17 00:00:00 2001 From: Craig Newbury Date: Mon, 5 Feb 2024 21:05:26 +0000 Subject: [PATCH] Add support for Wallet Orders --- src/FinanceOrder.php | 114 +++++++++++++++++++++++++++++++++++++++++++ src/PKPass.php | 11 +++-- 2 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 src/FinanceOrder.php diff --git a/src/FinanceOrder.php b/src/FinanceOrder.php new file mode 100644 index 0000000..33dc427 --- /dev/null +++ b/src/FinanceOrder.php @@ -0,0 +1,114 @@ +json); + + // Creates SHA hashes for string files in each project. + foreach ($this->locales as $language => $strings) { + $sha[$language . '.lproj/' . self::FILE_TYPE . '.strings'] = hash(self::HASH_ALGO, $strings); + } + + foreach ($this->files as $name => $path) { + $sha[$name] = hash(self::HASH_ALGO, file_get_contents($path)); + } + + foreach ($this->remote_file_urls as $name => $url) { + $sha[$name] = hash(self::HASH_ALGO, file_get_contents($url)); + } + + foreach ($this->files_content as $name => $content) { + $sha[$name] = hash(self::HASH_ALGO, $content); + } + + return json_encode((object)$sha); + } + + /** + * Creates .pkpass zip archive. + * + * @param string $manifest + * @param string $signature + * @return string + * @throws PKPassException + */ + protected function createZip($manifest, $signature) + { + // Package file in Zip (as .order) + $zip = new ZipArchive(); + $filename = tempnam($this->tempPath, self::FILE_TYPE); + if (!$zip->open($filename, ZipArchive::OVERWRITE)) { + throw new PKPassException('Could not open ' . basename($filename) . ' with ZipArchive extension.'); + } + $zip->addFromString('signature', $signature); + $zip->addFromString('manifest.json', $manifest); + $zip->addFromString(self::PAYLOAD_FILE, $this->json); + + // Add translation dictionary + foreach ($this->locales as $language => $strings) { + if (!$zip->addEmptyDir($language . '.lproj')) { + throw new PKPassException('Could not create ' . $language . '.lproj folder in zip archive.'); + } + $zip->addFromString($language . '.lproj/' . self::FILE_TYPE . '.strings', $strings); + } + + foreach ($this->files as $name => $path) { + $zip->addFile($path, $name); + } + + foreach ($this->remote_file_urls as $name => $url) { + $download_file = file_get_contents($url); + $zip->addFromString($name, $download_file); + } + + foreach ($this->files_content as $name => $content) { + $zip->addFromString($name, $content); + } + + $zip->close(); + + if (!file_exists($filename) || filesize($filename) < 1) { + @unlink($filename); + throw new PKPassException('Error while creating order.order. Check your ZIP extension.'); + } + + $content = file_get_contents($filename); + unlink($filename); + + return $content; + } +} diff --git a/src/PKPass.php b/src/PKPass.php index b8e1e45..f46ade5 100644 --- a/src/PKPass.php +++ b/src/PKPass.php @@ -23,6 +23,9 @@ */ class PKPass { + const FILE_TYPE = 'pass'; + const FILE_EXT = 'pkpass'; + const MIME_TYPE = 'application/vnd.apple.pkpass'; /** * Holds the path to the certificate. * @var string @@ -55,7 +58,7 @@ class PKPass /** * Holds the JSON payload. - * @var object|array + * @var string */ protected $json; @@ -312,7 +315,7 @@ public function create($output = false) // Output pass header('Content-Description: File Transfer'); - header('Content-Type: application/vnd.apple.pkpass'); + header('Content-Type: ' . self::MIME_TYPE); header('Content-Disposition: attachment; filename="' . $this->getName() . '"'); header('Content-Transfer-Encoding: binary'); header('Connection: Keep-Alive'); @@ -332,9 +335,9 @@ public function create($output = false) */ public function getName() { - $name = $this->name ?: 'pass'; + $name = $this->name ?: self::FILE_TYPE; if (!strstr($name, '.')) { - $name .= '.pkpass'; + $name .= '.' . self::FILE_EXT; } return $name;