问题
Here I have the "custom" checkout button for stripe, I was wondering how I make this execute a piece of JavaScript after the checkout process is successful. I would like it to change the "book-appointment-button" style to "block" instead of "none" (I already know how to do this). I am just struggling with how to make it so it triggers said JavaScript after the checkout has been completed if anyone can please help it would be greatly appreciated. Thanks in advance.
<!-- Start Of Stripe Payment -->
<script src="https://checkout.stripe.com/checkout.js"></script>
<button id="customButton">Purchase</button>
<script>
var handler = StripeCheckout.configure({
key: 'hidden',
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: 'DenchCodeLTD',
description: '2 widgets',
zipCode: true,
currency: 'gbp',
amount: 2000
});
e.preventDefault();
});
// Close Checkout on page navigation:
window.addEventListener('popstate', function() {
handler.close();
});
</script>
<!-- End Of Stripe Payment -->
<button id="book-appointment-submit" type="button" class="btn btn-success" style="display:none">
<span class="glyphicon glyphicon-ok"></span>
<?php echo (!$manage_mode) ? $this->lang->line('confirm') : $this->lang->line('update'); ?>
</button>
回答1:
Stripe will run the token
callback when the checkout is complete.
This is documented on Stripe's Checkout, but it is kinda hidden in the configuration options. Anyway, happy coding :)
回答2:
There is two steps:
1) In first step checkout process when user will click on button will show one popoup in which you have to enter card details. Once you are done, stripe generates token id for the current transaction. You will get token id in handler with token.id
.
2) In second step You have to make api call by using token id and your secret key to make payment or charge .
For 2nd process you can send your token id to server side by using Ajax or form submission. On server side you have to make call to stripe's charge api.
You can go through this document :
https://stripe.com/docs/checkout/php
来源:https://stackoverflow.com/questions/45656659/stripe-execute-javascript-on-form-complete