Wiggy A.
Wiggy A.

Reputation: 496

Stripe POST to flask through AJAX

I'm trying to use a function that sends an ajax request when a stripe form gets submitted. I've tried using .submit() and it doesn't seem to be doing anything. Here's what I'm trying:

HTML

<form action="/download_data" method="post" id="paymentForm">
                    <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                        data-key="{{ stripe_key }}"
                        data-description="A Flask Charge"
                        data-amount="500"
                        data-locale="auto"> 
                    </script>
                </form>

JS

$("#paymentForm").submit(function(event) {
    window.alert("HELLO")
    event.preventDefault();

    var $form = $(this),
        url = $form.attr('action');

    var formdata = $form.serialize();
    console.log(formdata)

    //AJAX will go here if I can get the function working
 })

And I don't think that this function is even getting called at all so I sort of stopped adding to it until I figure this out somehow. Any help would be greatly appreciated. Thanks!

Upvotes: 1

Views: 482

Answers (1)

Ywain
Ywain

Reputation: 17503

You need to use the "custom integration" method so you can define a callback that's called when Checkout creates a token, and you can then submit the token in an AJAX request (or do anything else you want):

var handler = StripeCheckout.configure({
  key: 'pk_...',
  token: function(token) {
    // Grab the token from `token.id` and send it in an AJAX request
  }
});

Here's a basic example: https://jsfiddle.net/ywain/ym0k4t9f/

Upvotes: 2

Related Questions