storekit-rs 0.5.3

Safe Rust bindings for Apple's StoreKit framework — in-app purchases and transaction streams on macOS
Documentation
use serde::{Deserialize, Serialize};

use crate::error::StoreKitError;
use crate::subscription_info::BillingPlanType;
use crate::transaction::{Transaction, TransactionHandle, TransactionPayload};
use crate::verification_result::{VerificationResult, VerificationResultPayload};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
    tag = "kind",
    rename_all = "camelCase",
    rename_all_fields = "camelCase"
)]
/// Represents options passed to `StoreKit.Product.purchase(options:)`.
pub enum PurchaseOption {
    /// Represents the `AppAccountToken` `StoreKit` case.
    AppAccountToken {
        /// App account token reported by `StoreKit`.
        app_account_token: String,
    },
    /// Represents the `BillingPlanType` `StoreKit` case.
    BillingPlanType {
        /// Value forwarded to `StoreKit` for `billingPlanType`.
        #[serde(rename = "billingPlanType")]
        billing_plan_type: BillingPlanType,
    },
    /// Represents the `Quantity` `StoreKit` case.
    Quantity {
        /// Value forwarded to `StoreKit` for `quantity`.
        quantity: i64,
    },
    /// Represents the `SimulatesAskToBuyInSandbox` `StoreKit` case.
    SimulatesAskToBuyInSandbox {
        /// Value forwarded to `StoreKit` for `simulate_ask_to_buy_in_sandbox`.
        simulate_ask_to_buy_in_sandbox: bool,
    },
    /// Represents the `CustomString` `StoreKit` case.
    CustomString {
        /// Value forwarded to `StoreKit` for `key`.
        key: String,
        /// Value returned by `StoreKit`.
        value: String,
    },
    /// Represents the `CustomNumber` `StoreKit` case.
    CustomNumber {
        /// Value forwarded to `StoreKit` for `key`.
        key: String,
        /// Value returned by `StoreKit`.
        #[serde(rename = "doubleValue")]
        value: f64,
    },
    /// Represents the `CustomBool` `StoreKit` case.
    CustomBool {
        /// Value forwarded to `StoreKit` for `key`.
        key: String,
        /// Value returned by `StoreKit`.
        #[serde(rename = "boolValue")]
        value: bool,
    },
    /// Represents the `CustomData` `StoreKit` case.
    CustomData {
        /// Value forwarded to `StoreKit` for `key`.
        key: String,
        /// Value forwarded to `StoreKit` for `value_base64`.
        value_base64: String,
    },
    /// Represents the `PromotionalOfferSignature` `StoreKit` case.
    PromotionalOfferSignature {
        /// Value forwarded to `StoreKit` for `offer_id`.
        offer_id: String,
        /// Value forwarded to `StoreKit` for `key_id`.
        key_id: String,
        /// Value forwarded to `StoreKit` for `nonce`.
        nonce: String,
        /// Value forwarded to `StoreKit` for `signature_base64`.
        signature_base64: String,
        /// Value forwarded to `StoreKit` for `timestamp`.
        timestamp: i64,
    },
    /// Represents the `PromotionalOfferCompactJws` `StoreKit` case.
    PromotionalOfferCompactJws {
        /// Value forwarded to `StoreKit` for `offer_id`.
        offer_id: String,
        /// Value forwarded to `StoreKit` for `compact_jws`.
        compact_jws: String,
    },
    /// Represents the `IntroductoryOfferEligibility` `StoreKit` case.
    IntroductoryOfferEligibility {
        /// Value forwarded to `StoreKit` for `compact_jws`.
        compact_jws: String,
    },
    /// Represents the `WinBackOffer` `StoreKit` case.
    WinBackOffer {
        /// Value forwarded to `StoreKit` for `offer_id`.
        offer_id: String,
    },
    /// Represents the `OnStorefrontChange` `StoreKit` case.
    OnStorefrontChange {
        /// Value forwarded to `StoreKit` for `should_continue_purchase`.
        should_continue_purchase: bool,
    },
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
/// Represents the result returned by `StoreKit.Product.purchase(options:)`.
pub enum PurchaseResult {
    /// The `StoreKit` operation succeeded.
    Success(VerificationResult<Transaction>),
    /// The person cancelled the `StoreKit` flow.
    UserCancelled,
    /// The `StoreKit` flow is pending further action.
    Pending,
}

#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Debug, Deserialize)]
pub(crate) struct PurchaseResultPayload {
    kind: String,
    #[serde(rename = "verificationResult")]
    verification_result: Option<VerificationResultPayload<TransactionPayload>>,
}

impl PurchaseResultPayload {
    pub(crate) fn into_purchase_result(
        self,
        transaction_handle: Option<TransactionHandle>,
    ) -> Result<PurchaseResult, StoreKitError> {
        match self.kind.as_str() {
            "success" => {
                let verification_result = self.verification_result.ok_or_else(|| {
                    StoreKitError::Unknown(
                        "StoreKit reported a successful purchase without a verification result"
                            .to_owned(),
                    )
                })?;
                let transaction = verification_result.into_result(|payload| {
                    Transaction::from_raw_parts(transaction_handle, payload)
                })?;
                Ok(PurchaseResult::Success(transaction))
            }
            "userCancelled" => Ok(PurchaseResult::UserCancelled),
            "pending" => Ok(PurchaseResult::Pending),
            other => Err(StoreKitError::Unknown(format!(
                "StoreKit returned an unknown purchase result kind '{other}'"
            ))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{PurchaseResult, PurchaseResultPayload};
    use crate::error::StoreKitError;

    fn payload(json: &str) -> PurchaseResultPayload {
        serde_json::from_str(json).expect("valid purchase result payload")
    }

    #[test]
    fn cancelled_and_pending_results_carry_no_transaction() {
        assert!(matches!(
            payload(r#"{"kind":"userCancelled","verificationResult":null}"#)
                .into_purchase_result(None),
            Ok(PurchaseResult::UserCancelled)
        ));
        assert!(matches!(
            payload(r#"{"kind":"pending","verificationResult":null}"#).into_purchase_result(None),
            Ok(PurchaseResult::Pending)
        ));
    }

    #[test]
    fn success_without_a_verification_result_is_an_error() {
        let result =
            payload(r#"{"kind":"success","verificationResult":null}"#).into_purchase_result(None);
        assert!(matches!(result, Err(StoreKitError::Unknown(_))));
    }

    #[test]
    fn unknown_result_kinds_are_errors() {
        let result =
            payload(r#"{"kind":"deferred","verificationResult":null}"#).into_purchase_result(None);
        match result {
            Err(StoreKitError::Unknown(message)) => assert!(message.contains("deferred")),
            other => panic!("expected an unknown-kind error, got {other:?}"),
        }
    }
}