#apple #server #server-api #notifications #offers #signature #app-store

app-store-server-library

The Rust server library for the App Store Server API and App Store Server Notifications

15 releases (2 stable)

2.0.0 Apr 3, 2024
1.0.0 Feb 2, 2024
0.9.1 Jan 29, 2024
0.5.0 Sep 4, 2023

#4 in #offers

Download history 25/week @ 2024-01-19 19/week @ 2024-01-26 18/week @ 2024-02-02 2/week @ 2024-02-09 27/week @ 2024-02-16 40/week @ 2024-02-23 8/week @ 2024-03-01 63/week @ 2024-03-08 56/week @ 2024-03-15 43/week @ 2024-03-22 144/week @ 2024-03-29 46/week @ 2024-04-05 6/week @ 2024-04-12

239 downloads per month

MIT license

265KB
3.5K SLoC

Apple App Store Server Rust Library

The Rust server library for the App Store Server API and App Store Server Notifications

Installation

Specify app-store-server-library in your project's Cargo.toml file, under the [dependencies] section:

[dependencies]
app-store-server-library = { version = "1.0.0", features = ["receipt-utility", "api-client"] }

Check crates.io for the latest version number.

Usage

API Usage

use app_store_server_library::{AppStoreServerApiClient, Environment, AppStoreApiResponse, APIError};

#[tokio::main]
async fn main() {
    let issuer_id = "99b16628-15e4-4668-972b-eeff55eeff55";
    let key_id = "ABCDEFGHIJ";
    let bundle_id = "com.example";
    let encoded_key = std::fs::read_to_string("/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8").unwrap(); // Adjust the path accordingly
    let environment = Environment::Sandbox;
    
    let client = AppStoreServerApiClient::new(encoded_key, key_id, issuer_id, bundle_id, environment);
    match client.request_test_notification().await {
        Ok(res) => {
            println!("{}", response.test_notification_token);
        }
        Err(err) => {
            println!("{}", err.http_status_code);
            println!("{:?}", err.raw_api_error);
            println!("{:?}", err.api_error);
            println!("{}", err.error_message);
        }
    }
}

Note: To extract transaction id from app/tx receipt, api-client feature must be enabled.

Verification Usage

// .unwrap() used for example purposes only
let root_cert = "apple-root-cert-in-base-base64-format"; // https://www.apple.com/certificateauthority/AppleRootCA-G3.cer
let root_cert_der = root_cert.as_der_bytes().unwrap(); // Use `base64` crate to decode base64 string into bytes 

let verifier = SignedDataVerifier::new(
    vec![root_cert_der], // Vector of root certificates
    Environment::Sandbox, // Environment
    "app.superapp.apple".to_string(), // Bundle id
    Some(12345678), // App id
);

let payload = "signed-payload";
let decoded_payload = verifier.verify_and_decode_notification(payload).unwrap();

Receipt Usage

let receipt = "MI..";
let transaction_id = extract_transaction_id_from_app_receipt(receipt);

Note: To extract transaction id from app/tx receipt, receipt-utility feature must be enabled.

Promotional Offer Signature Creation

// .unwrap() used for example purposes only
let private_key = include_str!("../assets/SubscriptionKey_L256SYR32L.p8");
let creator = PromotionalOfferSignatureCreator::new(private_key, "L256SYR32L".to_string(), "com.test.app".to_string()).unwrap();
let signature: String = creator.create_signature("com.test.product", "com.test.offer", uuid::Uuid::new_v4().to_string().as_str(), &uuid::Uuid::new_v4(), i64::try_from(system_timestamp()).unwrap()).unwrap();

Documentation

References

Dependencies

~12–25MB
~490K SLoC