メインコンテンツにスキップ

アプリ内購入

準備

まだ行っていない場合は、有料アプリケーション契約に署名し、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.')
})
})