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