The best way is probably how we do it in the billing plugin:

https://github.com/gameclosure/billing/blob/master/js/billing.js