hoels/app-store-server-library-php

App Store Server API和App Store Server通知的PHP服务器库。

1.4.1 2024-09-12 21:31 UTC

This package is auto-updated.

Last update: 2024-09-26 17:36:34 UTC


README

App Store Server APIApp Store Server通知提供的非官方PHP服务器库。还提供SwiftPythonNode.jsJava版本。

目录

  1. 作者
  2. 安装
  3. 文档
  4. 用法
  5. 支持

作者

我没有任何方式与苹果公司有关联。我是一名应用程序开发人员和IT安全专家,因为我认为苹果公司没有提供专门的PHP库,所以我开发了此库。不提供原生和安全库会导致不安全的第三方实现。因此,此库旨在尽可能紧密地反映原生苹果库。大多数功能是苹果Python库的精确复制,并进行了一些针对PHP的特定修改和苹果Swift库的影响。我打算保持此库及其主要版本与苹果库同步。

安装

要求

  • PHP 8.1+
  • OpenSSL和JSON PHP扩展
  • Composer

Composer

composer require hoels/app-store-server-library-php

文档

Python库文档

WWDC视频

从App Store Connect获取应用内购买密钥

要使用App Store Server API或创建促销优惠签名,需要从App Store Connect下载的签名密钥。要获取此密钥,您必须具有管理员角色。转到“用户和访问”>“集成”>“应用内购买”。在这里,您可以创建和管理密钥,以及找到您的发行者ID。使用密钥时,您还需要密钥ID和发行者ID。

获取苹果根证书

苹果PKI网站的“苹果根证书”部分下载并存储根证书。将这些证书作为数组提供给SignedDataVerifier,以便验证来自苹果的签名数据。

用法

API使用

use AppStoreServerLibrary\AppStoreServerAPIClient;
use AppStoreServerLibrary\AppStoreServerAPIClient\APIException;
use AppStoreServerLibrary\Models\Environment;

$privateKey = file_get_contents("/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8"); // Implementation will vary

$keyId = "ABCDEFGHIJ";
$issuerId = "99b16628-15e4-4668-972b-eeff55eeff55";
$bundleId = "com.example";
$environment = Environment::SANDBOX;

$client = new AppStoreServerAPIClient(
    signingKey: $privateKey,
    keyId: $keyId,
    issuerId: $issuerId,
    bundleId: $bundleId,
    environment: $environment
);

try {
    $response = $client->requestTestNotification();
    print_r($response);
} catch (APIException $e) {
    print_r($e);
}

验证使用

use AppStoreServerLibrary\Models\Environment;
use AppStoreServerLibrary\SignedDataVerifier;
use AppStoreServerLibrary\SignedDataVerifier\VerificationException;

$rootCertificates = load_root_certificates(); // Implementation will vary
$enableOnlineChecks = true;
$bundleId = "com.example";
$environment = Environment::SANDBOX;
$appAppleId = null; // appAppleId must be provided for the Production environment
$signedDataVerifier = new SignedDataVerifier(
    rootCertificates: $rootCertificates,
    enableOnlineChecks: $enableOnlineChecks,
    environment: $environment,
    bundleId: $bundleId,
    appAppleId: $appAppleId
);

try {
    $signedNotification = "ey..";
    $payload = $signedDataVerifier->verifyAndDecodeNotification($signedNotification);
    print_r($payload);
} catch (VerificationException $e) {
    print_r($e);
}

收据使用

use AppStoreServerLibrary\AppStoreServerAPIClient;
use AppStoreServerLibrary\AppStoreServerAPIClient\APIException;
use AppStoreServerLibrary\Models\Environment;
use AppStoreServerLibrary\Models\TransactionHistoryRequest;
use AppStoreServerLibrary\Models\TransactionHistoryRequest\Order;
use AppStoreServerLibrary\Models\TransactionHistoryRequest\ProductType;
use AppStoreServerLibrary\ReceiptUtility;

$privateKey = file_get_contents("/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8"); // Implementation will vary

$keyId = "ABCDEFGHIJ";
$issuerId = "99b16628-15e4-4668-972b-eeff55eeff55";
$bundleId = "com.example";
$environment = Environment::SANDBOX;

$client = new AppStoreServerAPIClient(
    signingKey: $privateKey,
    keyId: $keyId,
    issuerId: $issuerId,
    bundleId: $bundleId,
    environment: $environment
);
$receiptUtility = new ReceiptUtility();
$appReceipt = "MI..";

try {
    $transactionId = $receiptUtility->extractTransactionIdFromAppReceipt($appReceipt);
    if ($transactionId !== null) {
        $transactions = [];
        $response = null;
        $request = new TransactionHistoryRequest(
            sort: Order::ASCENDING,
            revoked: false,
            productTypes: [ProductType::AUTO_RENEWABLE]
        );
        while ($response === null || $response->getHasMore() === true) {
            $revision = $response?->getRevision();
            $response = $client->getTransactionHistory(
                transactionId: $transactionId,
                revision: $revision,
                transactionHistoryRequest: $request
            );
            foreach ($response->getSignedTransactions() as $transaction) {
                $transactions[] = $transaction;
            }
        }
        print_r($transactions);
    }
} catch (APIException $e) {
    print_r($e);
}

创建促销优惠签名

use AppStoreServerLibrary\PromotionalOfferSignatureCreator;

$privateKey = file_get_contents("/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8"); // Implementation will vary

$keyId = "ABCDEFGHIJ";
$bundleId = "com.example";

$promotionalOfferSignatureCreator = new PromotionalOfferSignatureCreator(
    signingKey: $privateKey,
    keyId: $keyId,
    bundleId: $bundleId
);

$productId = "<product_id>";
$subscriptionOfferId = "<subscription_offer_id>";
$applicationUsername = "<application_username>";
$nonce = "<nonce>";
$timestamp = time() * 1000;
$base64EncodedSignature = $promotionalOfferSignatureCreator->createSignature(
    productIdentifier: $productId,
    subscriptionOfferId: $subscriptionOfferId,
    applicationUsername: $applicationUsername,
    nonce: $nonce,
    timestamp: $timestamp
);

支持

只有库的最新主要版本将收到更新,包括安全更新。因此,建议升级到新主要版本。