In-App Purchases | Electron
https://www.electronjs.org/docs/latest/tutorial/in-app-purchases • 77 KB fetched Open original page
In-App Purchases | Electron
Skip to main content
Electron Docs API Blog Tools
* Electron Forge
* Electron Fiddle
Community
* Governance
* Showcase
* Resources
Releases English
* English
* Deutsch
* Español
* Français
* 日本語
* Português
* Русский
* 中文
Search
* Get Started
* Processes in Electron
* Best Practices
* Examples
* Dark Mode
* Device Access
* In-App Purchases
*
* Keyboard Shortcuts
* Deep Links
* Desktop Launcher Actions
*
* Menus
* Multithreading
* Native File Drag & Drop
* Navigation History
* Notifications
* Offscreen Rendering
* Online/Offline Event Detection
* Progress Bars
* Recent Documents
*
*
* Representing Files in a BrowserWindow
*
* SpellChecker
* Web Embeds
* Taskbar Customization
*
* Window Customization
* Development
* Native Node Modules
* Distribution
* Testing And Debugging
* References
* Contributing
*
* Examples
* In-App Purchases On this page
In-App Purchases
Preparing
Paid Applications Agreement
If you haven't already, you’ll need to sign the Paid Applications Agreement and set up your banking and tax information in iTunes Connect.
iTunes Connect Developer Help: Agreements, tax, and banking overview
Create Your In-App Purchases
Then, you'll need to configure your in-app purchases in iTunes Connect, and include details such as name, pricing, and description that highlights the features and functionality of your in-app purchase.
iTunes Connect Developer Help: Create an in-app purchase
Change the CFBundleIdentifier
To test In-App Purchase in development with Electron you'll have to change the CFBundleIdentifier in node_modules/electron/dist/Electron.app/Contents/Info.plist . You have to replace com.github.electron by the bundle identifier of the application you created with iTunes Connect.
< key > CFBundleIdentifier </ key >
< string > com.example.app </ string >
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.
// 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
}
// Check each transaction.
for ( const transaction of transactions ) {
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
// ...
// If the receipt is valid, the product is purchased
// ...
// Finish the transaction.
inAppPurchase . finishTransactionByDate ( transaction . transactionDate )
break
}
case 'failed' :
console . log ( ` Failed to purchase ${ payment . productIdentifier } . ` )
// Finish the transaction.
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
}
}
} )
// Check if the user is allowed to make in-app purchase.
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 => {
// Check the parameters.
if ( ! Array . isArray ( products ) || products . length <= 0 ) {
console . log ( 'Unable to retrieve the product information.' )
return
}
// Display the name and price of each product.
for ( const product of products ) {
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.' )
} )
} )
Edit this page
Previous
Device Access
Next
Keyboard Shortcuts
* Preparing
* Paid Applications Agreement
* Create Your In-App Purchases
* Change the CFBundleIdentifier
* Code example
Docs
* Getting Started
* API Reference
Checklists
* Performance
* Security
Tools
* Electron Forge
* Electron Fiddle
Community
* Governance
* Resources
* Bluesky
* X
* Mastodon
* Stack Overflow
More
* GitHub
* Open Collective
* Infrastructure Dashboard
Copyright OpenJS Foundation and Electron contributors. All rights reserved. The OpenJS Foundation has registered trademarks and uses trademarks. For a list of trademarks of the OpenJS Foundation , please see our Trademark Policy and Trademark List . Trademarks and logos not indicated on the list of OpenJS Foundation trademarks are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them. The OpenJS Foundation | Terms of Use | Privacy Policy | Bylaws | Code of Conduct | Trademark Policy | Trademark List | Cookie Policy
Hosting and infrastructure graciously provided by
Links found on this page
- Skip to main content [direct]
- Electron [direct]
- Docs [direct]
- API [direct]
- Blog [direct]
- Electron Forge [direct]
- Electron Fiddle [direct]
- Governance [direct]
- Showcase [direct]
- Resources [direct]
- Releases [direct]
- Deutsch [direct]
- Español [direct]
- Français [direct]
- 日本語 [direct]
- Português [direct]
- Русский [direct]
- 中文 [direct]
- Processes in Electron [direct]
- Best Practices [direct]
- Examples [direct]
- Dark Mode [direct]
- Device Access [direct]
- Keyboard Shortcuts [direct]
- Deep Links [direct]
- Desktop Launcher Actions [direct]
- Menus [direct]
- Multithreading [direct]
- Native File Drag & Drop [direct]
- Navigation History [direct]
- Notifications [direct]
- Offscreen Rendering [direct]
- Online/Offline Event Detection [direct]
- Progress Bars [direct]
- Recent Documents [direct]
- Representing Files in a BrowserWindow [direct]
- SpellChecker [direct]
- Web Embeds [direct]
- Taskbar Customization [direct]
- Window Customization [direct]
- Development [direct]
- Native Node Modules [direct]
- Distribution [direct]
- Testing And Debugging [direct]
- References [direct]
- Contributing [direct]
- iTunes Connect Developer Help: Agreements, tax, and banking overview [direct]
- Edit this page [direct]
- Security [direct]
- Bluesky [direct]
- X [direct]
- Mastodon [direct]
- Stack Overflow [direct]
- GitHub [direct]
- Open Collective [direct]
- Infrastructure Dashboard [direct]
- OpenJS Foundation [direct]
- Trademark Policy [direct]
- Trademark List [direct]
- Terms of Use [direct]
- Privacy Policy [direct]
- Bylaws [direct]
- Code of Conduct [direct]
- Cookie Policy [direct]
|
|