AppleTransactionClient is a Java library designed to interact with Apple's App Store subscription APIs. It allows you to generate JSON Web Tokens (JWT) for authentication and retrieve subscription status responses seamlessly.
- Features
- Prerequisites
- Installation
- Configuration
- Usage
- Examples
- Testing
- Publishing
- Contributing
- License
- JWT Generation: Easily create JWTs required for authenticating with Apple’s App Store APIs.
- Subscription Status Retrieval: Fetch and parse subscription status from Apple’s servers.
- Error Handling: Comprehensive error handling tailored for Apple App Store responses.
- Modular Design: Separation of API and implementation using Gradle’s
java-libraryplugin. - Support for Production and Sandbox Environments: Automatically switch between production and sandbox URLs based on response.
- Java: JDK 17 or higher
- Gradle: Version 8.2.1 or compatible
- Dependencies:
-
Clone the Repository:
git clone https://github.com/condo97/AppleTransactionClient.git cd AppleTransactionClient -
Build the Project:
./gradlew build
-
Publish to Local Maven Repository (Optional):
./gradlew publishToMavenLocal
-
Update
build.gradle:Ensure all dependencies are correctly specified. The provided
build.gradleis pre-configured with necessary dependencies and plugins. -
Set Up JWT Signing:
- Place your private key (
.p8file) in a secure location. - Note the
privateKeyIDassociated with your Apple Developer account. - Configure the paths and IDs in your application as required.
- Place your private key (
To interact with Apple’s App Store APIs, you need to generate a JWT. Use the JWTSigner class for this purpose.
import appletransactionclient.JWTSigner;
// Initialize JWTSigner with the path to your private key and your key ID
JWTSigner signer = new JWTSigner("path/to/AuthKey.p8", "YOUR_KEY_ID");
// Generate JWT with required payload
String jwt = signer.signJWT(Map.of(
"iss", "YOUR_ISSUER_ID",
"iat", System.currentTimeMillis() / 1000L,
"exp", (System.currentTimeMillis() / 1000L) + 80,
"aud", "appstoreconnect-v1",
"bid", "com.your.bundleid"
));Once you have the JWT, you can retrieve the subscription status using SubscriptionAppleHttpClient.
import appletransactionclient.SubscriptionAppleHttpClient;
import appletransactionclient.exception.AppStoreErrorResponseException;
import appletransactionclient.http.response.status.AppStoreStatusResponse;
// Initialize the client with base URLs and path
SubscriptionAppleHttpClient client = new SubscriptionAppleHttpClient(
"https://api.storekit.itunes.apple.com/",
"https://api.storekit-sandbox.itunes.apple.com/",
"/inApps/subscriptions/"
);
// Fetch subscription status
try {
AppStoreStatusResponse response = client.getStatusResponseV1(transactionID, jwt);
// Process the response
} catch (AppStoreErrorResponseException | IOException | InterruptedException | URISyntaxException e) {
e.printStackTrace();
// Handle exceptions
}import appletransactionclient.JWTSigner;
import appletransactionclient.SubscriptionAppleHttpClient;
import appletransactionclient.http.response.status.AppStoreStatusResponse;
import appletransactionclient.exception.AppStoreErrorResponseException;
import java.util.Map;
public class ExampleUsage {
public static void main(String[] args) {
try {
// Initialize JWTSigner
JWTSigner signer = new JWTSigner("path/to/AuthKey.p8", "YOUR_KEY_ID");
// Generate JWT
String jwt = signer.signJWT(Map.of(
"iss", "YOUR_ISSUER_ID",
"iat", System.currentTimeMillis() / 1000L,
"exp", (System.currentTimeMillis() / 1000L) + 80,
"aud", "appstoreconnect-v1",
"bid", "com.your.bundleid"
));
// Initialize HTTP Client
SubscriptionAppleHttpClient client = new SubscriptionAppleHttpClient(
"https://api.storekit.itunes.apple.com/",
"https://api.storekit-sandbox.itunes.apple.com/",
"/inApps/subscriptions/"
);
// Retrieve subscription status
Long transactionID = 1234567890L;
AppStoreStatusResponse response = client.getStatusResponseV1(transactionID, jwt);
// Handle the response
System.out.println("Environment: " + response.getEnvironment());
System.out.println("Bundle ID: " + response.getBundleId());
// Further processing...
} catch (Exception e) {
e.printStackTrace();
// Handle exceptions appropriately
}
}
}The project includes unit tests using JUnit Jupiter. To run the tests:
./gradlew testTest classes are located in the src/test/java/appletransactionclient directory.
This library uses Gradle's maven-publish plugin. To publish the library to a Maven repository:
-
Configure Publishing Details in
build.gradle:Ensure the
publishingsection is correctly set up with your repository details. -
Publish the Library:
./gradlew publish
For publishing to a local Maven repository:
./gradlew publishToMavenLocal
Contributions are welcome! Please follow these steps:
-
Fork the Repository
-
Create a Feature Branch:
git checkout -b feature/YourFeature
-
Commit Your Changes
-
Push to the Branch:
git push origin feature/YourFeature
-
Create a Pull Request
Please ensure your code adheres to the project's coding standards and all tests pass.
This project is licensed under the MIT License.