在rails 4中为customer.id收费时,条带形式无法提交

时间:2014-01-13 23:22:24

标签: ruby-on-rails ruby-on-rails-4 stripe-payments

我有一个使用Stripe处理付款的rails应用程序。当用户第一次购买物品时,一切都顺利进行。我在收费表格中添加了一个复选框,允许用户保存信用卡以备将来交易。

当我查看User对象时,我可以看到Stripe客户ID保存在模型中的customer_id字段中,但是当该用户购买带有该存储卡的内容时,表单将不会提交(当我单击按钮时) “用保存的信用卡购买”没有任何反应。

charges_controller.rb (收费控制器的相关部分)

Stripe::Charge.create(
  :amount   => @gift.price * 100,
  :currency => "cad",
  :card => params[:existing_card_id],
  :customer => @user.customer_id
)

if @charge.save 
  redirect_to gifts_path
  flash[:notice] = "You have successfully sent #{@charge.recipient_email} a #{@gift.name}!"
  CodeDelivery.send_code(@user, @gift, @charge, @code).deliver
  @code.update_attributes(:used => true)
else 
  render 'charges/new'
  flash[:alert] = "Something went wrong with your purchase."
end 

new.html.erb (第二个嵌入表单是具有customer_id的用户的表单)

<%= title "Redwood | " + @gift.name %>
<div class="container">
    <div class="modal fade" id="myModal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title"><center><%= @gift.name %></center></h4>
                </div>
                <div class="modal-body">
                    <p><%= image_tag @gift.picture.url(:mobile) %></p>
                    <p><%= @gift.description %></p>
                    <p>$<%= @gift.price %></p>
                    <% if current_user.customer_id.nil? or current_user.customer_id == 0 %>
                      <%= form_for @charge, :url => { :action => :create }, :html => { :id => "payment-form" } do |f| %>

                          <%= f.hidden_field :gift_id, :value => @gift.id %>
                          <%= f.hidden_field :user_id, :value => current_user.id %>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-send"></i>
                            <%= f.text_field :sender_name, :class => "form-control", :placeholder => "Sender's Name" %>
                          </div>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-send"></i>
                            <%= f.email_field :recipient_email, :class => "form-control", :placeholder => "Recipient's Email" %>
                          </div>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-earphone"></i>
                            <%= f.text_field :recipient_phone_number, :class => "form-control", :placeholder => "Recipient's Phone Number" %>
                          </div>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-pencil"></i>
                            <%= f.text_field :recipient_msg, :class => "form-control", :placeholder => "Message to Recipient" %>
                          </div>

                          <div class="form-group left-inner-addon ">
                            <i class="glyphicon glyphicon-credit-card"></i>
                            <%= text_field_tag :card_number, nil, :class => "form-control", :placeholder => "Credit Card Number", "data-stripe" => "number" %>
                          </div>

                           <div class="form-group">
                             <div class="row">
                               <div class="col-xs-4 left-inner-addon ">
                                 <i class="glyphicon glyphicon-calendar"></i>
                                 <%= text_field_tag :card_month, nil, :class => "form-control", :placeholder => "MM", "data-stripe" => "exp-month" %>
                               </div>
                               <div class="col-xs-5 left-inner-addon ">
                                 <i class="glyphicon glyphicon-calendar"></i>
                                 <%= text_field_tag :card_year, nil, :class => "form-control", :placeholder => "YYYY","data-stripe" => "exp-year" %>
                               </div>
                             </div>
                           </div>

                           <div class="form-group">
                             <div class="row">
                               <div class="col-xs-5 left-inner-addon ">
                                 <i class="glyphicon glyphicon-ok-circle"></i>
                                 <%= text_field_tag :card_cvv, nil, :class => "form-control", :placeholder => "CVV", "data-stripe" => "cvc" %>
                               </div>
                             </div>
                           </div>

                             <div class="checkbox"
                                 <label>Save Credit Card</label>
                                    <%= f.check_box :save_cc %>
                           </div>
                           <%= f.submit "Buy", :class => "btn btn-primary", :type => "submit" %>
                        <% end %>
                  <% else %>
                    <%= form_for @charge, :url => { :action => :create }, :html => { :id => "payment-form" } do |f| %>

                            <%= f.hidden_field :gift_id, :value => @gift.id %>
                            <%= f.hidden_field :user_id, :value => current_user.id %>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-send"></i>
                            <%= f.text_field :sender_name, :class => "form-control", :placeholder => "Sender's Name" %>
                          </div>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-send"></i>
                            <%= f.email_field :recipient_email, :class => "form-control", :placeholder => "Recipient's Email" %>
                          </div>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-earphone"></i>
                            <%= f.text_field :recipient_phone_number, :class => "form-control", :placeholder => "Recipient's Phone Number" %>
                          </div>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-pencil"></i>
                            <%= f.text_field :recipient_msg, :class => "form-control", :placeholder => "Message to Recipient" %>
                          </div>
                          <%= f.submit "Buy With Saved Credit Card", :class => "btn btn-primary", :type => "submit" %>
                        <% end %>
                  <% end %>
                </div>
                <div class="modal-footer">
                </div>
            </div>
        </div>
    </div>
</div>

stripe.html (JS部分为Stripe)

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

