Electron 應(yīng)用程序內(nèi)購(gòu)

2023-02-16 17:15 更新

準(zhǔn)備工作?

付費(fèi)應(yīng)用協(xié)議?

如果你還沒有,你需要在 iTunes Connect 簽署付費(fèi)應(yīng)用協(xié)議, 并設(shè)置您的銀行和稅務(wù)信息。

iTunes Connect 開發(fā)人員幫助: 協(xié)議、稅務(wù)和銀行概述

創(chuàng)建您的應(yīng)用內(nèi)購(gòu)買?

然后,您需要在iTunes Connect中配置您的應(yīng)用內(nèi)購(gòu)買,并包含名稱,定價(jià)和說明等詳細(xì)信息,以突出顯示您的應(yīng)用內(nèi)購(gòu)買的功能。

iTunes Connect開發(fā)人員幫助:創(chuàng)建應(yīng)用程序內(nèi)購(gòu)買

變更 CFBundleIdentifier

若要在Electron開發(fā)階段對(duì)應(yīng)用內(nèi)購(gòu)買功能進(jìn)行測(cè)試,您必須在node_modules/electron/dist/Electron.app/Contents/Info.plist路徑下修改CFBundleIdentifier。 您必須使用通過ITunes Connect創(chuàng)建的應(yīng)用的bundle indentifier來替換掉com.github.electron。

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

代碼示例

通過下面的例子來了解如何在Electron中使用應(yīng)用內(nèi)購(gòu)買功能。 您必須使用通過ITunes Connect創(chuàng)建的產(chǎn)品的唯一標(biāo)識(shí) (ID)來替換掉下面示例中的PRODUCT_IDS。( com.example.app.product1 的ID是 product1)。 請(qǐng)注意,您必須盡可能早的在你的應(yīng)用中監(jiān)聽transactions-updated事件。

// Main process
const { inAppPurchase } = require('electron')
const PRODUCT_IDS = ['id1', 'id2']

// Listen for transactions as soon as possible.
inAppPurchase.on('transactions-updated', (event, transactions) => {
  if (!Array.isArray(transactions)) {
    return
  }

  // 檢查每一筆交易.
  transactions.forEach((transaction) => {
    const payment = transaction.payment

    switch (transaction.transactionState) {
      case 'purchasing':
        console.log(`Purchasing ${payment.productIdentifier}...`)
        break

      case 'purchased': {
        console.log(`${payment.productIdentifier} purchased.`)

        // Get the receipt url.
        const receiptURL = inAppPurchase.getReceiptURL()

        console.log(`Receipt URL: ${receiptURL}`)

        // Submit the receipt file to the server and check if it is valid.
        // @see https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
        // ...
        // 如果收據(jù)通過校驗(yàn),說明產(chǎn)品已經(jīng)被購(gòu)買了
        // ...

        // 交易完成.
        inAppPurchase.finishTransactionByDate(transaction.transactionDate)

        break
      }

      case 'failed':

        console.log(`Failed to purchase ${payment.productIdentifier}.`)

        // 終止交易
        inAppPurchase.finishTransactionByDate(transaction.transactionDate)

        break
      case 'restored':

        console.log(`The purchase of ${payment.productIdentifier} has been restored.`)

        break
      case 'deferred':

        console.log(`The purchase of ${payment.productIdentifier} has been deferred.`)

        break
      default:
        break
    }
  })
})

// 檢查用戶是否允許當(dāng)前app啟用應(yīng)用內(nèi)購(gòu)買功能.
if (!inAppPurchase.canMakePayments()) {
  console.log('The user is not allowed to make in-app purchase.')
}

// Retrieve and display the product descriptions.
inAppPurchase.getProducts(PRODUCT_IDS).then(products => {
  // 檢查參數(shù).
  if (!Array.isArray(products) || products.length <= 0) {
    console.log('Unable to retrieve the product informations.')
    return
  }

  // Display the name and price of each product.
  products.forEach(product => {
    console.log(`The price of ${product.localizedTitle} is ${product.formattedPrice}.`)
  })

  // Ask the user which product they want to purchase.
  const selectedProduct = products[0]
  const selectedQuantity = 1

  // Purchase the selected product.
  inAppPurchase.purchaseProduct(selectedProduct.productIdentifier, selectedQuantity).then(isProductValid => {
    if (!isProductValid) {
      console.log('The product is not valid.')
      return
    }

    console.log('The payment has been added to the payment queue.')
  })
})


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)