Created
February 22, 2018 20:34
-
-
Save lfades/5e916dcffed22665071b6efbe732f255 to your computer and use it in GitHub Desktop.
Next with Stripe
This file contains 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
function LoadScript(src) { | |
return new Promise((resolve, reject) => { | |
const script = document.createElement('script') | |
script.src = src | |
script.addEventListener('load', resolve) | |
script.addEventListener('error', reject) | |
document.body.appendChild(script) | |
}) | |
} | |
export default class ComponentWithStripe extends React.Component { | |
stripe = this.loadStripeHandler(this) | |
loadStripeHandler(Component) { | |
if (!process.browser) return | |
LoadScript('https://checkout.stripe.com/checkout.js') | |
.then(() => { | |
Component.stripe = window.StripeCheckout.configure({ | |
key: 'xxxxxxx', | |
image: | |
'https://stripe.com/img/documentation/checkout/marketplace.png', | |
locale: 'auto', | |
token: this.payWithStripe | |
}) | |
}) | |
.catch(error => { | |
console.log(error) | |
}) | |
} | |
payWithStripe(token) { | |
// payment stuff | |
} | |
render () { | |
// jsx | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment