I have a page which uses Stripe to buy items. The javascript code below is what populates the items via AJAX and PHP, inserts them in as input fields. It then listens for the form being submitted. It checks if the credit card credentials are good. If so, it's supposed to post to another PHP file which will check all other credentials. It will either return an error or success. Unfortunately, this post function does not execute but everything before it works.
I even tried to make it post to basic php file that echos a string so I could see if it was a PHP error or not. Still nothing displayed. I removed the posting variables and changed it to a get method. Now this worked and displayed the string in the div #post-output. So there is something preventing this from posting to the PHP file. :/
$(document).ready(function(){
// CHECK IF LOGGED IN
$.get( "http://mywebsite.com/logincheck.php", function( data ) {
if (data === "false") {
window.location = "register.html";
}
});
// PROCESS AND GET ALL OF THE CONTENT FROM THE DATABASE
var qpon_id = localStorage.qponid;
var shop_id = localStorage.shop;
var qg = localStorage.qg;
var variation = localStorage.variation;
var deuce = localStorage.deuce;
var thrice = localStorage.thrice;
$.post("http://mywebsite.com/cart.php", {qg : qg, variation : variation, id : qpon_id, deuce : deuce, thrice : thrice, shop : shop_id}, function(data) {
if (data.length > 0) {
$( "#qponcart" ).html( data );
}
});
// Watch for a form submission:
$("#purchase-qpons").submit(function(event) {
var error = false;
// disable the submit button to prevent repeated clicks:
$('#submit').attr('disabled', 'disabled');
// Get the values:
var ccNum = $('.card-number').val();
var cvcNum = $('.card-cvc').val();
var expMonth = $('.card-expiry-month').val();
var expYear = $('.card-expiry-year').val();
// Validate the number:
if (!Stripe.validateCardNumber(ccNum)) {
error = true;
reportError('The credit card number appears to be invalid.');
}
// Validate the CVC:
if (!Stripe.validateCVC(cvcNum)) {
error = true;
reportError('The CVC number appears to be invalid.');
}
// Validate the expiration:
if (!Stripe.validateExpiry(expMonth, expYear)) {
error = true;
reportError('The expiration date appears to be invalid.');
}
if (!error) {
// Clear any current errors
$('#payment-errors').html('');
// Get the Stripe token:
Stripe.createToken({
number: ccNum,
cvc: cvcNum,
exp_month: expMonth,
exp_year: expYear
}, stripeResponseHandler);
}
// Prevent the form from submitting:
return false;
}); // form submission
});
function reportError(msg) {
// Show the error in the form:
$('#payment-errors').text(msg).addClass('meow alert-error fade in');
// Re-enable the submit button:
$('#submit').removeAttr('disabled');
return false;
}
// Function handles the Stripe response:
function stripeResponseHandler(status, response) {
// Check for an error:
if (response.error) {
reportError(response.error.message);
} else { // No errors, submit the form:
var formcontainer = $("#purchase-qpons");
// Token contains id, last4, and card type:
var token = response['id'];
// Insert the token into the form so it gets submitted to the server
formcontainer.append("<input type='hidden' name='stripeToken' id='stripeToken' value='" + token + "' />");
// POST TO THE SERVER
$.post("http://mywebsite.com/purchase.php", {stripeToken : $("#stripeToken").val(), charge_total : $("#charge_total").val()}, function(data){
if (data.length > 0) {
$( "#post-output" ).html( data );
}
});
}
} // End of stripeResponseHandler() function.