2016-04-22 53 views
3

我想stripe.js整合到一個web應用程序我工作的性質「stripeToken」,但是我被拋出了以下錯誤:Stripe.js - 不能讀取的不確定

Cannot read property 'stripeToken' of undefined 

的客戶方是設置令牌的隱藏輸入,但由於某些原因,服務器不能把它這樣:

var stripeToken = request.body.stripeToken; 

任何的IDE,爲什麼這可能是什麼?

客戶端JS

jQuery(function($) { 
    $('#payment-form').submit(function(event) { 
    var $form = $(this); 

    // Disable the submit button to prevent repeated clicks 
    $form.find('button').prop('disabled', true); 

    Stripe.card.createToken({ 
     number: $('.card-number').val(), 
     cvc: $('.card-cvc').val(), 
     exp_month: $('.card-expiry-month').val(), 
     exp_year: $('.card-expiry-year').val() 
    }, stripeResponseHandler); 

    // Prevent the form from submitting with the default action 
    return false; 
    }); 
}); 

function stripeResponseHandler(status, response) { 
    // Grab the form: 
    var $form = $('#payment-form'); 
    if (response.error) { // Problem! 

    // Show the errors on the form: 
    $form.find('.payment-errors').text(response.error.message); 
    $form.find('.submit').prop('disabled', false); // Re-enable submission 

    } else { // Token was created! 

    // Get the token ID: 
    var token = response.id; 

    // Insert the token ID into the form so it gets submitted to the server: 
    $form.append($('<input type="hidden" name="stripeToken">').val(token)); 

    // Submit the form: 
    $form.get(0).submit(); 
    } 
}; 

jQuery(function($) { 
    $('[data-numeric]').payment('restrictNumeric'); 
    $('.cc-number').payment('formatCardNumber'); 
    $('.cc-exp').payment('formatCardExpiry'); 
    $('.cc-cvc').payment('formatCardCVC'); 
    $.fn.toggleInputError = function(erred) { 
    this.parent('.form-group').toggleClass('has-error', erred); 
    return this; 
    }; 
    $('form').submit(function(e) { 
    e.preventDefault(); 
    var cardType = $.payment.cardType($('.cc-number').val()); 
    $('.cc-number').toggleInputError(!$.payment.validateCardNumber($('.cc-number').val())); 
    $('.cc-exp').toggleInputError(!$.payment.validateCardExpiry($('.cc-exp').payment('cardExpiryVal'))); 
    $('.cc-cvc').toggleInputError(!$.payment.validateCardCVC($('.cc-cvc').val(), cardType)); 
    $('.cc-brand').text(cardType); 
    $('.validation').removeClass('text-danger text-success'); 
    $('.validation').addClass($('.has-error').length ? 'text-danger' : 'text-success'); 
    }); 
}); 

服務器端JS

app.post('/', function(req, res) { 
    var stripeToken = request.body.stripeToken; 

    var charge = stripe.charges.create({ 
    amount: 1000, // amount in cents, again 
    currency: "usd", 
    source: stripeToken, 
    description: "Example charge" 
    }, function(err, charge) { 
    if (err && err.type === 'StripeCardError') { 
     // The card has been declined 
    } 
    }); 
}); 

表(玉)

form(novalidate='', autocomplete='on', method='POST' id="payment-form") 
    .form-group 
    label.control-label(for='cc-number') 
     | Card number formatting 
     small.text-muted 
     | [ 
     span.cc-brand 
     | ] 
    input#cc-number.input-lg.form-control.cc-number(type='tel', autocomplete='cc-number', placeholder='•••• •••• •••• ••••', required='') 
    .form-group 
    label.control-label(for='cc-exp') Card expiry formatting 
    input#cc-exp.input-lg.form-control.cc-exp(type='tel', autocomplete='cc-exp', placeholder='••/••', required='') 
    .form-group 
    label.control-label(for='cc-cvc') Card CVC formatting 
    input#cc-cvc.input-lg.form-control.cc-cvc(type='tel', autocomplete='off', placeholder='•••', required='') 
    button.btn.btn-lg.btn-primary(type='submit' class='submit') Submit 
    h2.validation 
+0

你發佈到'/'但是沒有輸出? 'app.post'文章中的'console.log',以使它被打出並輸出一些東西......嘗試確定它是否會導致錯誤。在輸出頁面之前,您還需要異步並等待條紋響應。 –

+0

我的表單和和createToken函數中的類沒有匹配,這就是它失敗的原因。現在我被服務'不能讀取undefined'的屬性'stripeToken'。接下來的挑戰! –

+0

你的請求是在變量'req'不'請求' –

回答

2

我們的要求是在變量reqrequest

這個var stripeToken = request.body.stripeToken;應該是var stripeToken = req.body.stripeToken;