Apple App Store 服务器 Node.js 库
这是适用于 App Store 服务器 API 和 App Store 服务器通知 的 Node.js 服务器库。同时也提供 Swift、Python 和 Java 版本。
目录
安装
要求
- Node 16+
NPM/Yarn
# 使用 NPM
npm install @apple/app-store-server-library --save
# 使用 Yarn
yarn add @apple/app-store-server-library
文档
从 App Store Connect 获取应用内购买密钥
要使用 App Store 服务器 API 或创建促销优惠签名,需要从 App Store Connect 下载签名密钥。要获取此密钥,您必须具有管理员角色。转到"用户和访问" > "集成" > "应用内购买"。在这里,您可以创建和管理密钥,并找到您的发行者 ID。使用密钥时,您还需要密钥 ID 和发行者 ID。
获取 Apple 根证书
从 Apple PKI 网站的 Apple 根证书部分下载并存储根证书。将这些证书作为数组提供给 SignedDataVerifier,以验证签名数据是否来自 Apple。
使用方法
API 使用
import { AppStoreServerAPIClient, Environment, SendTestNotificationResponse } from "@apple/app-store-server-library"
const issuerId = "99b16628-15e4-4668-972b-eeff55eeff55"
const keyId = "ABCDEFGHIJ"
const bundleId = "com.example"
const filePath = "/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8"
const encodedKey = readFile(filePath) // 具体实现可能有所不同
const environment = Environment.SANDBOX
const client = new AppStoreServerAPIClient(encodedKey, keyId, issuerId, bundleId, environment)
try {
const response: SendTestNotificationResponse = await client.requestTestNotification()
console.log(response)
} catch (e) {
console.error(e)
}
验证使用
import { SignedDataVerifier } from "@apple/app-store-server-library"
const bundleId = "com.example"
const appleRootCAs: Buffer[] = loadRootCAs() // 具体实现可能有所不同
const enableOnlineChecks = true
const environment = Environment.SANDBOX
const appAppleId = undefined // 当环境为生产环境时,appAppleId 是必需的
const verifier = new SignedDataVerifier(appleRootCAs, enableOnlineChecks, environment, bundleId, appAppleId)
const notificationPayload = "ey..."
const verifiedNotification = await verifier.verifyAndDecodeNotification(notificationPayload)
console.log(verifiedNotification)
收据使用
import { AppStoreServerAPIClient, Environment, GetTransactionHistoryVersion, ReceiptUtility, Order, ProductType, HistoryResponse, TransactionHistoryRequest } from "@apple/app-store-server-library"
const issuerId = "99b16628-15e4-4668-972b-eeff55eeff55"
const keyId = "ABCDEFGHIJ"
const bundleId = "com.example"
const filePath = "/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8"
const encodedKey = readFile(filePath) // 具体实现可能有所不同
const environment = Environment.SANDBOX
const client =
new AppStoreServerAPIClient(encodedKey, keyId, issuerId, bundleId, environment)
const appReceipt = "MI..."
const receiptUtil = new ReceiptUtility()
const transactionId = receiptUtil.extractTransactionIdFromAppReceipt(appReceipt)
if (transactionId != null) {
const transactionHistoryRequest: TransactionHistoryRequest = {
sort: Order.ASCENDING,
revoked: false,
productTypes: [ProductType.AUTO_RENEWABLE]
}
let response: HistoryResponse | null = null
let transactions: string[] = []
do {
const revisionToken = response !== null && response.revision !== null ? response.revision : null
response = await client.getTransactionHistory(transactionId, revisionToken, transactionHistoryRequest, GetTransactionHistoryVersion.V2)
if (response.signedTransactions) {
transactions = transactions.concat(response.signedTransactions)
}
} while (response.hasMore)
console.log(transactions)
}
创建促销优惠签名
import { PromotionalOfferSignatureCreator } from "@apple/app-store-server-library"
const keyId = "ABCDEFGHIJ"
const bundleId = "com.example"
const filePath = "/path/to/key/SubscriptionKey_ABCDEFGHIJ.p8"
const encodedKey = readFile(filePath) // 具体实现可能有所不同
const productId = "<product_id>"
const subscriptionOfferId = "<subscription_offer_id>"
const applicationUsername = "<application_username>"
const nonce = "<nonce>"
const timestamp = Date.now()
const signatureCreator = new PromotionalOfferSignatureCreator(encodedKey, keyId, bundleId)
const signature = signatureCreator.createSignature(productId, subscriptionOfferId, applicationUsername, nonce, timestamp)
console.log(signature)
支持
只有最新的主要版本库会收到更新,包括安全更新。因此,建议更新到新的主要版本。