在Rails上使用条纹付款

时间:2019-03-18 03:22:39

标签: ruby-on-rails ruby-on-rails-5

我从网上跟踪了许多教程,以了解如何使用信用卡信用付款。我到目前为止所要做的就是: 我的付款表格:

<%= form_for(@payment, :html => {:id => 'payment-form'}) do |f| %>
            <div class="form-group">
                <%= f.label :name %>
                <%= f.text_field :name, :class => 'form-control' %>
            </div>
            <div class="form-group">
                <%= f.label :phone %>
                <%= f.text_field :phone, :class => 'form-control' %>
            </div>
            <div class="form-group">
                <%= f.label :address %>
                <%= f.text_area :address, :class => 'form-control' %>
            </div>
            <div class="form-group">
                <%= f.label 'Payment Type' %>
                <%= f.select :pay_type, Payment.pay_types.keys, {},:class => 'form-control' %>
            </div>
             <div class="form-group">
                <label>Card Number</label>
                <input type="text" size="20" autocomplete="off" data-stripe="number" id="number" class="credit-number form-control" placeholder = "**** **** **** ****" value = "4242424242424242" pattern="[\d ]*" />
            </div>

            <div class="form-group">
                <label>Security Code/CVC</label>
                <input type="text" size="4" autocomplete="off" data-stripe="cvc" id="cvc" class="credit-scurity form-control" placeholder="***" value = "123" pattern="\d*" />
            </div>

            <div class="form-group">
                <label>Expiration (MM/YYYY)</label>
                <input type="text" size="2" data-stripe="exp_month" id="exp-month" class="card-expiry-month form-control" placeholder="MM" pattern="\d*" />
                <span> / </span>
                <input type="text" size="4" data-stripe="exp_year" id="exp-year" class="card-expiry-year form-control" placeholder="YYYY" pattern="\d*" />
            </div>
            <div class="actions">
                <%= f.submit 'Place order', :class => 'btn btn-primary btn-payment' %>
            </div>
         </div>
        <% end %>

以及以下形式的脚本:

jQuery(function ($) {
  var show_error, stripeResponseHandler;
  $("#payment-form").submit(function (event) {
    var $form;
    $form = $(this);
    $form.find("input[type=submit]").prop("disabled", true);
    Stripe.card.createToken($form, stripeResponseHandler);
    return false;
  });
  stripeResponseHandler = function (status, response) {
    var $form, token;
    $form = $("#payment-form");
    if (response.error) {
      show_error(response.error.message);
      $form.find("input[type=submit]").prop("disabled", false);
    } else {
      token = response.id;
      $form.append($('<input type="hidden" name="stripeToken" />').val(token));
      $("[data-stripe=number]").remove();
      $("[data-stripe=cvv]").remove();
      $("[data-stripe=exp-year]").remove();
      $("[data-stripe=exp-month]").remove();
      $form.get(0).submit();
    }
    return false;
  };

  show_error = function (message) {
    $("#flash-messages").html('<div class="alert alert-warning"><a class="close" data-dismiss="alert">×</a><div id="flash_alert">' + message + '</div></div>');
    $('.alert').delay(5000).fadeOut(3000);
    return false;
  };
});

payments.controller:

def create
    @payment = Payment.new(payment_params)
    Stripe.api_key = "sk_test_Hm0ywtd94a4e27SHOfzVJLpZ"
    @cart = current_cart
     @amount = @cart.total_price.to_i * 100

      token = params[:stripeToken]
      debugger
      # Create a Customer
      customer = Stripe::Customer.create(
        :description => params[:email], 
        :card => token
      )


  charge = Stripe::Charge.create(

    :customer    => customer.id,
    :amount => @amount, # amount in cents, again
    :currency => 'usd',
    :card => token,
    :description => params[:email]
  )
    if @payment.save
        @payment.add_line_items_from_cart(current_cart)
        Cart.destroy(session[:cart_id])
        session[:cart_id] = nil
        redirect_to root_path
    else
        render 'new'
    end
end

现在我收到错误消息:“无法向没有有效卡的客户收费” 我正在尝试调试,问题是:

token = params[:stripeToken]

params [:stripeToken]为零,令牌也为零。 请帮助我

1 个答案:

答案 0 :(得分:0)

乔治,如果params [:stripeToken]为nil,则很好的选择是永远不要将其附加到表单上。查看您的javascript:

 if (response.error) {
  show_error(response.error.message);
  $form.find("input[type=submit]").prop("disabled", false);
} else {
  token = response.id;
  $form.append($('<input type="hidden" name="stripeToken" />').val(token));

我最好的猜测是您遇到了一个错误,该错误阻止了$ form.append代码的发生。

要检验我的理论,您应该检查所涉及的表格是否有错误。 https://developers.google.com/web/tools/chrome-devtools/javascript/