<?php use Ramsey\Uuid\Uuid; class ItunesSignatureGenerator { private $appBundleID = 'www.u17.com'; private $keyIdentifier = 'ZZZZZZZ'; private $itunesPrivateKeyPath = '/path/to/the/file.p8; /** * @see https://developer.apple.com/documentation/storekit/in-app_purchase/generating_a_signature_for_subscription_offers * * @param $productIdentifier * @param $offerIdentifier * * @return Signature */ public function generateSubscriptionOfferSignature($productIdentifier, $offerIdentifier) { $nonce = strtolower(Uuid::uuid1()->toString()); $timestamp = time() * 1000; $applicationUsername = 'username'; $message = implode( "\u{2063}", [ $this->appBundleID, $this->keyIdentifier, $productIdentifier, $offerIdentifier, $applicationUsername, $nonce, $timestamp ] ); $message = $this->sign($message); return new Signature( base64_encode($message), $nonce, $timestamp, $this->keyIdentifier ); } private function sign($data) { $signature = ''; openssl_sign( $data, $signature, openssl_get_privatekey('file://' . $this->itunesPrivateKeyPath), OPENSSL_ALGO_SHA256 ); return $signature; } }
一些注意事項 openssl是能夠直接進行ECDSA簽名的php
一、$nonce 必須爲小寫,而且每次購買時的nonce不能重複不然會報簽名錯誤沒法購買 code -12json
二、$time 是毫秒time*1000app
三、\u2063 的字符格式須要注意 php裏面能夠用戶"\u{2063}" 來表示,可是有的一些環境不支持這樣的寫法 因此還能夠使用另一種 json_decode('"\u2036"') 來轉一下格式ui