条纹支付成功后执行JS功能

时间:2019-03-30 01:42:36

标签: javascript stripe-payments

我已决定使用Stripe提供的自定义按钮代码来接受我出售的单个产品的付款。看起来像这样:

<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>

虽然它确实有效(当然,当我使用实际的公共API密钥时),但我找不到解决方案是在付款成功后执行自己的一些JS的方法。

我在文档中找不到答案,因此请从SO社区中寻求建议。

1 个答案:

答案 0 :(得分:1)

您可以在令牌回调中运行代码:

<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>