app store server library java
Release 3.2.0
App Store 伺服器 API 和 App Store 伺服器通知的 Java 伺服器程式庫。也適用於 Swift、Python 和 Node.js。
implementation ' com.apple.itunes.storekit:app-store-server-library:3.2.0 '
< dependency >
< groupId >com.apple.itunes.storekit groupId >
< artifactId >app-store-server-library artifactId >
< version >3.2.0 version >
dependency >
Java文檔
全球開發者大會視頻
若要使用 App Store Server API 或建立促銷優惠簽名,需要從 App Store Connect 下載簽名金鑰。若要取得此金鑰,您必須具有管理員角色。轉至使用者和存取 > 整合 > 應用程式內購買。您可以在此處建立和管理金鑰,以及尋找您的頒發者 ID。使用金鑰時,您還需要金鑰 ID 和頒發者 ID。
下載並儲存在 Apple PKI 網站的 Apple 根憑證部分中找到的根憑證。將這些憑證作為陣列提供給 SignedDataVerifier,以驗證簽章資料是否來自 Apple。
import com . apple . itunes . storekit . client . APIException ;
import com . apple . itunes . storekit . client . AppStoreServerAPIClient ;
import com . apple . itunes . storekit . model . Environment ;
import com . apple . itunes . storekit . model . SendTestNotificationResponse ;
import java . io . IOException ;
import java . nio . file . Files ;
import java . nio . file . Path ;
public class APIExample {
public static void main ( String [] args ) throws Exception {
String issuerId = "99b16628-15e4-4668-972b-eeff55eeff55" ;
String keyId = "ABCDEFGHIJ" ;
String bundleId = "com.example" ;
Path filePath = Path . of ( "/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8" );
String encodedKey = Files . readString ( filePath );
Environment environment = Environment . SANDBOX ;
AppStoreServerAPIClient client =
new AppStoreServerAPIClient ( encodedKey , keyId , issuerId , bundleId , environment );
try {
SendTestNotificationResponse response = client . requestTestNotification ();
System . out . println ( response );
} catch ( APIException | IOException e ) {
e . printStackTrace ();
}
}
}
import com . apple . itunes . storekit . model . Environment ;
import com . apple . itunes . storekit . model . ResponseBodyV2DecodedPayload ;
import com . apple . itunes . storekit . verification . SignedDataVerifier ;
import com . apple . itunes . storekit . verification . VerificationException ;
import java . io . FileInputStream ;
import java . io . InputStream ;
import java . util . Set ;
public class ExampleVerification {
public static void main ( String [] args ) {
String bundleId = "com.example" ;
Environment environment = Environment . SANDBOX ;
Set < InputStream > rootCAs = Set . of (
new FileInputStream ( "/path/to/rootCA1" ),
new FileInputStream ( "/path/to/rootCA2" )
);
Long appAppleId = null ; // appAppleId must be provided for the Production environment
SignedDataVerifier signedPayloadVerifier = new SignedDataVerifier ( rootCAs , bundleId , appAppleId , environment , true );
String notificationPayload = "ey..." ;
try {
ResponseBodyV2DecodedPayload payload = signedPayloadVerifier . verifyAndDecodeNotification ( notificationPayload );
System . out . println ( payload );
} catch ( VerificationException e ) {
e . printStackTrace ();
}
}
}
import com . apple . itunes . storekit . client . AppStoreServerAPIClient ;
import com . apple . itunes . storekit . client . GetTransactionHistoryVersion ;
import com . apple . itunes . storekit . migration . ReceiptUtility ;
import com . apple . itunes . storekit . model . Environment ;
import com . apple . itunes . storekit . model . HistoryResponse ;
import com . apple . itunes . storekit . model . TransactionHistoryRequest ;
import java . nio . file . Files ;
import java . nio . file . Path ;
import java . util . LinkedList ;
import java . util . List ;
public class ExampleMigration {
public static void main ( String [] args ) throws Exception {
String issuerId = "99b16628-15e4-4668-972b-eeff55eeff55" ;
String keyId = "ABCDEFGHIJ" ;
String bundleId = "com.example" ;
Path filePath = Path . of ( "/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8" );
String encodedKey = Files . readString ( filePath );
Environment environment = Environment . SANDBOX ;
AppStoreServerAPIClient client =
new AppStoreServerAPIClient ( encodedKey , keyId , issuerId , bundleId , environment );
String appReceipt = "MI..." ;
ReceiptUtility receiptUtil = new ReceiptUtility ();
String transactionId = receiptUtil . extractTransactionIdFromAppReceipt ( appReceipt );
if ( transactionId != null ) {
TransactionHistoryRequest request = new TransactionHistoryRequest ()
. sort ( TransactionHistoryRequest . Order . ASCENDING )
. revoked ( false )
. productTypes ( List . of ( TransactionHistoryRequest . ProductType . AUTO_RENEWABLE ));
HistoryResponse response = null ;
List < String > transactions = new LinkedList <>();
do {
String revision = response != null ? response . getRevision () : null ;
response = client . getTransactionHistory ( transactionId , revision , request , GetTransactionHistoryVersion . V2 );
transactions . addAll ( response . getSignedTransactions ());
} while ( response . getHasMore ());
System . out . println ( transactions );
}
}
}
import com . apple . itunes . storekit . offers . PromotionalOfferSignatureCreator ;
import java . nio . file . Files ;
import java . nio . file . Path ;
import java . util . UUID ;
public class ExampleSignatureCreation {
public static void main ( String [] args ) throws Exception {
String keyId = "ABCDEFGHIJ" ;
String bundleId = "com.example" ;
Path filePath = Path . of ( "/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8" );
String encodedKey = Files . readString ( filePath );
PromotionalOfferSignatureCreator signatureCreator = new PromotionalOfferSignatureCreator ( encodedKey , keyId , bundleId );
String productId = "" ;
String subscriptionOfferId = "" ;
String appAccountToken = "" ;
UUID nonce = UUID . randomUUID ();
long timestamp = System . currentTimeMillis ();
String encodedSignature = signatureCreator . createSignature ( productId , subscriptionOfferId , appAccountToken , nonce , timestamp );
System . out . println ( encodedSignature );
}
}
只有該庫的最新主要版本才會收到更新,包括安全性更新。因此,建議更新到新的主要版本。