PHP code example of hoels / app-store-server-library-php
1. Go to this page and download the library: Download hoels/app-store-server-library-php library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hoels / app-store-server-library-php example snippets
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
);