I want to be able to take this form - get a stripe ID with stripe checkout (code implemented and working) and then submit the form via ajax and insert the stripe id into the hidden value in the form.
What jQuery code would allow me to do this?
class signupForm(Form):
forename = StringField('Forename', validators = [ Required()])
surname = StringField('Surname', validators = [Required()])
username = StringField('Username', validators = [Required(), Length(min = 4, max = 20)])
password1 = PasswordField('Password', validators = [Required(), Length(min = 6, max=50)])
password2 = PasswordField('Password', validators = [Required(), Length(min = 6, max=50)])
email = StringField('Email', validators = [Email(), Required()])
phone = StringField('Phone number', validators = [Required()])
addressLine1 = StringField('Address Line 1', validators = [Required()])
addressLine2 = StringField('Address Line 2')
town = StringField('Town', validators = [Required()])
county = StringField('County', validators = [Required()])
postcode = StringField('Postcode', validators = [Required()])
recurringDonationQuantity = StringField('If you would like to make a monthly recurring donation, please enter the amount in Sterling below. <br> Recurring Donation Amount')
stripetoken = HiddenField('')
My page:
{% with messages = get_flashed_messages() %}
{% if messages %}
<div class="alert alert-warning" role="alert">
<ul>
{% for message in messages %}
<li>{{ message | safe }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endwith %}
{{ wtf.quick_form(form) }}
I also have this javascrpt in-page
<script>
var handler = StripeCheckout.configure({
key: '{{key}}',
image: '{{ url_for("static", filename="logo.png") }}',
locale: 'english',
token: function(token,args) {
$('#stripe-token').val = token;
wt
console.log(token)
}
});
$('#pay').on('click', function(e) {
// Open Checkout with further options
if ('{{type}}' == 'normal') {
description = 'Normal Membership';
amount = 2500;
console.log('membership type is NORMAL')
} else {
description = 'Associate Membership';
amount = 2500;
console.log('membership type is ASSOCIATE')
}
handler.open({
name: 'My Organisation',
description: description,
amount: amount,
currency: 'GBP',
panelLabel: 'Okay',
billingAddress: true,
zipCode: true
});
e.preventDefault();
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
</script>
In the end I did this, I was hoping there was an easier way:
There are so many csrf things because I haven't gotten around to working out which one made it work.
I might try something like this using jQuery's
serialize
method.Hopefully this points you in the right direction to avoid having to have so many fields hard-coded into your javascript.
You may also choose to keep the ajax call as a
'POST'
, to do that you would need to take a look at jQuery'sserializeArray
if you want to roll your own solution, or see the following code I adapted from this stackoverflow quesion:The key to take away here is that you can use javascript or jQuery to read your
<form>
data and parameterize it. You want to avoid hard-coding your postdata
if at all possible. It's a hassle to do, and it's a bigger hassle to change it down the line.