javascript - 在我的 Rails 应用程序中使用 Stripe 为卡充电时出现问题

标签 javascript ruby-on-rails forms stripe-payments

我正在构建一个接受信用卡的 Rails 应用程序,并且我正在尝试使用 Stripe 来完成它。我在将数据从我的应用程序传递到 Stripe 以便充电时遇到一些问题。这就是我希望获得有关此主题的帮助。

首先,我有一个标准表单(使用值而不是占位符来快速提交以进行测试)。表单成功地将姓名和电子邮件输入数据库,并且客户的“计划”暂时硬编码在 Controller 中:

    <%= form_for @customer do |f| %>
      <div class="payment-errors"></div>
      <div class="name field">
        <%= f.label :name %>
        <%= f.text_field :name, :value => "Your name" %>
      </div>
      <div class="email field">
        <%= f.label :email %>
        <%= f.text_field :email, :value => "yourname@example.com" %>
      </div>
      <div class="cc_number field">
        <%= label_tag 'cc_number' %>
        <%= text_field_tag 'cc_number', nil, :value => "4242424242424242" %>
      </div>
      <div class="ccv field">
        <%= label_tag 'ccv' %>
        <%= text_field_tag 'ccv', nil, :value => "123" %>
      </div>
      <div class="cc_expiration field">
        <%= label_tag 'cc_month', "Expiration date" %>
        <%= text_field_tag 'cc_month', nil, :value => "12" %>
        <%= text_field_tag 'cc_year', nil, :value => "2012" %>
      </div>
      <div class="actions">
        <%= f.submit "Continue", :class => 'btn' %>
      </div>
    <% end %>

也在我的signups_view中上面的代码在哪里,我有这个JS,mostly provided by Stripe :

<script type="text/javascript">
  // this identifies your website in the createToken call below
  Stripe.setPublishableKey('<%= STRIPE['public'] %>');

  function stripeResponseHandler(status, response) {
      if (response.error) {
          // show the errors on the form
          $(".payment-errors").text(response.error.message);
          $("input[type=submit]").removeAttr("disabled");
      } else {
          var form$ = $("form");
          // 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='customer[stripe_token]' id='stripeToken' value='" + token + "'/>");
          // and submit
          $('.cc_number.field, .ccv.field, .cc_expiration.field').remove();
          form$.get(0).submit();
      }
  }

  $(document).ready(function() {
    $("form").submit(function(event) {
      // disable the submit button to prevent repeated clicks
      $('input[type=submit]').attr("disabled", "disabled");

      Stripe.createToken({
          number: $('#cc_number').val(),
          cvc: $('#ccv').val(),
          exp_month: $('#cc_month').val(),
          exp_year: $('#cc_year').val()
      }, stripeResponseHandler);

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

</script>

form$.append("<input type='hidden' name='customer[stripe_token]' id='stripeToken' value='" + token + "'/>"); 行似乎有问题,因为我的 Ruby 应用程序在到达 customer[stripe_token] 时崩溃了。

Finally, in my `customers_controller`, I have:

  def create
    @customer = Customer.new(params[:customer])
    @customer.product = 

    if @customer.save
      save_order
      redirect_to @customer
    else
      render action: 'new'
    end

  def save_order
    Stripe.api_key = STRIPE['secret']
    charge = Stripe::Charge.create(
      :amount => 20,
      :currency => "usd",
      :card => @customer.stripe_token,
      :description => "Product 1"
    )
  end

每当我提交表单时,它都会显示 else每次在 Controller 中添加一个子句,经过大量调试、谷歌搜索、从头开始删除这个子句并从头开始重建,我仍然感到困惑。

任何帮助将非常非常感谢。

编辑:添加了customer model

  attr_accessible :name, :email, :stripe_token, :product

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :email, :presence => true,
                    :format => { :with => email_regex },
                    :length => { :minimum => 6, :maximum => 60 },
                    :uniqueness => { :case_sensitive => false }

  validates :name, :length => {:minimum => 2, :maximum => 80 }

最佳答案

查看您的客户模型以了解正在发生的情况将有所帮助。如果 @customer.save 返回 false,则意味着验证器可能失败。

此外,您的模型上是否有 stripe_token 作为可访问属性?否则,您将无法像您所做的那样从表单中分配它。请注意, token 不应存储在数据库中,因为它只能使用一次。

class Customer 
  attr_accessor :stripe_token # do you have this?
end 

还有一点需要注意:您可能需要存储 Stripe ID 字段,以便您可以检索客户付款并稍后取消他们的帐户。

关于javascript - 在我的 Rails 应用程序中使用 Stripe 为卡充电时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12028445/

相关文章:

javascript - PHP文本框动态ID生成

javascript - Jquery Slide 弄乱了其他的 div

javascript - 如何使用 Chrome 扩展延迟或拦截导航更改,直到函数在 DOM 上完成

javascript - i对于 JavaScript 文件和样式表使用 Rails 中的资源管道有什么好处吗?

javascript - 如何在 Vuex 的 Action 中调用 Action

javascript - 如何捕获表单字段中的 flagstrap 选择值?

javascript - 黑色文本 - 黑色背景时为白色

ruby-on-rails - Rails 如何 - 使用 send_data 导出数据然后重定向到新页面?

php - 在不离开/刷新页面的情况下,使用php表单向mysql数据库添加记录

php - 取消设置所有 $_POST 变量,这样我就不会在重定向时收到警告框