跳至主要內容

應用程式內購買

準備工作

如果您還沒做過,您需要在 iTunes Connect 中簽署「付費應用程式協議」並設定您的銀行和稅務資訊。

iTunes Connect 開發人員幫助:協議、稅務和銀行業務概述

建立您的應用程式內購買

接著,您需要在 iTunes Connect 中設定您的應用程式內購買,並包含詳細資訊,例如名稱、定價和描述,以突顯應用程式內購買的功能和特性。

iTunes Connect 開發人員幫助:建立應用程式內購買

變更 CFBundleIdentifier

若要在 Electron 中測試開發中的應用程式內購買,您必須變更 node_modules/electron/dist/Electron.app/Contents/Info.plist 中的 CFBundleIdentifier。您必須將 com.github.electron 替換為您使用 iTunes Connect 建立的應用程式的套件識別碼。

<key>CFBundleIdentifier</key>
<string>com.example.app</string>

程式碼範例

以下範例顯示如何在 Electron 中使用應用程式內購買。您必須將產品 ID 替換為使用 iTunes Connect 建立的產品的識別碼 (com.example.app.product1 的識別碼是 product1)。請注意,您必須盡快在您的應用程式中監聽 transactions-updated 事件。

// Main process
const { inAppPurchase } = require('electron')
const PRODUCT_IDS = ['id1', 'id2']

// Listen for transactions as soon as possible.
inAppPurchase.on('transactions-updated', (event, transactions) => {
if (!Array.isArray(transactions)) {
return
}

// Check each transaction.
for (const transaction of transactions) {
const payment = transaction.payment

switch (transaction.transactionState) {
case 'purchasing':
console.log(`Purchasing ${payment.productIdentifier}...`)
break

case 'purchased': {
console.log(`${payment.productIdentifier} purchased.`)

// Get the receipt url.
const receiptURL = inAppPurchase.getReceiptURL()

console.log(`Receipt URL: ${receiptURL}`)

// Submit the receipt file to the server and check if it is valid.
// @see https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
// ...
// If the receipt is valid, the product is purchased
// ...

// Finish the transaction.
inAppPurchase.finishTransactionByDate(transaction.transactionDate)

break
}

case 'failed':

console.log(`Failed to purchase ${payment.productIdentifier}.`)

// Finish the transaction.
inAppPurchase.finishTransactionByDate(transaction.transactionDate)

break
case 'restored':

console.log(`The purchase of ${payment.productIdentifier} has been restored.`)

break
case 'deferred':

console.log(`The purchase of ${payment.productIdentifier} has been deferred.`)

break
default:
break
}
}
})

// Check if the user is allowed to make in-app purchase.
if (!inAppPurchase.canMakePayments()) {
console.log('The user is not allowed to make in-app purchase.')
}

// Retrieve and display the product descriptions.
inAppPurchase.getProducts(PRODUCT_IDS).then(products => {
// Check the parameters.
if (!Array.isArray(products) || products.length <= 0) {
console.log('Unable to retrieve the product information.')
return
}

// Display the name and price of each product.
for (const product of products) {
console.log(`The price of ${product.localizedTitle} is ${product.formattedPrice}.`)
}

// Ask the user which product they want to purchase.
const selectedProduct = products[0]
const selectedQuantity = 1

// Purchase the selected product.
inAppPurchase.purchaseProduct(selectedProduct.productIdentifier, selectedQuantity).then(isProductValid => {
if (!isProductValid) {
console.log('The product is not valid.')
return
}

console.log('The payment has been added to the payment queue.')
})
})