Skip to content

Instantly share code, notes, and snippets.

@kumar303
Last active August 29, 2015 14:15
Show Gist options
  • Save kumar303/53b30027f057cab16384 to your computer and use it in GitHub Desktop.
Save kumar303/53b30027f057cab16384 to your computer and use it in GitHub Desktop.
new fxpay interface with promises
// 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);
}
});
@muffinresearch
Copy link

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