0

我正在尝试集成 Balanced Payments 以允许用户保存他们的信用卡信息以供以后使用。相关的视图代码和 javascript 文件如下,但是我在使用 javascript 文件中定义的提交处理程序时遇到了问题。

平衡卡.js

var marketplaceUri = '{MY MARKETPLACE URI}';
var requestBinUrl = '{FORM SUBMISSION PATH}'

var debug = function(tag, content) {
    $('<' + tag + '>' + content + '</' + tag + '>').appendTo('#result');
};

try {
    balanced.init(marketplaceUri);
} catch (e) {
    debug('code', 'balanced.init error!');
}

function balancedCallback(response) {
    var tag = (response.status < 300) ? 'pre' : 'code';
    debug(tag, JSON.stringify(response));
    switch(response.status) {
        case 201:
            console.log(response.data);
            var $form = $("#credit-card-form");
            var card_token_uri = response.data['uri'];
            $('<input>').attr({
                type: 'hidden',
                value: card_token_uri,
                name: 'balancedCreditCardURI'
            }).appendTo($form);
            $form.attr({action: requestBinUrl});
            $form.get(0).submit();
        case 400:
            console.log(response.error);
        case 404:
            console.log(response.error);
            break;
    }
}

var tokenizeCreditCard = function(e) {
    e.preventDefault();

    var $form = $('#credit-card-form');
    var creditCardData = {
        card_number: $form.find('.cc-number').val(),
        expiration_month: $form.find('.cc-em').val(),
        expiration_year: $form.find('.cc-ey').val(),
        security_code: $form.find('.cc-csc').val()
    }

    balanced.card.create(creditCardData, balancedCallback);
};

$('#credit-card-form').submit(tokenizeCreditCard);

credit_card.html.haml

= javascript_include_tag 'https://js.balancedpayments.com/v1/balanced.js'

= javascript_include_tag 'balanced_card'

= render :partial => 'crdtcard'

_crdtcard.html.haml

%h3 Credit Card
= form_tag '#', :method => :post, :id => "credit-card-form" do
  = label_tag "Card Number"
  = text_field_tag "cc-number", nil, :placeholder => "Enter Credit Card Number", :class => "cc-number", :autocomplete => "off"
  = label_tag "Expiration"
  = text_field_tag "cc-em", nil, :placeholder => "Expiration Month", :class => "cc-em", :autocomplete => "off"
  = text_field_tag "cc-em", nil, :placeholder => "Expiration Year", :class => "cc-ey", :autocomplete => "off"
  = label_tag "Security Code"
  = text_field_tag "cc-csc", nil, :placeholder => "Security Code", :class => "cc-csc", :autocomplete => "off"
  = submit_tag "Submit", :name => "submit"

当我使用 Firebug 进行调试时,tokenizeCreditCard 似乎从未被调用过。

4

1 回答 1

2

您错过了将绑定包装在onDomready事件中:

$(function(){
  $('#credit-card-form').submit(tokenizeCreditCard);
})
于 2013-03-15T00:25:04.203 回答