1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Libraries/InApp/StoreKit/Apple.php

73 lines
1.7 KiB
PHP
Raw Normal View History

2022-01-30 00:46:39 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2022-01-30 00:46:39 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Libraries\InApp\StoreKit;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
/**
* Class Apple.
*/
class Apple
{
private string $bundle_id = '';
private string $issuer_id = '';
2022-01-30 00:46:39 +01:00
private string $key_id = '';
private string $private_key = '';
private string $alg = 'ES256';
public function createJwt()
{
$this->bundle_id = config('ninja.ninja_apple_bundle_id');
$this->issuer_id = config('ninja.ninja_apple_issuer_id');
2022-01-30 00:46:39 +01:00
$this->key_id = config('ninja.ninja_apple_api_key');
$this->private_key = config('ninja.ninja_apple_private_key');
$issue_time = time();
$expiration_time = $issue_time + 60 * 60;
$header = [
'alg' => $this->alg,
'kid' => $this->key_id,
'typ' => 'JWT',
2022-01-30 00:46:39 +01:00
];
$payload = [
'iss'=> $this->issuer_id,
'iat'=> $issue_time,
'exp'=> $expiration_time,
'aud'=> 'appstoreconnect-v1',
'nonce'=> $this->guidv4(),
'bid'=> $this->bundle_id,
2022-01-30 00:46:39 +01:00
];
$jwt = JWT::encode($payload, $this->private_key, $this->alg, $header);
$decoded = JWT::decode($jwt, new Key($this->private_key, $this->alg));
return $decoded;
}
private function guidv4()
{
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4));
}
}