인앱 결제
준비 작업
유료 애플리케이션 계약
아직 서명하지 않았다면, iTunes Connect에서 유료 애플리케이션 계약에 서명하고 은행 및 세금 정보를 설정해야 한다.
iTunes Connect 개발자 도움말: 계약, 세금, 은행 개요
인앱 구매 설정하기
다음으로 iTunes Connect에서 인앱 구매를 구성해야 한다. 인앱 구매의 이름, 가격, 기능과 특징을 강조하는 설명 등 세부 정보를 포함시킨다.
iTunes Connect 개발자 도움말: 인앱 구매 생성
Electron에서 개발 중인 In-App Purchase를 테스트하려면 node_modules/electron/dist/Electron.app/Contents/Info.plist
파일의 CFBundleIdentifier
를 변경해야 한다. com.github.electron
을 iTunes Connect에서 생성한 애플리케이션의 번들 식별자로 바꿔야 한다.
<key>CFBundleIdentifier</key>
<string>com.example.app</string>
코드 예제
다음은 Electron에서 인앱 결제를 사용하는 방법을 보여주는 예제다. com.example.app.product1
같은 제품 ID를 iTunes Connect에서 생성한 제품의 식별자로 바꿔야 한다. (product1
이 식별자다.) 앱에서 가능한 한 빨리 transactions-updated
이벤트를 감지해야 한다는 점에 유의한다.
// 메인 프로세스
const { inAppPurchase } = require('electron')
const PRODUCT_IDS = ['id1', 'id2']
// 가능한 한 빨리 트랜잭션을 감지한다.
inAppPurchase.on('transactions-updated', (event, transactions) => {
if (!Array.isArray(transactions)) {
return
}
// 각 트랜잭션을 확인한다.
for (const transaction of transactions) {
const payment = transaction.payment
switch (transaction.transactionState) {
case 'purchasing':
console.log(`${payment.productIdentifier} 구매 중...`)
break
case 'purchased': {
console.log(`${payment.productIdentifier} 구매 완료.`)
// 영수증 URL을 가져온다.
const receiptURL = inAppPurchase.getReceiptURL()
console.log(`영수증 URL: ${receiptURL}`)
// 영수증 파일을 서버에 제출하고 유효성을 확인한다.
// @see https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
// ...
// 영수증이 유효하면 제품 구매가 완료된다.
// ...
// 트랜잭션을 완료한다.
inAppPurchase.finishTransactionByDate(transaction.transactionDate)
break
}
case 'failed':
console.log(`${payment.productIdentifier} 구매 실패.`)
// 트랜잭션을 완료한다.
inAppPurchase.finishTransactionByDate(transaction.transactionDate)
break
case 'restored':
console.log(`${payment.productIdentifier} 구매가 복원되었습니다.`)
break
case 'deferred':
console.log(`${payment.productIdentifier} 구매가 연기되었습니다.`)
break
default:
break
}
}
})
// 사용자가 인앱 결제를 할 수 있는지 확인한다.
if (!inAppPurchase.canMakePayments()) {
console.log('사용자가 인앱 결제를 할 수 없습니다.')
}
// 제품 설명을 가져와 표시한다.
inAppPurchase.getProducts(PRODUCT_IDS).then(products => {
// 매개변수를 확인한다.
if (!Array.isArray(products) || products.length <= 0) {
console.log('제품 정보를 가져올 수 없습니다.')
return
}
// 각 제품의 이름과 가격을 표시한다.
for (const product of products) {
console.log(`${product.localizedTitle}의 가격은 ${product.formattedPrice}입니다.`)
}
// 사용자에게 어떤 제품을 구매할지 묻는다.
const selectedProduct = products[0]
const selectedQuantity = 1
// 선택한 제품을 구매한다.
inAppPurchase.purchaseProduct(selectedProduct.productIdentifier, selectedQuantity).then(isProductValid => {
if (!isProductValid) {
console.log('제품이 유효하지 않습니다.')
return
}
console.log('결제가 결제 큐에 추가되었습니다.')
})
})