In-App Purchases

    iTunes Connect Developer Help: Agreements, tax, and banking overview

    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. // 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. // Check each transaction.
    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. // If the receipt is valid, the product is purchased
    24. // ...
    25. // Finish the transaction.
    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. // Check if the user is allowed to make in-app purchase.
    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. // Check the parameters.
    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 he/she wants 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. })