发布后的JQuery发布

时间:2013-10-31 22:18:58

标签: javascript php jquery ajax stripe-payments

我有一个使用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.

0 个答案:

没有答案
相关问题