<script type="text/javascript">
  // This identifies your website in the createToken call below
  Stripe.setPublishableKey('pk_test_aoHfD0KHBMVsUzbr2KvpHQKi');

  var stripeResponseHandler = function(status, response) {
    var $form = $('#payment-form');

    if (response.error) {
      // Show the errors on the form
      $form.find('.payment-errors').text(response.error.message);
      $form.find('button').prop('disabled', false);
    } else {
      // token contains id, last4, and card type
      var token = response.id;
      // Insert the token into the form so it gets submitted to the server
      $form.append($('<input type="hidden" name="stripeToken" />').val(token));
      // and re-submit
      $form.get(0).submit();
    }
  };

  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($form, stripeResponseHandler);
      // Prevent the form from submitting with the default action
      return false;
    });
  });
</script>

我的申请目前可以为首次购买者或不想存储信用卡信息的人处理付款。问题在于存储信用卡的人。

1 个答案:

答案 0 :(得分:0)

通过修改第二个付款表单的表单ID(对于已保存信用卡的用户)并从控制器代码中删除:card属性,解决了此问题。

<强> new.html.erb

<%= title "Redwood | " + @gift.name %>
<div class="container">
    <div class="modal fade" id="myModal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title"><center><%= @gift.name %></center></h4>
                </div>
                <div class="modal-body">
                    <p><%= image_tag @gift.picture.url(:mobile) %></p>
                    <p><%= @gift.description %></p>
                    <p>$<%= @gift.price %></p>
                    <% if current_user.customer_id.nil? or current_user.customer_id == 0 %>
                      <%= form_for @charge, :url => { :action => :create }, :html => { :id => "payment-form" } do |f| %>

                          <%= f.hidden_field :gift_id, :value => @gift.id %>
                          <%= f.hidden_field :user_id, :value => current_user.id %>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-send"></i>
                            <%= f.text_field :sender_name, :class => "form-control", :placeholder => "Sender's Name" %>
                          </div>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-send"></i>
                            <%= f.email_field :recipient_email, :class => "form-control", :placeholder => "Recipient's Email" %>
                          </div>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-earphone"></i>
                            <%= f.text_field :recipient_phone_number, :class => "form-control", :placeholder => "Recipient's Phone Number" %>
                          </div>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-pencil"></i>
                            <%= f.text_field :recipient_msg, :class => "form-control", :placeholder => "Message to Recipient" %>
                          </div>

                          <div class="form-group left-inner-addon ">
                            <i class="glyphicon glyphicon-credit-card"></i>
                            <%= text_field_tag :card_number, nil, :class => "form-control", :placeholder => "Credit Card Number", "data-stripe" => "number" %>
                          </div>

                           <div class="form-group">
                             <div class="row">
                               <div class="col-xs-4 left-inner-addon ">
                                 <i class="glyphicon glyphicon-calendar"></i>
                                 <%= text_field_tag :card_month, nil, :class => "form-control", :placeholder => "MM", "data-stripe" => "exp-month" %>
                               </div>
                               <div class="col-xs-5 left-inner-addon ">
                                 <i class="glyphicon glyphicon-calendar"></i>
                                 <%= text_field_tag :card_year, nil, :class => "form-control", :placeholder => "YYYY","data-stripe" => "exp-year" %>
                               </div>
                             </div>
                           </div>

                           <div class="form-group">
                             <div class="row">
                               <div class="col-xs-5 left-inner-addon ">
                                 <i class="glyphicon glyphicon-ok-circle"></i>
                                 <%= text_field_tag :card_cvv, nil, :class => "form-control", :placeholder => "CVV", "data-stripe" => "cvc" %>
                               </div>
                             </div>
                           </div>

                             <div class="checkbox"
                                 <label>Save Credit Card</label>
                                    <%= f.check_box :save_cc %>
                           </div>
                           <%= f.submit "Buy", :class => "btn btn-primary", :type => "submit" %>
                        <% end %>
                  <% else %>
                    <%= form_for @charge, :url => { :action => :create }, :html => { :id => "saved-payment-form" } do |f| %>

                            <%= f.hidden_field :gift_id, :value => @gift.id %>
                            <%= f.hidden_field :user_id, :value => current_user.id %>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-send"></i>
                            <%= f.text_field :sender_name, :class => "form-control", :placeholder => "Sender's Name" %>
                          </div>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-send"></i>
                            <%= f.email_field :recipient_email, :class => "form-control", :placeholder => "Recipient's Email" %>
                          </div>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-earphone"></i>
                            <%= f.text_field :recipient_phone_number, :class => "form-control", :placeholder => "Recipient's Phone Number" %>
                          </div>

                          <div class="form-group left-inner-addon">
                            <i class="glyphicon glyphicon-pencil"></i>
                            <%= f.text_field :recipient_msg, :class => "form-control", :placeholder => "Message to Recipient" %>
                          </div>
                          <%= f.submit "Buy With Saved Credit Card", :class => "btn btn-primary", :type => "submit" %>
                        <% end %>
                  <% end %>
                </div>
                <div class="modal-footer">
                </div>
            </div>
        </div>
    </div>
</div>

<强> charges_controller.rb

Stripe::Charge.create(
  :amount   => @gift.price * 100,
  :currency => "cad",
  :customer => @user.customer_id
)

if @charge.save 
  redirect_to gifts_path
  flash[:notice] = "You have successfully sent #{@charge.recipient_email} a #{@gift.name}!"
  CodeDelivery.send_code(@user, @gift, @charge, @code).deliver
  @code.update_attributes(:used => true)
else 
  render 'charges/new'
  flash[:alert] = "Something went wrong with your purchase."
end