简体   繁体   中英

Execute JS function after successful stripe payment

I've decided to use the custom button code supplied by Stripe for accepting payment on a single product I sell. It looks like this:

<script src="https://checkout.stripe.com/checkout.js"></script>

<button id="customButton">Hire Bike (1 Day)</button>

<script>
var handler = StripeCheckout.configure({
key: 'MY_KEY',
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
locale: 'auto',
token: function(token) {
    // You can access the token ID with `token.id`.
    // Get the token ID to your server-side code for use.
}
});

document.getElementById('customButton').addEventListener('click', function(e) {
// Open Checkout with further options:
handler.open({
    name: 'Bike Company',
    description: '1 Day Bike Hire',
    currency: 'usd',
    amount: 25000
});
e.preventDefault();
});

// Close Checkout on page navigation:
window.addEventListener('popstate', function() {
handler.close();
});
</script>

While it does work (when I use my actual public API key of course), what I can't find a solution for is a way to execute some of my own JS when the payment is successful.

I can't find an answer in the documentation, so looking for suggestions from the SO community.

You can run code in the token callback:

<script src="https://checkout.stripe.com/checkout.js"></script>

<button id="customButton">Hire Bike (1 Day)</button>

<script>
var handler = StripeCheckout.configure({
key: 'MY_KEY',
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
locale: 'auto',
token: function(token) {
    // DO STUFF HERE
    alert("Wahoo! You paid!")
}
});

document.getElementById('customButton').addEventListener('click', function(e) {
// Open Checkout with further options:
handler.open({
    name: 'Bike Company',
    description: '1 Day Bike Hire',
    currency: 'usd',
    amount: 25000
});
e.preventDefault();
});

// Close Checkout on page navigation:
window.addEventListener('popstate', function() {
handler.close();
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM