应用程序内购

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

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

    代码示例

    通过下面的例子来了解如何在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((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. // @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. // 终止交易
    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. // 检查用户是否允许当前app启用应用内购买功能.
    45. if (!inAppPurchase.canMakePayments()) {
    46. console.log('The user is not allowed to make in-app purchase.')
    47. }
    48. // Retrieve and display the product descriptions.
    49. inAppPurchase.getProducts(PRODUCT_IDS).then(products => {
    50. // 检查参数.
    51. if (!Array.isArray(products) || products.length <= 0) {
    52. console.log('Unable to retrieve the product informations.')
    53. return
    54. }
    55. // Display the name and price of each product.
    56. products.forEach(product => {
    57. console.log(`The price of ${product.localizedTitle} is ${product.formattedPrice}.`)
    58. })
    59. // Ask the user which product they want to purchase.
    60. const selectedProduct = products[0]
    61. const selectedQuantity = 1
    62. // Purchase the selected product.
    63. inAppPurchase.purchaseProduct(selectedProduct.productIdentifier, selectedQuantity).then(isProductValid => {
    64. if (!isProductValid) {
    65. console.log('The product is not valid.')
    66. return
    67. }
    68. console.log('The payment has been added to the payment queue.')
    69. })