feat: promisify In-App Purchase (#17355)

* feat: promisify In-App Purchase

* use mate::Arguments in GetProducts
This commit is contained in:
Shelley Vohr 2019-03-13 13:56:01 -07:00 committed by GitHub
parent faabd0cc8b
commit 3e5a98b5f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 113 additions and 46 deletions

View file

@ -3,7 +3,7 @@
## Preparing
### Paid Applications Agreement
If you haven't already, youll need to sign the Paid Applications Agreement and set up your banking and tax information in iTunes Connect.
If you haven't already, youll 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](https://help.apple.com/itunes-connect/developer/#/devb6df5ee51)
@ -12,7 +12,6 @@ Then, you'll need to configure your in-app purchases in iTunes Connect, and incl
[iTunes Connect Developer Help: Create an in-app purchase](https://help.apple.com/itunes-connect/developer/#/devae49fb316)
### 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.
@ -22,12 +21,10 @@ To test In-App Purchase in development with Electron you'll have to change the `
<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.
```javascript
const { inAppPurchase } = require('electron').remote
const PRODUCT_IDS = ['id1', 'id2']
@ -95,7 +92,7 @@ if (!inAppPurchase.canMakePayments()) {
}
// Retrieve and display the product descriptions.
inAppPurchase.getProducts(PRODUCT_IDS, (products) => {
inAppPurchase.getProducts(PRODUCT_IDS).then(products => {
// Check the parameters.
if (!Array.isArray(products) || products.length <= 0) {
console.log('Unable to retrieve the product informations.')
@ -103,17 +100,16 @@ inAppPurchase.getProducts(PRODUCT_IDS, (products) => {
}
// Display the name and price of each product.
products.forEach((product) => {
products.forEach(product => {
console.log(`The price of ${product.localizedTitle} is ${product.formattedPrice}.`)
})
// Ask the user which product he/she wants to purchase.
// ...
let selectedProduct = products[0]
let selectedQuantity = 1
// Purchase the selected product.
inAppPurchase.purchaseProduct(selectedProduct.productIdentifier, selectedQuantity, (isProductValid) => {
inAppPurchase.purchaseProduct(selectedProduct.productIdentifier, selectedQuantity).then(isProductValid => {
if (!isProductValid) {
console.log('The product is not valid.')
return