nullform / app-store-server-api-client
App Store Server API 和 App Store Server 通知的 PHP 客户端
Requires
- php: ^7.2||^8.0
- ext-json: *
- firebase/php-jwt: ^6.0
- guzzlehttp/guzzle: ^7.5
- nullform/http-status: ^1.1
- symfony/uid: ^5.4
Requires (Dev)
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-09-12 17:49:21 UTC
README
非官方的用于 App Store Server API 和 App Store Server 通知 的 PHP 客户端。
安装
要求
- PHP >= 7.2
Composer
composer require nullform/app-store-server-api-client
用法
创建 API Key 实例
// As instance of anonymous class... $apiKey = new class extends AbstractApiKey {}; $apiKey->setPrivateKey(\file_get_contents($privateKeyFile)); $apiKey->setPrivateKeyId('Your private key id'); $apiKey->setIssuerId('Your issuer id'); $apiKey->setName('Key name (optional)'); // ... or as instance of ApiKey $apiKey = new ApiKey( \file_get_contents($privateKeyFile), 'Your private key id', 'Your issuer id', 'Key name (optional)' );
创建 Bundle 实例(s)
// As instance of anonymous class... $bundle = new class extends AbstractBundle {}; $bundle->setBundleId('Your bundle id'); $bundle->setName('Bundle name (optional)'); // ... or as instance of Bundle $bundle2 = new Bundle('Your bundle #2 id');
创建 API 客户端实例
$client = new AppStoreServerApiClient($apiKey, $bundle, Environment::PRODUCTION);
使用客户端进行一个或多个 Bundle 的操作
try { $historyResponse = $client->getTransactionHistory($originalTransactionId); $transactions = $historyResponse->getDecodedTransactions(); } catch (HttpClientException $httpClientException) { echo "HTTP client error: " . $httpClientException->getMessage(); } catch (AppleException $appleException) { echo "Apple error ({$appleException->getCode()}): " . $appleException->getMessage(); } try { $bundle2HistoryResponse = $client->setBundle($bundle2)->getTransactionHistory($originalTransactionId); } catch (\Exception $e) { echo "Error: " . $e->getMessage(); }
您可以通过通用的 callApi() 方法手动调用 App Store Server API
use Nullform\AppStoreServerApiClient\AppStoreServerApiClient; use Nullform\AppStoreServerApiClient\AbstractModel; use Nullform\AppStoreServerApiClient\Environment; $apiKey = new MyApiKey(); // Extends AbstractApiKey $bundle = new MyBundle(); // Extends AbstractBundle $client = new AppStoreServerApiClient($apiKey, $bundle, Environment::SANDBOX); $queryParams = new class extends AbstractModel { public $param = 'value'; }; $requestBody = new class extends AbstractModel { public $bodyParam = 'value'; }; // Get instance of ResponseInterface $response = $client->callApi("POST", "inApps/v1/notifications/test", $queryParams, $requestBody); // Get response body $responseBody = $response->getBody()->getContents();
AppStoreServerApiClient 方法
获取交易历史
AppStoreServerApiClient::getTransactionHistory( string $transactionId, null|string|GetTransactionHistoryParams $paramsOrRevision = null ): HistoryResponse
获取您的应用程序的客户内购交易历史。
https://developer.apple.com/documentation/appstoreserverapi/get_transaction_history
获取所有交易历史
AppStoreServerApiClient::getAllTransactionHistory( string $transactionId ): JWSTransactionDecodedPayload[]
递归获取完整交易历史。
获取交易信息
AppStoreServerApiClient::getTransactionInfo( string $transactionId ): TransactionInfoResponse
获取关于您的应用程序的单个交易的信息。
https://developer.apple.com/documentation/appstoreserverapi/get_transaction_info
获取所有订阅状态
AppStoreServerApiClient::getAllSubscriptionStatuses( string $transactionId ): StatusResponse
获取您的应用程序中客户的所有订阅的状态。
https://developer.apple.com/documentation/appstoreserverapi/get_all_subscription_statuses
发送消耗信息
AppStoreServerApiClient::sendConsumptionInformation( string $transactionId, ConsumptionRequest $request ): void
在您的服务器收到消耗请求通知后,将可消耗内购的消耗信息发送到 App Store。
https://developer.apple.com/documentation/appstoreserverapi/send_consumption_information
查找订单 ID
AppStoreServerApiClient::lookUpOrderId( string $orderId ): OrderLookupResponse
使用订单 ID 从收据中获取客户的内购。
https://developer.apple.com/documentation/appstoreserverapi/look_up_order_id
获取退款历史
AppStoreServerApiClient::getRefundHistory( string $transactionId ): RefundLookupResponse
获取客户在您的应用程序中所有已退款的内购列表。
https://developer.apple.com/documentation/appstoreserverapi/get_refund_history
扩展订阅续订日期
AppStoreServerApiClient::extendSubscriptionRenewalDate( string $originalTransactionId, ExtendRenewalDateRequest $request ): ExtendRenewalDateResponse
使用原始交易标识符扩展客户的活跃订阅的续订日期。
https://developer.apple.com/documentation/appstoreserverapi/extend_a_subscription_renewal_date
扩展所有活跃订阅者的订阅续订日期
AppStoreServerApiClient::extendSubscriptionRenewalDatesForAllActiveSubscribers( MassExtendRenewalDateRequest $request ): MassExtendRenewalDateResponse
使用订阅的产品标识符扩展所有符合资格的活跃订阅者的续订日期。
获取订阅续订日期扩展的状态
AppStoreServerApiClient::getStatusOfSubscriptionRenewalDateExtensions( string $productId, string $requestIdentifier ): MassExtendRenewalDateStatusResponse
检查续订日期扩展请求是否完成,并提供了成功或失败扩展的最终计数。
请求测试通知
AppStoreServerApiClient::requestTestNotification(): SendTestNotificationResponse
要求 App Store Server 通知将测试通知发送到您的服务器。
https://developer.apple.com/documentation/appstoreserverapi/request_a_test_notification
获取测试通知状态
AppStoreServerApiClient::getTestNotificationStatus( string $testNotificationToken ): CheckTestNotificationResponse
检查发送到您的服务器的测试 App Store 服务器通知的状态。
https://developer.apple.com/documentation/appstoreserverapi/get_test_notification_status
获取通知历史
AppStoreServerApiClient::getNotificationHistory( NotificationHistoryRequest $params, ?string $paginationToken = null ): CheckTestNotificationResponse
获取 App Store 服务器尝试发送到您的服务器的通知列表。
https://developer.apple.com/documentation/appstoreserverapi/get_notification_history
设置 App Store Bundle
AppStoreServerApiClient::setBundle( BundleInterface $bundle ): self
设置 App Store Bundle 以授权您的 API 调用。
设置 Token TTL
AppStoreServerApiClient::setTokenTtl( int $ttl ): self
设置 JWT TTL 的新值(以秒为单位)。最大值:3600。
https://developer.apple.com/documentation/appstoreserverapi/generating_tokens_for_api_requests
设置 HTTP 客户端请求超时
AppStoreServerApiClient::setHttpClientRequestTimeout( float $timeout ): self
设置 HTTP 客户端请求超时的新值(以秒为单位)。
调用 API
AppStoreServerApiClient::callApi( string $method, string $path, ?AbstractModel $params = null, ?AbstractModel $body = null ): \Psr\Http\Message\ResponseInterface
使用先前传递的凭据自定义调用 App Store Server API。
接收 App Store Server 通知 V2
要接收App Store Server Notifications,请使用AppStoreServerNotificationsClient
$notificationClient = new AppStoreServerNotificationsClient(); try { $payload = $notificationClient->receive($requestBody); } catch (NotificationBadRequestException $exception) { echo $exception->getMessage(); }
请注意,AppStoreServerNotificationsClient仅适用于版本2的通知。
测试
对于单元测试,您必须使用来自App Store Connect的密钥和沙箱凭据创建credentials.php和private-key.p8(请参阅tests/credentials.example.php)。