iap-apple
Version:
Integration with Apples InAppPurchases in Typescript, available for NodeJS environments.
147 lines (142 loc) • 5.38 kB
TypeScript
declare enum RECEIPT_STATUS_ENUM {
SUCCESS = 0,
VALID_NO_PURCHASE = 2,
CANNOT_READ_JSON = 21000,
DATA_MALFORMED = 21002,
RECEIPT_NOT_AUTHENTICATED = 21003,
SHARED_SECRET_DOESNT_MATCH = 21004,
SERVER_NOT_AVAILABLE = 21005,
SUBSCRIPTION_EXPIRED = 21006,
TEST_ENV_RECEIPT_DETECTED = 21007,
PRODUCTION_ENV_RECEIPT_DETECTED = 21008,
INTERNAL_DATA_ACCESS_ERROR = 21009,
USER_ACCOUNT_DELETED = 21010
}
interface IPendingRenewalInfo {
auto_renew_product_id: string;
auto_renew_status: '0' | '1';
original_transaction_id: string;
product_id: string;
expiration_intent?: '1' | '2' | '3' | '4' | '5';
grace_period_expires_date_ms?: string;
is_in_billing_retry_period?: '0' | '1';
offer_code_ref_name?: string;
price_consent_status?: '0' | '1';
promotional_offer_id?: string;
}
interface IReceiptInAppItem {
quantity: string;
product_id: string;
transaction_id: string;
original_transaction_id: string;
purchase_date: string;
purchase_date_ms: string;
original_purchase_date: string;
original_purchase_date_ms: string;
expires_date?: string;
expires_date_ms?: string;
expiration_intent?: '1' | '2' | '3' | '4' | '5';
is_trial_period: string;
cancellation_date?: string;
cancellation_date_ms?: string;
cancellation_reason?: '0' | '1';
app_item_id: string;
web_order_line_item_id?: string;
is_in_intro_offer_period?: string;
promotional_offer_id?: string;
offer_code_ref_name?: string;
in_app_ownership_type?: 'FAMILY_SHARED' | 'PURCHASED';
}
interface ILogger {
log: (message: string) => void;
warn: (message: string) => void;
error: (message: string) => void;
}
interface IIAPAppleConfig {
appleExcludeOldTransactions?: boolean;
appSharedSecret: string;
test?: boolean | undefined;
logger?: ILogger | null;
}
interface PurchasedItem {
bundleId: string;
appItemId: string;
originalTransactionId?: string;
transactionId: string;
productId: string;
originalPurchaseDateMS?: number;
expirationDateMS?: number;
purchaseDateMS: number;
isTrialPeriod: boolean;
cancellationDateMS?: number;
quantity: number;
}
interface IAPAppleError {
rejectionMessage: string;
data?: IVerifyReceiptResponseBody | null;
}
interface IVerifyReceiptResponseBody {
status: RECEIPT_STATUS_ENUM;
environment: 'Sandbox' | 'Production';
receipt: IReceipt;
latest_receipt: string;
latest_receipt_info: IReceiptInAppItem[];
'is-retryable'?: boolean;
pending_renewal_info?: IPendingRenewalInfo[];
}
interface IReceipt {
bundle_id: string;
application_version: string;
in_app: IReceiptInAppItem[];
latest_receipt_info: IReceiptInAppItem[];
original_application_version: string;
receipt_creation_date_ms: string;
expiration_date_ms: string;
original_purchase_date: string;
app_item_id: string;
version_external_identifier: string;
expires_date_ms?: string;
}
/**
* Validates an Apple App Store receipt against Apple's verifyReceipt endpoint.
* Attempts production endpoint first, falls back to sandbox if needed.
*
* @param receipt - Base64-encoded receipt data from the App Store
* @param config - Configuration including shared secret and optional settings
* @returns Validated receipt response from Apple
* @throws {IAPAppleError} When validation fails or receipt is invalid
*/
declare function verify(receipt: string, config: IIAPAppleConfig): Promise<IVerifyReceiptResponseBody>;
/**
* Checks whether the receipt validation was successful.
*
* @param verifyReceiptResponse - Response from Apple's verifyReceipt endpoint
* @returns True if the receipt status indicates success
*/
declare const isVerifiedReceipt: (verifyReceiptResponse: IVerifyReceiptResponseBody | null) => boolean;
/**
* Determines if a purchased item has expired (cancelled or past expiration date).
*
* @param purchasedItem - The purchased item to check
* @returns True if the item has been cancelled or its expiration date has passed
* @throws {Error} If purchasedItem is invalid or missing transactionId
*/
declare const isPurchasedItemExpired: (purchasedItem: PurchasedItem | null) => boolean;
/**
* Checks if a purchased item has been cancelled.
*
* @param purchasedItem - The purchased item to check
* @returns True if the item has a cancellation date
* @throws {Error} If purchasedItem is invalid or missing transactionId
*/
declare const isPurchasedItemCanceled: (purchasedItem: PurchasedItem) => boolean;
/**
* Extracts purchased items from a validated receipt response.
* Combines in_app and latest_receipt_info, deduplicates by original_transaction_id,
* and returns items sorted by purchase date (newest first).
*
* @param verifyReceiptResponse - Response from Apple's verifyReceipt endpoint
* @returns Array of purchased items, deduplicated and sorted by purchase date descending
*/
declare const getPurchasedItems: (verifyReceiptResponse: IVerifyReceiptResponseBody | null) => PurchasedItem[];
export { type IAPAppleError, type IIAPAppleConfig, type ILogger, type IPendingRenewalInfo, type IReceiptInAppItem, type IVerifyReceiptResponseBody, type PurchasedItem, RECEIPT_STATUS_ENUM, getPurchasedItems, isPurchasedItemCanceled, isPurchasedItemExpired, isVerifiedReceipt, verify };