应用内购买 (macOS)

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

    Code example

    Here is an example that shows how to use In-App Purchases in Electron. You'll have to replace the product ids by the identifiers of the products created with iTunes Connect (the identifier of com.example.app.product1 is product1). Note that you have to listen to the transactions-updated event as soon as possible in your app.

    1. const { inAppPurchase } = require('electron').remote
    2. const PRODUCT_IDS = ['id1', 'id2']
    3. // Listen for transactions as soon as possible.
    4. inAppPurchase.on('transactions-updated', (event, transactions) => {
    5. if (!Array.isArray(transactions)) {
    6. return
    7. }
    8. // Check each transaction.
    9. transactions.forEach(function (transaction) {
    10. var 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. let receiptURL = inAppPurchase.getReceiptURL()
    19. console.log(`Receipt URL: ${receiptURL}`)
    20. // @see https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
    21. // ...
    22. // If the receipt is valid, the product is purchased
    23. // ...
    24. // Finish the transaction.
    25. inAppPurchase.finishTransactionByDate(transaction.transactionDate)
    26. break
    27. case 'failed':
    28. console.log(`Failed to purchase ${payment.productIdentifier}.`)
    29. // Finish the transaction.
    30. inAppPurchase.finishTransactionByDate(transaction.transactionDate)
    31. break
    32. case 'restored':
    33. console.log(`The purchase of ${payment.productIdentifier} has been restored.`)
    34. break
    35. case 'deferred':
    36. console.log(`The purchase of ${payment.productIdentifier} has been deferred.`)
    37. break
    38. default:
    39. break
    40. }
    41. })
    42. // Check if the user is allowed to make in-app purchase.
    43. if (!inAppPurchase.canMakePayments()) {
    44. console.log('The user is not allowed to make in-app purchase.')
    45. }
    46. // Retrieve and display the product descriptions.
    47. inAppPurchase.getProducts(PRODUCT_IDS).then(products => {
    48. // Check the parameters.
    49. if (!Array.isArray(products) || products.length <= 0) {
    50. console.log('Unable to retrieve the product informations.')
    51. return
    52. }
    53. // Display the name and price of each product.
    54. products.forEach(product => {
    55. console.log(`The price of ${product.localizedTitle} is ${product.formattedPrice}.`)
    56. })
    57. // Ask the user which product he/she wants to purchase.
    58. let selectedProduct = products[0]
    59. let selectedQuantity = 1
    60. // Purchase the selected product.
    61. inAppPurchase.purchaseProduct(selectedProduct.productIdentifier, selectedQuantity).then(isProductValid => {
    62. if (!isProductValid) {
    63. console.log('The product is not valid.')
    64. return
    65. }
    66. console.log('The payment has been added to the payment queue.')
    67. })