Last active
August 29, 2015 14:15
-
-
Save kumar303/53b30027f057cab16384 to your computer and use it in GitHub Desktop.
new fxpay interface with promises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fetch products from the API and also restore products from receipts. | |
// This used to be fxpay.init() and fxpay.getProducts() | |
// Old code: | |
// https://github.com/mozilla/fxpay/blob/master/example/shared/js/index.js#L41-L50 | |
// https://github.com/mozilla/fxpay/blob/master/example/shared/js/index.js#L249-L257 | |
fxpay.getProducts().then(function(allProducts) { | |
allProducts.forEach(function(product) { | |
console.log('product ID:', product.id); | |
console.log('product name:', product.name); | |
if (product.receiptExists()) { | |
product.validateReceipt().then(function() { | |
console.log('product', this, 'can be restored'); | |
}).catch(function(error) { | |
console.error('invalid receipt for installed product:', error); | |
}); | |
} else { | |
// Create a purchase button for this product. | |
} | |
}); | |
}).catch(function(error) { | |
console.error('error getting products:', error); | |
}); | |
// Purchase a product. | |
// Old code: https://github.com/mozilla/fxpay/blob/master/example/shared/js/index.js#L125-L134 | |
fxpay.purchase(product.id).then(function(productInfo) { | |
console.log('product has been purchased:', productInfo); | |
}).catch(function(error) { | |
console.error('error purchasing product', error.productInfo, 'error:', error); | |
}); | |
// Validate an app receipt. | |
// Old code: https://github.com/mozilla/fxpay/blob/master/example/hosted-paid-app/media/js/index.js#L33-L51 | |
fxpay.validateAppReceipt().then(function(receipt) { | |
console.log('receipt is valid; app was purchased'); | |
console.log('product URL:', receipt.productUrl); | |
console.log('receipt status:', receipt.validationResult.status); | |
}).catch(function(error) { | |
console.error('error validating receipt:', error); | |
var receiptInfo = error.productInfo && error.productInfo.receiptInfo; | |
if (receiptInfo.validationResult) { | |
console.log('receipt status:', receiptInfo.validationResult.status); | |
console.log('receipt status reason:', receiptInfo.validationResult.reason); | |
} | |
}); |
Yeah, after this new API, fxpay.init()
could go away entirely. No need for onrestore()
.
Re: fxpay.eachProduct()
I'm not sure about methods starting with each. I think it makes more sense to have that be fxpay.getProducts().then(function(products){}, function(){})
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
.catch
on line 21 makes it seem likeeachProduct
returns a promise but it is also accepting a callback which seems to just be a shorthand for something likefxpay.products.forEach
. If it is just a shorthand I don't really see the purpose but I believe it is actually async so thefxpay.eachProduct().then(...)
API seems better. It might be good to renameeachProduct
togetProducts
(which I think is its current name).An alternative to
fxpay.preValidateReceipts()
could befxpay.getPurchasedProducts()
which can go through the receipts and yield the products that have been purchased. I guess this could setpurchaseCount
on the product or something. Alternatively it could set a list of receipts on it. This might make more sense as you'd likely want to restore the purchases at startup but then fetch all products when the user enters some sort of purchase mode.I'm assuming you wouldn't register an
onrestore
callback if you were using promises which is whatfxpay.preValidateReceipts()
seems like it would trigger. It would be weird to have anonrestore
and usefxpay.getPurchasedProducts()
, perhaps that should be an error.