我有一个使用 Stripe 购买商品的页面。下面的 javascript 代码通过 AJAX 和 PHP 填充项目,将它们作为输入字段插入。然后它监听正在提交的表单。它检查信用卡凭证是否良好。如果是这样,它应该发布到另一个 PHP 文件,该文件将检查所有其他凭据。它将返回错误或成功。不幸的是,这个 post 函数并没有执行,而是在它工作之前的所有内容。
我什至尝试将其发布到回显字符串的基本 php 文件中,以便我可以查看它是否是 PHP 错误。仍然没有任何显示。我删除了发布变量并将其更改为 get 方法。现在这工作并在 div #post-output 中显示了字符串。所以有一些东西阻止它发布到 PHP 文件中。:/
$(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.