应用内购买 (macOS)

准备工作

付费应用协议

如果你还没有,你需要在 iTunes Connect 签署付费应用协议, 并设置您的银行和税务信息。

iTunes Connect 开发人员帮助: 协议、税务和银行概述

创建您的应用内购买

然后,您需要在iTunes Connect中配置您的应用内购买,并包含名称,定价和说明等详细信息,以突出显示您的应用内购买的功能。

iTunes Connect开发人员帮助:创建应用程序内购买

变更 CFBundleIdentifier

若要在Electron开发阶段对应用内购买功能进行测试,您必须在node_modules/electron/dist/Electron.app/Contents/Info.plist路径下修改CFBundleIdentifier。 您必须使用通过ITunes Connect创建的应用的bundle indentifier来替换掉com.github.electron

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

代码示例

通过下面的例子来了解如何在Electron中使用应用内购买功能。 您必须使用通过ITunes Connect创建的产品的唯一标识 (ID)来替换掉下面示例中的PRODUCT_IDS。( com.example.app.product1 的ID是 product1)。 请注意,您必须尽可能早的在你的应用中监听transactions-updated事件。

  1. const { inAppPurchase } = require('electron').remote
  2. const PRODUCT_IDS = ['id1', 'id2']
  3. // 尽早监听transactions事件.
  4. inAppPurchase.on('transactions-updated', (event, transactions) => {
  5. if (!Array.isArray(transactions)) {
  6. return
  7. }
  8. // 检查每一笔交易.
  9. transactions.forEach(function (transaction) {
  10. const payment = transaction.payment
  11. switch (transaction.transactionState) {
  12. case 'purchasing':
  13. console.log(`Purchasing ${payment.productIdentifier}...`)
  14. break
  15. case 'purchased': {
  16. console.log(`${payment.productIdentifier} purchased.`)
  17. // Get the receipt url.
  18. const receiptURL = inAppPurchase.getReceiptURL()
  19. console.log(`Receipt URL: ${receiptURL}`)
  20. // Submit the receipt file to the server and check if it is valid.
  21. // @see https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
  22. // ...
  23. // 如果收据通过校验,说明产品已经被购买了
  24. // ...
  25. // 交易完成.
  26. inAppPurchase.finishTransactionByDate(transaction.transactionDate)
  27. break
  28. }
  29. case 'failed':
  30. console.log(`Failed to purchase ${payment.productIdentifier}.`)
  31. // Finish the transaction.
  32. inAppPurchase.finishTransactionByDate(transaction.transactionDate)
  33. break
  34. case 'restored':
  35. console.log(`The purchase of ${payment.productIdentifier} has been restored.`)
  36. break
  37. case 'deferred':
  38. console.log(`The purchase of ${payment.productIdentifier} has been deferred.`)
  39. break
  40. default:
  41. break
  42. }
  43. })
  44. })
  45. // 检查用户是否允许当前app启用应用内购买功能.
  46. if (!inAppPurchase.canMakePayments()) {
  47. console.log('The user is not allowed to make in-app purchase.')
  48. }
  49. // 检索并显示产品描述.
  50. inAppPurchase.getProducts(PRODUCT_IDS).then(products => {
  51. // 检查参数.
  52. if (!Array.isArray(products) || products.length <= 0) {
  53. console.log('Unable to retrieve the product informations.')
  54. return
  55. }
  56. // 显示每个产品的名称和价格.
  57. products.forEach(product => {
  58. console.log(`The price of ${product.localizedTitle} is ${product.formattedPrice}.`)
  59. })
  60. // 询问用户需要购买哪个产品.
  61. const selectedProduct = products[0]
  62. const selectedQuantity = 1
  63. // Purchase the selected product.
  64. inAppPurchase.purchaseProduct(selectedProduct.productIdentifier, selectedQuantity).then(isProductValid => {
  65. if (!isProductValid) {
  66. console.log('The product is not valid.')
  67. return
  68. }
  69. console.log('The payment has been added to the payment queue.')
  70. })
  71. })