I Have a form and details embedded in it. The form is submitted by AJAX. A part of the form is a Stripe payment form. When I click but submit button, I want Stripe to check the details are correct, but not submit the charge, until the rest of the form has been checked and submitted. Then the Stripe payment can be submitted.
Here is my code for the form submissions (written myself, massively simplified):
JS:
$(".booking-form").submit(function(e) {
e.preventDefault();
// JavaScipt form validation stuff. If all correct, submit form via AJAX
var data = $(".form").serialize();
$.post(bookingDetails.ajaxurl, data, function(response) {
if (response['data'] == "success") {
// Show some stuff to the user that confirms the submission, etc
}
});
});
PHP:
function sendData() {
$name = $_POST['name'];
$email = $_POST['email'];
$to = "[email protected]";
$subject = "New email";
$message = "New Email contents";
if ( (!empty($name)) && (!empty($email)) ) { // check that fields have data before submitting
$mail = mail($to, $subject, $message, $headers);
wp_send_json_success("success"); // yes, this site runs on WordPress
}
}
And the Stripe stuff (from the wp-stripe plugin, but happy to edit it or whatever)
JS:
function stripeResponseHandler(status, response) {
if (response.error) {
$(".payment-errors").show().html(response.error.message);
} else {
var form$ = $("#wp-stripe-payment-form");
var token = response['id'];
form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
var newStripeForm = form$.serialize();
$.ajax({
type : "post",
dataType : "json",
url : ajaxurl,
data : newStripeForm,
success: function(response) {
$('.wp-stripe-details').prepend(response);
}
});
}
}
$("#wp-stripe-payment-form").submit(function(event) {
event.preventDefault();
$(".wp-stripe-notification").hide();
var amount = $('.total-vat').val() * 100; //amount you want to charge
Stripe.createToken({
name: $('.wp-stripe-name').val(),
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val()
}, stripeResponseHandler);
if (paymentSuccessful) {
console.log("the payment was successful");
}
return false; // prevent the form from submitting with the default action
}
So my question is, how do I integrate the Stripe stuff and my custom stuff?