-
-
Save kumar303/53b30027f057cab16384 to your computer and use it in GitHub Desktop.
// 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); | |
} | |
}); |
The .catch
on line 21 makes it seem like eachProduct
returns a promise but it is also accepting a callback which seems to just be a shorthand for something like fxpay.products.forEach
. If it is just a shorthand I don't really see the purpose but I believe it is actually async so the fxpay.eachProduct().then(...)
API seems better. It might be good to rename eachProduct
to getProducts
(which I think is its current name).
An alternative to fxpay.preValidateReceipts()
could be fxpay.getPurchasedProducts()
which can go through the receipts and yield the products that have been purchased. I guess this could set purchaseCount
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 what fxpay.preValidateReceipts()
seems like it would trigger. It would be weird to have an onrestore
and use fxpay.getPurchasedProducts()
, perhaps that should be an error.
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(){})
One subtle difference from the old and new code is that by deferring receipt validation until after fetching products, restoration might be a little slower. If this is a real problem I figure we could introduce
fxpay.preValidateReceipts()
if the developer wants the speed-up.