Stripe を使用してアイテムを購入するページがあります。以下の JavaScript コードは、AJAX と PHP を介してアイテムを入力し、入力フィールドとして挿入するものです。次に、送信されるフォームをリッスンします。クレジット カードの資格情報が有効かどうかを確認します。その場合、他のすべての資格情報をチェックする別の PHP ファイルに投稿することになっています。エラーまたは成功のいずれかが返されます。残念ながら、この事後操作は実行されませんが、機能する前のすべてが実行されます。
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.