Skip to content

Instantly share code, notes, and snippets.

@renoinn
Created May 28, 2020 10:30
Show Gist options
  • Select an option

  • Save renoinn/676211dbadcf614564072b405ea45757 to your computer and use it in GitHub Desktop.

Select an option

Save renoinn/676211dbadcf614564072b405ea45757 to your computer and use it in GitHub Desktop.
load Stripe using async / await
const StripeUrl = 'https://js.stripe.com'
/* usage
*
* async mountCard() {
* const stripe = await loadStripe(pk)
* const elements = stripe.elements()
* const card = elements.create('card')
* card.mount('#card-element')
* }
*/
export const loadStripe = async (pk, account, version = 'v3', locale = 'ja') => {
return new Promise((resolve, reject) => {
let element = document.createElement('script')
element.src = `${StripeUrl}/${version}`
element.type = 'text/javascript'
element.onload = () => {
const options = {
stripeAccount: account
apiVersion: version,
locale: locale
}
const stripe = window.Stripe(pk, options)
resolve(stripe)
}
element.onerror = e => reject(e)
document.getElementsByTagName('head')[0].appendChild(element)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment