In-App Purchases应用内购买
Preparing准备
Paid Applications Agreement有偿申请协议
If you haven't already, you’ll need to sign the Paid Applications Agreement and set up your banking and tax information in iTunes Connect.如果您还没有,您需要签署付费应用程序协议,并在iTunes Connect中设置您的银行和税务信息。
iTunes Connect Developer Help: Agreements, tax, and banking overviewiTunes Connect开发者帮助:协议、税务和银行概述
Create Your In-App Purchases创建应用内购买
Then, you'll need to configure your in-app purchases in iTunes Connect, and include details such as name, pricing, and description that highlights the features and functionality of your in-app purchase.然后,您需要在iTunes Connect中配置您的应用内购买,并包括名称、定价和说明等详细信息,以突出显示应用内购买的功能和功能。
iTunes Connect Developer Help: Create an in-app purchaseiTunes Connect开发者帮助:创建应用内购买
Change the CFBundleIdentifier更改CFBundleIdentifier
To test In-App Purchase in development with Electron you'll have to change the 要使用Electron测试开发中的应用内购买,您必须更改CFBundleIdentifier
in node_modules/electron/dist/Electron.app/Contents/Info.plist
. node_modules/electron/dist/Electron.app/Contents/Info.plist
中的CFBundleIdentifier
。You have to replace 您必须将com.github.electron
by the bundle identifier of the application you created with iTunes Connect.com.github.electron
替换为使用iTunes Connect创建的应用程序的捆绑标识符。
<key>CFBundleIdentifier</key>
<string>com.example.app</string>
Code example代码示例
Here is an example that shows how to use In-App Purchases in Electron. 下面是一个示例,演示如何在Electron版中使用应用内购买。You'll have to replace the product ids by the identifiers of the products created with iTunes Connect (the identifier of 您必须将产品ID替换为使用iTunes Connect创建的产品的标识符(com.example.app.product1
is product1
). com.example.app.product1
的标识符为product1
)。Note that you have to listen to the 请注意,您必须在应用程序中尽快监听transactions-updated
event as soon as possible in your app.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.
transactions.forEach((transaction) => {
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 informations.')
return
}
// Display the name and price of each product.
products.forEach(product => {
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.')
})
})